Sei sulla pagina 1di 1

a) Write a C program to find the sum of individual digits of a positive

integer.
Objective 1.1:
To find the sum of individual digits of a given positive number.
Description :
Sum of the individual digits means adding all the digits of a number.
e.g. If a given number is 123 then sum of digits is 1+2+3=6
Pseudo Code:
declare variables called sum, LSD, number
set sum to 0
read a number from user
if the number is not a positive number then exit
loop (number != 0)
LSD = number % 10
sum = sum + LSD
number = number / 10
end loop
print sum
Source Code:
/* To find the sum of individual digits of a given positive number.*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
int num,sum=0,new_num,temp;
clrscr();
printf("Enter the positive number........ \n");
scanf("%d",&temp);
if ( temp>0 )
num=temp;
else
exit (1);
while(temp!=0)
{
new_num=temp%10;
sum=sum+new_num;
temp=temp/10;
}
printf(" \n sum of individual digits of %d is...%d ",num,sum);
getch();
}

Potrebbero piacerti anche