Home  >  Article  >  Computer Tutorials  >  Write a program in C language to calculate the sum of the digits of an integer

Write a program in C language to calculate the sum of the digits of an integer

王林
王林forward
2024-01-16 19:57:151660browse

Write a program in C language to calculate the sum of the digits of an integer

A c language array programming question: write a program to sum the digits of an integer

The key to this question is to separate numbers. You can use this method to separate. For example, use 119 to achieve separation. According to the question, the result is 1 1 9 = 11, right? We can separate in this way, (1) Using 119, the remainder after dividing 119 by 10 is 9, right? If you are smart, you should have discovered that 9 has been separated by now. (2) Then separate the tens digits. The result of 119/10 can only be an integer, which is 11, right? Then divide 11 and 11 by 10, and the remainder is 1 pair. Bar? Is 1 separated again? (3) Finally, it is the hundreds digit. The result of 11/10 is 1, right, and the hundreds digit is also separated. (4) In the end, we only need to add them.

The end condition of separation is when the integer division result is less than 10.

During the separation process, we can use arrays to store the separated numbers.

The specific code is as follows:

/*Method 1 is a recursive method. I think this method is simpler and the best to understand*/

#include

int show(int num)

{

if(num

return num;/*recursive exit*/

else

return num show(num/10);

}

void main()

{

int num,sum;

printf ("Please enter an integer:");

scanf("%d",&num);

sum=show(num);

printf ("The result of the sum of each digit is: %d\n",sum);

}

/*Method 2 is done using arrays. The method may be too complicated. Please don’t suggest it. If you have any good methods, please share them*/

#include

void main()

{

int value[10];

int num;

printf ("Please enter an integer:");

scanf("%d",&num);

while(num=10000)

{

printf ("This code currently only supports the calculation of 4-digit numbers below 10000, and the input integer is invalid! Please re-enter:");

scanf("%d",&num);

}

if(num>=0&num

printf("The result is: %d\n",num);

else if(num>=10&num

{

value[0]=num ;

value[1]=num/10;

printf ("The result is: %d\n", value[0] value[1]);

}

else if(num>=100&num

{

value[0]=num; /*Separate single digits*/

value[1]=num/10; /*Get the new number to be separated*/

value[2]=value[1]; /*Separate tens digits*/

value[3]=value[1]/10; /*Get the hundreds digit*/

printf ("The result is: %d\n", value[0] value[2] value[3]);

}

else

{

value[0]=num; /*Separate single digits*/

value[1]=num/10; /*Get the new number to be separated*/

value[2]=value[1]; /*Separate tens digits*/

value[3]=value[1]/10; /*Get the new number to be separated*/

value[4]=value[3]; /*Get the hundreds digit*/

value[5]=value[3]/10; /*Get the number in thousands*/

printf ("The result is: %d\n", value[0] value[2] value[4] value[5]);

}

}

A question about arrays in C language C experts can help

#include

main()

{ int a[ ]={1,2,3,4,5,6,7,8,9,0},*p; //An array of 10 elements is defined, each element is of type int , and also defines a p pointer.

p=a; //p pointer points to array a

printf (“%x\n”,p); //The output displays the address of p in hexadecimal,

printf (“%x\n”,p 9);//p 9 means moving the address by a distance of 9 int types and then changing it to hexadecimal display,

}

If you really want to print the address, just use the %p format. The default is hexadecimal. The effect is the same as %x, but some operations are omitted.

If a variable of type int occupies 2 bytes,

The output of the first printf statement is 194, then the output result of the second printf statement is

This question means to ask you how many bytes are occupied from the first element to the last element

0x194 2* 9=

Calculate it yourself, too lazy to convert.

194 represents the first address of the array in hexadecimal. An Int type occupies 2 bytes, so the distance to move 9 elements is 2*9 bytes, and then the address is displayed.

This goes deep into the representation format of the type in the memory. Just remember that 1 means the distance to move an element type.

A few simple C language programming questions about arrays. Thanks!

first question:

#include "stdio.h"

void main()

{int a[10];

int i,max;

for(i=0;i

scanf("%d,",&a[i]);

max=a[0];

for(i=1;i

{

if(max

max=a[i];

}

printf("Maximum value is: %d",max);

}

Second question:

#include "stdio.h"

void main()

{

int i,a[10];

long int sum; /*sum is the total score*/

float average; /*average*/

for(i=0;i

{

printf ("Please enter the score of student %d:", (i 1));

scanf("%d",a[i]);

printf("\n");

}

for(i=0;i

sum=sum a[i];

average=sum/10.0;

printf("The average score of 10 students is: %f", average);

}

The third question:

#include "stdio.h"

void main()

{

int a[10]={1,2,3,4,5,6,7,8,9,10};

int b[10]={2,4,6,8,10,12,14,16,18,20};

int c[10];

for(i=0;i

c[i]=a[i] b[i];

for(i=0;i

printf("%d,",c[i]);

}

Question 4:

#include "stdio.h"

{

int i,a[100];

int cx;

for(i=1;i

{

a[i]=i 2;

}

scanf("Enter the value you want to query: %d",cx);

for(i=0;i

if(cx==a[i])

{

printf ("The %dth number is the value you want to query!");

break;

}

else

printf ("The value you entered is not in this array");

}

The above is the detailed content of Write a program in C language to calculate the sum of the digits of an integer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete