Home Computer Tutorials Computer Knowledge 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

Jan 16, 2024 pm 07:57 PM

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Fix the Steam Cloud Error? Try These Methods How to Fix the Steam Cloud Error? Try These Methods Apr 04, 2025 am 01:51 AM

The Steam Cloud error can be caused by many reasons. To play a game smoothly, you need to take some measures to remove this error before you launch the game. php.cn Software introduces some best ways as well as more useful information in this post.

Windows Metadata and Internet Services Problem: How to Fix It? Windows Metadata and Internet Services Problem: How to Fix It? Apr 02, 2025 pm 03:57 PM

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.

Fixed – OneDrive Not Uploading Photos on PC Fixed – OneDrive Not Uploading Photos on PC Apr 02, 2025 pm 04:04 PM

OneDrive is an online cloud storage service from Microsoft. At times, you might find OneDrive fail to upload photos to the cloud. If you are on the same boat, keep reading this post from php.cn Software to get effective solutions now!

How to Resolve the KB5035942 Update Issues – Crashing System How to Resolve the KB5035942 Update Issues – Crashing System Apr 02, 2025 pm 04:16 PM

KB5035942 update issues - crashing system commonly happens to users. Inflicted people hope to find a way out of the kind of trouble, such as crashing system, installation, or sound issues. Targeting these situations, this post published by php.cn wil

Remove PC App Store Malware - A Full Guide for You! Remove PC App Store Malware - A Full Guide for You! Apr 04, 2025 am 01:41 AM

If you have a program called PC App Store on your computer and did not purposely install it, then your PC may be infected with the malware. This post from php.cn introduces how to remove PC App Store malware.

Fix: Brothers: A Tale of Two Sons Remake Not Launching/Loading Fix: Brothers: A Tale of Two Sons Remake Not Launching/Loading Apr 02, 2025 am 02:40 AM

Is Brothers: A Tale of Two Sons Remake not launching? Encountering Brothers: A Tale of Two Sons Remake black screen? Here this post on php.cn offers you tested solutions to assist you in addressing this problem.

How to Use Chris Titus Tool to Create a Debloated Win11/10 ISO How to Use Chris Titus Tool to Create a Debloated Win11/10 ISO Apr 01, 2025 am 03:15 AM

Chris Titus Tech has a tool called Windows Utility that can help you easily create a debloated Windows 11/10 ISO to install a clean system. php.cn offers a full guide on how to do this thing using the Chris Titus tool.

Is Core Isolation Blocked by ew_usbccgpfilter.sys? Here Are Fixes! Is Core Isolation Blocked by ew_usbccgpfilter.sys? Here Are Fixes! Apr 13, 2025 am 12:47 AM

Many SurfaceBook users report that they meet the “core isolation blocked by ew_usbccgpfilter.sys” issue on Windows 11/10. This post from php.cn helps to fix the annoying issue. Keep on your reading.

See all articles