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

What is the output for the program given below typedef enum grade{GOOD,BAD,WORST,}BAD; main() { BAD g1; g1=1; printf("%d",g1); }

4 Answers   ADITI,


related proverb of "dont count your chicken before it hatches"

1 Answers  


In which area global, external variables are stored?

3 Answers  


write a function for strtok()??

2 Answers   Verifone,


The program to allow the characters from the input received and send this function to a function check if the characters between letters a to z is a function of y joins as the characters main and output to otherwise return to the original function of the y characters

0 Answers  






How do I round numbers?

0 Answers  


differentiate built-in functions and user – defined functions.

0 Answers  


What is meant by recursion?

0 Answers   ADP,


Find MAXIMUM of three distinct integers using a single C statement

0 Answers  


why division operator not work in case of float constant?

2 Answers  


a linearly ordered set of data elements that have the same structure and whose order is preserved in storage by using sequential allocation a) circular b) ordinary c) array d) linear list

0 Answers  


What is define c?

0 Answers  


Categories