C-Code to sum the digits of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i;
printf("Enter the number: ");
scanf("%d",&n);
while(n)
{
i=num%10;
num=num/10;
sum=sum+i;
}
printf("Sum of digits of %d is %d",n,sum);
getch();
}
Output:
Enter the number: 567
Sum of digits of 567 is 18
Sum of digits of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i;
printf("Enter the number: ");
scanf("%d",&n);
for(;n!=0;n=n/10)
{
i=n%10;
sum=sum+i;
}
printf("Sum of digits of %d is %d",n,sum);
getch();
}
Output:
Enter a number: 567
Sum of digits of number: 18
___________________________________________________________
Addition of digits of a number to a single digit
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum,d;
sum=0;
printf("Enter the number: ");
scanf("%d",n);
while((n>9) || (n==10))
{
while(n>0)
{
d=n%10;
n=n/10;
s=s+d;
}
n=sum;
}
printf("Sum of digits to a single number of a given number %d is %d",n,sum);
getch();
}
Output:
Enter the number: 567
Sum of digits to a single number of a given number 567 is 9
No comments:
Post a Comment