if a five digit number is input through the keyboard, write
a program to calculate the sum of its digits.
(hint:-use the modulus operator.'%')

Answer Posted / haris

Try it. It will seriously work. You would appreciate it.

#include<stdio.h>
#include<conio.h>

int main()

{
int x;
int a;
int b;
int c;
int d;
int e;
int sum;

printf("Please enter a five digit number:\n");
scanf("%d",&x);

a=x%10;
b=(x/10)% 10;
c=(x/100)% 10;
d=(x/1000)%10;
e=(x/10000)%10;

sum=a+b+c+d+e;

printf("\n\nThe sum of all digits is %d.",sum);

getch();
return 0;

}

Is This Answer Correct ?    12 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

I have seen function declarations that look like this

610


What does 3 mean in texting?

625


Which one to choose from 'initialization lists' or 'assignment', for the use in the constructor?

579


application areas a 'c' a) operating system b) graphics, interpreter, assembler c) program evalution, communication softwares d) all the above

620


The statement, int(*x[]) () what does in indicate?

661






How was c created?

601


Explain what are header files and explain what are its uses in c programming?

635


When we use void main and int main?

597


How are portions of a program disabled in demo versions?

765


I need testPalindrome and removeSpace #include #define SIZE 256 /* function prototype */ /* test if the chars in the range of [left, right] of array is a palindrome */ int testPalindrome( char array[], int left, int right ); /* remove the space in the src array and copy it over to the "copy" array */ /* set the number of chars in the "copy" array to the location that cnt points t */ void removeSpace(char src[], char copy[], int *cnt); int main( void ) { char c; /* temporarily holds keyboard input */ char string[ SIZE ]; /* original string */ char copy[ SIZE ]; /* copy of string without spaces */ int count = 0; /* length of string */ int copyCount; /* length of copy */ printf( "Enter a sentence:\n" ); /* get sentence to test from user */ while ( ( c = getchar() ) != '\n' && count < SIZE ) { string[ count++ ] = c; } /* end while */ string[ count ] = '\0'; /* terminate string */ /* make a copy of string without spaces */ removeSpace(string, copy, ©Count); /* print whether or not the sentence is a palindrome */ if ( testPalindrome( copy, 0, copyCount - 1 ) ) { printf( "\"%s\" is a palindrome\n", string ); } /* end if */ else { printf( "\"%s\" is not a palindrome\n", string ); } /* end else */ return 0; /* indicate successful termination */ } /* end main */ void removeSpace(char src[], char copy[], int *cnt) { } int testPalindrome( char array[], int left, int right ) { }

2222


I came across some code that puts a (void) cast before each call to printf. Why?

689


Can the curly brackets { } be used to enclose a single line of code?

721


What is maximum size of array in c?

591


Explain how can I right-justify a string?

639


Devise a program that inputs a 3 digit number n and finds out whether the number is prime or not. Find out its factors.

676