write a program to sum of its digit with using control
structure or with out using loop. for ex: let the number is
25634 then answer will be=2+5+6+3+4=20

Answers were Sorted based on User's Feedback



write a program to sum of its digit with using control structure or with out using loop. for ex: l..

Answer / alex r.

// without loop
// for digit in DEC
#include <stdio.h>
int sum(int digit)
{
if (digit%10 != digit)
return digit%10 + sum(digit/10);
return digit;
}
int main(void)
{
int digit = 25634;
printf("\nSum:%d", sum(digit));
return 0;
}

Is This Answer Correct ?    15 Yes 7 No

write a program to sum of its digit with using control structure or with out using loop. for ex: l..

Answer / lalabs

// assume that num is non-negative.

int sum_digits_recursive ( int num )
{
if ( num == 0 ) return 0;
return (num % 10 + sum_digits_recursive ( num / 10 ));
}

Is This Answer Correct ?    6 Yes 1 No

write a program to sum of its digit with using control structure or with out using loop. for ex: l..

Answer / rama krishna sidhartha

#include <stdio.h>
#include<conio.h>
void main()
{
int s=0,c,n;
clrscr();
printf("\n ENTER A VALUE : ");
scanf("%d",&n);
while(n>0)
{
c=n%10;
s=s+c;
n=n/10;
}
printf("\n THE SUM OF INDIVIDUAL DIGITS : %d",s);
getch();
}

Is This Answer Correct ?    10 Yes 10 No

write a program to sum of its digit with using control structure or with out using loop. for ex: l..

Answer / thiruapthi rao

// mainlogic
while(number>0)
{
remainder=number%10;
sum=sum+remainder;
number=number/10;
}
/*
number=123
sum=1+2+3=6
*/

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Interview Questions

Where register variables are stored in c?

0 Answers  


Where is volatile variable stored?

0 Answers  


How can I find out if there are characters available for reading?

0 Answers  


What is the use of sizeof?

0 Answers  


Should I use symbolic names like true and false for boolean constants, or plain 1 and 0?

0 Answers  






Find greatest of two numbers using macro

4 Answers   Bosch, Siemens,


What is union and structure?

0 Answers  


Why is it that not all header files are declared in every C program?

0 Answers  


ratio,age,persentage

0 Answers  


Where define directive used?

0 Answers  


a=0; b=(a=0)?2:3; a) What will be the value of b? why b) If in 1st stmt a=0 is replaced by -1, b=? c) If in second stmt a=0 is replaced by -1, b=?

6 Answers   Geometric Software,


How do you list files in a directory?

0 Answers  


Categories