Program to find the sum of digits of a given number until
the sum becomes a single digit
Answers were Sorted based on User's Feedback
int n = 123456789; //any numer of you want sum
int sum = 0;
while (n > 0)
{
int p = n % 10;
sum = sum + p;
n = n / 10;
if(n==0 && sum>9)
{
n=sum;
sum=0;
}
}
printf("%d",sum);
Is This Answer Correct ? | 157 Yes | 63 No |
Answer / suvabrata das
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c=0,r,i;
clrscr();
printf("enter no.");
scanf("%d",&n);
while(n>0)
{
{
r=n%10;
c=c+r;
n=n/10;
}
if(c>9)
{
n=c;
c=0;
}
}
printf("%d",c);
getch();
}
Is This Answer Correct ? | 58 Yes | 34 No |
Answer / t. ashok kumar
//Sum of digits of a number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,r,m;
int s=0;
cout<<"Sum of digits of a number:-\n";
cout<<"Enter the number: ";
cin>>n;
m=n;
repeat:
while (n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
if(s>9)
{
n=s;
s=0;
goto repeat;
}
cout<<"Sum of digits of the number "<<m<<" is "<<s;
getch();
}
Is This Answer Correct ? | 14 Yes | 6 No |
Answer / somasundaram
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int no;
clrscr();
printf("Enter number : ");
scanf("%d", &no);
if(no==0)
printf("sum = 0");
else
{
no=no%9;
if(no==0)
printf("sum = 9");
else
printf("sum = %d", no);
}
getch();
}
Is This Answer Correct ? | 9 Yes | 5 No |
Answer / sanjay m
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int n,c=0,r,i,S=0;
clrscr();
printf("enter no.");
scanf("%d",&n);
while(n>0)
{
{
r=n%10;
c=c+r;
n=n/10;
}
if(c>9)
{
int X=c%10;
int Y=c/10;
S=X+Y;
}
}
if(c<10)
printf("%d",c);
else
printf("%d",S);
getch();
}
Is This Answer Correct ? | 13 Yes | 14 No |
Answer / sohini khan
main()
{int n,p,s=0;
while(n>0)
{
p=n%10;
s=s+p;
n=n/10;
}
printf("%d",s);
getch(),
}
Is This Answer Correct ? | 29 Yes | 43 No |
Answer / rama krishna sidhartha
int n = 1234; //any numer of you want sum
int sum = 0;
void main()
{
clrscr();
while (n > 0)
{
int p = n % 10;
sum = sum + p;
n = n / 10;
}
printf("%d",sum);
getch();
}
Is This Answer Correct ? | 49 Yes | 75 No |
Why is extern used in c?
What is the difference between void main() and void main (void) give example programme?
declare afunction pointer to int printf(char *)?
wat s the meaning of (int *)p +4;
Why n++ execute faster than n+1 ?
What is pointers in c with example?
Explain what is the difference between far and near ?
How can you increase the size of a dynamically allocated array?
What does typeof return in c?
how to print value of e(exp1)up to required no of digits after decimal?
main() { printf("\n %d %d %d",sizeof('3'),sizeof("3"),sizeof(3)); }
what is the difference between <stdio.h> and "stdio.h"
14 Answers Invendis, Kanbay, Mastek, MathWorks,