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
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 |
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 |
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 |
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 |
What are the advantages and disadvantages of pointers?
write a program to insert an element at the specified position in the given array in c language
A global variable when referred to in another file is declared as this a) local variable b) external variable c) constant d) pointers
main() { int a[3][4] ={1,2,3,4,5,6,7,8,9,10,11,12} ; int i, j , k=99 ; for(i=0;i<3;i++) for(j=0;j<4;j++) if(a[i][j] < k) k = a[i][j]; printf("%d", k); }
4 Answers Vector, Wipro, Zoho,
What is the meaning of typedef struct in c?
10. Study the code: void show() main() { show(); } void show (char *s) { printf("%sn",s); } What will happen if it is compiled & run on an ANSI C Compiler? A)It will compile & nothing will be printed when it is executed B)it will compile but not link C)the compiler will generate an error D)the compiler will generate a warning
Can we compile a program without main() function?
what is the most appropriate way to write a multi-statement macro?
Write a program to write a given string in maximum possibilities? i.e str[5]="reddy"; i.e we can write this string in 120 ways for that write a program
Where are local variables stored in c?
difference between string and array?
why we wont use '&' sing in aceesing the string using scanf