Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label sum of digit. Show all posts
Showing posts with label sum of digit. Show all posts

Wednesday, December 17, 2014

Program to find sum of digits of any number given by user using while loop in C.


Sum of digits


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

/*Program to find sum of digits of any number given by user max. 5 digits */

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

void main()
{
int num,num1,rem,sum,i;
clrscr();

sum=0;
printf("\n\tSum of Digits Finder");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number

while(num1>=1)
{
rem=num1%10;
num1=num1/10;
sum=sum+rem;
}

printf("\nSum of all digits = %d",sum);

getch();

}


Program to find sum of digits of any number given by user using for loop in C.


Sum of all digits


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

/*Program to find sum of digits of any number given by user max. 5 digits*/

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

void main()
{
int num,num1,rem,sum,i;
clrscr();

sum=0;
printf("\n\tSum of Digits Finder");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number
for(i=1;num1>=1;i++)
{
rem=num1%10;
num1=num1/10;
sum=sum+rem;
}

printf("\nSum of all digits = %d",sum);

getch();

}


Program to find sum of digits of any number given by user using do while loop in C.


Sum of all digits


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

/*Program to find sum of digits of any number given by user max. 5 digit number */

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

void main()
{
int num,num1,rem,sum;
clrscr();

sum=0;
printf("\n\tSum of Digits Finder");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number

do
{
rem=num1%10;
num1=num1/10;
sum=sum+rem;
}while(num1>=1);


printf("\nSum of all digits = %d",sum);

getch();

}



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();
}