search
HomeComputer TutorialsComputer KnowledgeWrite 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

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:Excel办公网. If there is any infringement, please contact admin@php.cn delete
Fixes for ExpressVPN Not Connecting on Windows PC/Mac/iPhone - MiniToolFixes for ExpressVPN Not Connecting on Windows PC/Mac/iPhone - MiniToolMay 01, 2025 am 12:51 AM

Is ExpressVPN not connecting on iPhone, Android phone, Mac, or Windows PC? What should you do if ExpressVPN won’t connect or work? Take it easy and go to find solutions from this post. Here, php.cn collects multiple ways to solve this internet issue.

Targeted Solutions for 100% GPU Usage on Windows 10/11Targeted Solutions for 100% GPU Usage on Windows 10/11May 01, 2025 am 12:50 AM

Is 100% GPU usage bad? How to fix 100% GPU usage in Windows 10? Don’t fret. php.cn Website provides you with this article to work out high GPU issues. Those methods are worth a try if you are undergoing 100% GPU usage.

How to Download/Install/Add/Remove/Disable Add-ins in Word - MiniToolHow to Download/Install/Add/Remove/Disable Add-ins in Word - MiniToolMay 01, 2025 am 12:49 AM

You can use add-ins in Microsoft Word to get more features. This post introduces how to download, install, add or remove add-ins in Microsoft Word. A free file recovery method is also provided to help you recover deleted/lost Word or any other files.

Fix the System Backup Error Codes 0x807800A1 & 0X800423F3 - MiniToolFix the System Backup Error Codes 0x807800A1 & 0X800423F3 - MiniToolMay 01, 2025 am 12:48 AM

Some people find the system backup error codes 0x807800A1 & 0X800423F3 when they attempt to create a system backup. These codes will prevent you from doing any backup tasks. Don’t worry! This article on php.cn Website will teach you how to fix th

Windows 10 Enterprise ISO Download/Install, Easy Guide to LearnWindows 10 Enterprise ISO Download/Install, Easy Guide to LearnMay 01, 2025 am 12:47 AM

Want to get an ISO file of Windows 10 Enterprise to install it for business usage? Windows 10 Enterprise ISO download & install are easy and php.cn will show you how to download Windows 10 Enterprise ISO 20H2, 21H1, or 21H2 via some direct downlo

NordVPN Not Working on Windows 11: Here Are Easy Fixes - MiniToolNordVPN Not Working on Windows 11: Here Are Easy Fixes - MiniToolMay 01, 2025 am 12:46 AM

NordVPN not connecting or working on your Windows 11 computer? Do you know the reasons for this issue? If you want to solve this problem, do you know what you should do? If you have no idea, you come to the right place. In this post, php.cn Software

Is Twitter Down? How to Check It? How to Fix It? Read This Post! - MiniToolIs Twitter Down? How to Check It? How to Fix It? Read This Post! - MiniToolMay 01, 2025 am 12:45 AM

Nowadays, Twitter becomes more and more popular all over the world. However, sometimes you may find it is not working properly. Is Twitter down? How to check it? How to fix it? This post from php.cn provides details for you.

Redfall Release Date, Trailers, Pre-Order, and Latest News - MiniToolRedfall Release Date, Trailers, Pre-Order, and Latest News - MiniToolMay 01, 2025 am 12:44 AM

When does Redfall come to market? Redfall fans can’t wait to try this game. fortunately, the release date has been clarified and more information we know about Redfall will be all disclosed in this article on php.cn Website, so if you are interested,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function