Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language programming: input an integer, output it bit by bit in positive or reverse order, and do it recursively.
C language programming: input an integer, output it bit by bit in positive or reverse order, and do it recursively.
C language programming: input an integer, output it bit by bit in positive or reverse order, and do it recursively. First, the problem analysis:

In order to output integers bit by bit, it is necessary to separate each bit, whether in positive or reverse order.

The practice in C is to take the remainder of 10, take out one bit, and then divide it by 10 to achieve the effect of "shifting".

Second, the algorithm design:

1. Enter the integer to be processed;

2. Take everyone out;

3. Recursive call, with the current value divided by 10 as the independent variable;

4. Exit when the value is 0.

Positive sequence and reverse sequence output depend on the positional relationship between recursive call and output.

Third, the reference code:

Positive sequence:

# include & ltstdio.h & gtvoid func(int n){ int v; If(n==0) returns; It ends at 0. v = n % 10; Remove a little. func(n/ 10); Recursively call printf("%d ",v); Output. } int main(){ int n; scanf("%d ",& ampn); Input. func(n); Call the function to output bits in positive order. Returns 0; }

Reverse order:

# include & ltstdio.h & gtvoid func(int n){ int v; If(n==0) returns; It ends at 0. v = n % 10; Remove a little. printf("%d ",v); Output. func(n/ 10); Recursively call} int main(){ int n;; scanf("%d ",& ampn); Input. func(n); Call the function to output bits in positive order. Returns 0; }

C language programming input an integer, calculate and output the square of the number.

Method 1:

# include & ltstdio.h & gt

int square(int m)

{

Returns m * m

}

Major (invalid)

{

int a,result

Printf ("Please enter: \ n");

scanf("%d ",& ampa);

Result = square (a);

printf("%d*%d=%d\n ",a,a,result);

}

Method 2:

# include & ltstdio.h & gt

# include & ltmath.h & gt

Major (invalid)

{

int a,result

Printf ("Please enter: \ n");

scanf("%d ",& ampa);

result=(int)pow(a,2);

printf("%d*%d=%d\n ",a,a,result);

}

I hope I can help you. If you are satisfied, please adopt it!

Input any integer by programming in C language, and output it in reverse order.

# include & ltstdio.h & gt

# include & ltstring.h & gt

void main()

{

char str[ 100];

int n;

Printf ("Enter a number:");

gets(str);

n = strlen(str);

for(; n & gt=0; n - )printf("%c ",str[n]);

getchar();

}

C language programming: input a three-digit integer and output it reversely. If 123, output 32 1.

# include & ltstdio.h & gt

Master ()

{

int I;

int j;

char n[ 100];

char m[ 100];

Printf ("Please enter a three-digit integer: \ n");

I = 0;

n[I]= getchar();

while(n[i]! = '\n ')

{

++ I;

n[I]= getchar();

}

-Me;

j = 0;

while(n[j]! = '\n ')

{

m[I]= n[j];

-Me;

++ j;

}

Printf ("The reverse order of this line is: \ n");

printf("%s\n ",m);

System ("suspended");

}

C language programming: output the inverse of a 3-digit integer, such as input 123 and output 32 1.

# include & ltstdio.h & gt

void main()

{int x,a,b,c;

scanf("%d ",& ampx);

a = x/ 100;

b = x/ 10% 10;

c = x % 10;

x = c * 100+b * 10+a;

printf("%d ",x);

}

Input an integer array, request the program to output the maximum value, and program in C language.

# include & ltstdio.h & gt

# include & ltmalloc.h & gt

int main()

{

int * arr = NULL

Unsigned int i, length = 0;

int max

do

{

Printf ("Please enter array length:");

Scanf("%d ",& length);

}while (length < = 0);

Printf("%d\n ",length);

arr =(int *)malloc(length * size of(int));

if(arr == NULL)

{

Printf ("memory allocation error");

return- 1;

}

for(I = 0; I < length; i++)

{

scanf("%d ",& amparr[I]);

}

max = arr[0];

for(I = 0; I < length; i++)

{

if(max & lt; arr[i])

{

max = arr[I];

}

}

Printf ("Maximum quantity is %d\n", max);

Returns 0;

}

I haven't written for a long time, but I have written the stupidest method. I remember that there seem to be two variables that can be solved.

Design a program that requires an integer to be input and can be output bit by bit in positive and reverse order. For example, input 3456 and outputs 3456 and 6543.

Positive sequence: direct output

Reverse order: Take 3456 as an example, suppose that the variable n=3456 has been defined.

N% 10 to get the last number and output it; n = n/ 10; At this time, n = 345 cycles this step until n/ 10=0.

For your own good, don't post the code, experience it yourself, and wish you success!

C language programming topic, find the sum of all digits of an integer, and realize it with recursive function. For example, input 345 outputs 12.

# include & ltstdio.h & gt

int sum(int n)

{

If(n==0) returns 0;

other

Returns n%10+sum (n/10);

}

void main()

{

int n;

scanf("%d ",& ampn);

printf("%d\n ",sum(n));

}

C language programming: enter an integer and find the sum of all numbers. How to write the program?

# include & ltstdio.h & gtvoid main(){ int n,s = 0; scanf("%d ",& ampn); while(n){ s+= n % 10; n/= 10; } printf("S=%d ",S); 9. Programming language: A formal language used to define computer programs. It is a standardized communication skill used to send instructions to computers. Computer language enables programmers to accurately define the data needed by computers and the actions that should be taken in different situations.

10. Classification: machine language, assembly language, high-level language.

1 1. Form: assembly language and high-level language.

C language programming: input an integer and output all prime factors of the integer. For example, the input is 120 and the outputs are 2, 2, 2, 3 and 5.

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

bool isPrime(int n)

{

If(n== 1) returns false.

for(int I = 2; I * I < = n; i++) {

If(n%i ==0) returns false.

}

Return true

}

int main()

{

int n,I,j,m;

Printf ("Please enter n: \ n");

scanf("%d ",& ampn);

m = n;

for(I = 2; I < = n; i++) {

if(isPrime(i)){

while(n % I = = 0 & amp; & ampn & gt0) {

printf("%d ",I);

n/= I;

}

}

}

printf(" \ n ");

System ("suspended");

Returns 0;

}