3. a) Write a C program to find the sum of individual digits of a positive integer
Algorithm
Step-1 : Start
Step-2 : Declare variables sum, number
Step-3 : Initialize sum=0 and read the number
Step-4 : perform sum of individual digits
4.1 : compute sum=sum+number%10
4.2 : compute number=number/10
4.3 : if(number!=0)
4.3.1: Go to step 4.1
Step-5 : Display sum
Step-6 : Stop
Flowchart
Program
#include<stdio.h>
void main()
{
int num, k=1, sum=0;
printf("Enter the number whose digits are to be added:");
scanf("%d",&num);
while(num!=0)
{
k=num%10;
sum=sum+k;
k=num/10;
num=k;
}
printf("Sum of the digits:%d \n",sum);
}
Algorithm
Step-1 : Start
Step-2 : Declare variables sum, number
Step-3 : Initialize sum=0 and read the number
Step-4 : perform sum of individual digits
4.1 : compute sum=sum+number%10
4.2 : compute number=number/10
4.3 : if(number!=0)
4.3.1: Go to step 4.1
Step-5 : Display sum
Step-6 : Stop
Flowchart
#include<stdio.h>
void main()
{
int num, k=1, sum=0;
printf("Enter the number whose digits are to be added:");
scanf("%d",&num);
while(num!=0)
{
k=num%10;
sum=sum+k;
k=num/10;
num=k;
}
printf("Sum of the digits:%d \n",sum);
}

Comments
Post a Comment