Some simple C language algorithms and simple C language Algorithms
1.
Enter a number to print the following figure.
Input 5
Print:
*
**
***
****
*****
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int num, I, j;
Scanf ("% d", & num );
For (I = 1; I <= num; I ++)
{
For (j = 1; j <I + 1; j ++)
{
Printf ("*");
}
Printf ("\ n ");
}
Return 0;
}
2.
Enter a number to print the following figure.
Input 5
Print:
*
**
***
****
*****
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int num, I, j, k;
Scanf ("% d", & num );
For (I = 1; I <= num; I ++)
{
For (j = 1; j <= num-I; j ++)
{
Printf ("");
}
For (k = 1; k <I + 1; k ++)
{
Printf ("*");
}
Printf ("\ n ");
}
Return 0;
}
3.
Enter a number to print the following figure.
Input 5
Print:
*
***
*****
*******
*********
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int num, I, j, k, l;
Scanf ("% d", & num );
For (I = 1; I <= num; I ++)
{
For (j = 1; j <= num-I; j ++)
{
Printf ("");
}
For (k = 1; k <I * 2-1 + 1; k ++)
{
Printf ("*");
}
For (l = 1; l <= num-I; l ++)
{
Printf ("");
}
Printf ("\ n ");
}
Return 0;
}
4.
Enter a number to print the following figure.
Input 5
Print
ABBBB
AABBB
AAABB
AAAAB
AAAAA
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int num, I, j, k, l;
Scanf ("% d", & num );
For (I = 1; I <= num; I ++)
{
For (j = 1; j <= num-I; j ++)
{
Printf ("");
}
For (k = 1; k <I + 1; k ++)
{
Printf ("");
}
For (l = 1; l <= num-I; l ++)
{
Printf ("B ");
}
Printf ("\ n ");
}
Return 0;
}
5.
Enter a number to determine whether it is a prime number )(****)
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int nu, I;
Scanf ("% d", & nu );
For (I = 2; I <nu; I ++)
{
If (nu % I = 0)
{
Break; // if it can be fully divided, it indicates that it is not a prime number.
}
}
If (I = nu) // if it is equal to nu, it indicates that it is not a factor.
{
Printf ("is a prime number ");
}
Else
{
Printf ("not a prime number ");
}
Return 0;
}
6.
Enter two numbers and calculate the maximum common divisor of the two numbers (*****)
// The maximum public factor, also known as the maximum public approx. And the maximum public factor, refers to the maximum total number of two or more Integers
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int I, numA, numB, mix;
Scanf ("% d, % d", & numA, & numB );
Mix = (numA <numb )? NumA: numB;
For (I = mix; I> = 1; I --)
{
If (numA % I = 0 & numB % I = 0)
{
Break;
}
}
Printf ("% d", I); // The result of I is the maximum public approx.
Return 0;
}
7.
Enter two numbers to obtain the minimum public multiple of them (****)
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int numa, numb, max;
Scanf ("% d, % d", & numa, & numb );
Max = (numa <numb )? Numa: numb;
While (1) // find out that the smallest number that can just divide two numbers is the least common multiple.
{
If (max % numa = 0 & max % numb = 0)
{
Break;
}
Max ++;
}
Printf ("% d", max );
Return 0;
}
8.
Enter a number to separate the prime factor (*****)
Factorization factor: Splits a sum into the product of several prime numbers.
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int nu;
Scanf ("% d", & nu );
For (int I = 2; I <= nu; I ++ ){
If (nu % I = 0) {// use the smallest factor (I)
Printf ("% d", I );
Nu/= I; // get
I --;
}
}
Return 0;
}
9.
Enter two numbers n, a, if n = 3, a = 2;
Output 2 + 22 + 222 values. (No output sub-statement is required )(****)
M = 5, n =
3 + 33 + 333 + 3333 + 33333
10*3 + 3
10*33 + 3
Ret = 10 * ret +
Sum = sum + ret
*/
# Include <stdio. h>
Int main (int argc, const char * argv []) {
// Insert code here...
Int m,;
Scanf ("% d", & m, & );
Int sum = 0;
Int ret = 0;
For (int I = 1; I <= m; I ++)
{
Ret = 10 * ret + a; // ret is the current value, such as 3,33, 333,3333
Sum = sum + ret;
}
Printf ("% d \ n", sum );
Return 0;
}
10.
In the five-digit number, the symmetric number is called the return number to find all the return numbers.
For example, 12321 (***)
# Include <stdio. h>
Int main (int argc, const char * argv []) {
// Insert code here...
For (int I = 10000; I <= 99999; I ++)
{
Int bita, bitb, bitc, bitd;
// 12345% 10 = 5
Bita = I % 10;
// 12345/10 = 1234 1234% 10 = 4
Bitb = I/10% 10;
// 12345/1000 = 123 12% 10 = 2
Bitc = I/1000% 10;
// 12345/10000 = 1 1% 10 = 1
Bitd = I/10000% 10;
If (bita = bitd & bitb = bitc)
{
Printf ("% d", I );
}
}
Return 0;
}
11.
Input any majority, the last number is 0, and the maximum number of these numbers is output. (**)
Thought: input a number each time for comparison, and save the current maximum number each time.
# Include <stdio. h>
Int main (int argc, const char * argv []) {
// Insert code here...
Int nu, max;
Scanf ("% d", & nu );
Max = nu; // The tentative first number is the maximum value, which is a good idea.
While (1)
{
Scanf ("% d", & nu );
If (nu = 0)
{
Break;
}
If (max <nu)
{
Max = nu;
}
}
Printf ("% d", max );
Return 0;
}
12.
Enter a number to calculate the factorial of this number. (*)
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int num1, I, sum = 1;
Scanf ("% d", & num1 );
For (I = 1; I <= num1; I ++)
{
Sum * = I;
}
Printf ("% d \ n", sum );
Return 0;
}
13.
Input an integer (within the int range) and output 10 hexadecimal values in reverse order.
# Include <stdio. h>
Int main (int argc, const char * argv []) {
Int num1, I = 0, shu [64], j;
Scanf ("% d", & num1 );
// Take out each digit of this number, and arrange the order right after it is obtained.
While (1)
{
Shu [I] = num1 % 10;
Num1 = num1/10;
I ++;
If (num1 = 0)
{
Break;
}
}
// Print each digit of the number in the order
For (j = 0; j <I; j ++)
{
Printf ("% d", shu [j]);
}
Printf ("\ n ");
Return 0;
}