Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, November 8, 2014

Program to take 5 digit number as input and calculate the sum of its digits in C.


sum of digit


Code Box
---------------------------------------------

/*Program to take 5 digit number as
input and calculate the sum of its digits */

#include<stdio.h>
#include<conio.h>
void main()
{

int num,num1,i,sum;
sum=0;
clrscr();

printf("\nEnter a 5 digit number:");
scanf("%d",&num);

num1=num;

for(i=1;i<=5;i++)
{
sum=sum+num1%10;
num1=num1/10;
}
printf("\nSum= %d",sum);

getch();
}


---------------------------------------------


sum of digit


Code Box 2
--------------------------------------------

/*Program to take 5 digit number as
input and calculate the sum of its digits */

#include<stdio.h>
#include<conio.h>
void main()
{

int num,num1,i,j,rem,sum;
sum=0;
clrscr();

printf("\nEnter a 5 digit number:");
scanf("%d",&num);

num1=num;

printf("\nDigits in number:");
for(i=1;i<=5;i++)
{
rem=num1%10;
if(i>1)
{
printf("+");
}
printf("%d",rem);
sum=sum+rem;
num1=num1/10;
}
printf("\nSum= %d",sum);

getch();
}



No comments: