Right Place For Right PersonTM 
Sponsored Ads
Home Tutorials Articles Forums Source Code Books Certifications Interviews Questions
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Learn C with example

Input a number and sum its digits

In this example we are going to find digits of a number and then add these digits to find the sum of digits.

In this we are including two header library files and five predefined  functions. In this example we first take number then find there digits and then add these digits to find sum in while loop.

To find digits we just find the modulo of 10 .This will gives last digit .Then add it into another variable(r). To find next digit we need to cut last digit so divide by 10 and store it into a varaible(i). Repeat while loop until  i in not equals to zero.

Then finally print the sum(r).

/* input a number and sum its digit */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,p,r=0;
clrscr();
printf("\nEnter a number to sum its digit :\t");
scanf("%d",&i);
while(i!=0)
{
p=i%10;
r=r+p;
i=i/10;
}
printf("The sum of digit\t:%d",r);
getch();
}

Download Source Code

Output Of Example

Enter a no to which you have to sum digit: 1234
The sum of digit :10
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

C Aptitude Questions

C Interview Questions And Answers

C Objective Interview Questions And Answers(10)

C Subjective Interview Questions And Answers(100)     

C syntax, semantics and simple programming questions(61)    

C Interview Questions And Answers( Objective and Subjective)

Interview Questions And Answers
Struts interview questions and answers (Subjective)
500 Java Objective Questions and Answer
Core Java Objective Questions And Answers
Core Java Subjective Questions And Answers
Core Java Interview Questions And Answers
Core Java Interview Questions and Answers (Subjective)
Core Java Interview Questions and Answers( Objective)
50 Servlet interview questions
155 Java Interview Questions
EJB Interview Questions and Answers(Subjective)
R4R,JSP Interview Questions and Answer(Subjective)
R4R,Java Servlets Interview Questions and Answers(Subjective)
Core Java Subjective ,Objective and Interview Questions And Answers
275 Core java interview questions
Java Objective Questions and Answer
Java Architect Interview Questions
Applet Interview Questions and Answers
Core Java example
Servlet Objective Questions And Answers
Servlet Subjective Questions And Answers
Servlet Interview Questions And Answers
JSP Objective Questions And Answers
JSP Subjective Questions And Answers
JSP Interview Questions And Answers

Contact US:

Your Name:


Your Email:

Message:

Comments:

Give Your Comments:


Advertiser PRIVACY POLICY ||User PRIVACY POLICY || R4R Group Srvices