C Program to Find Sum of Digits of a Number without loop:-

In this program, we will calculate the sum of the number inserted by the user or in an inserted integer. The program is taken as an input and stored in the variable number, denoted as no. Initially, the sum of the variable is zero, and then it is divided by 10 to obtain the result or output.
ALGORITHMS:-
Step 1: Start
Step 2: Ask the user to insert an integer as an input.
Step 3: Divide the integer by 10 in order to obtain the quotient and remainder.
Step 4: Increase the new variable with the remainder received in the above step.
Step 5: Repeat the above steps with the quotient till the value of the quotient becomes zero.
Step 6: Printf the output or sum.
Step 7: Stop.
CODE: -
#include<stdio.h>
int main(){ int n,d1,d2,d3,d4,d5,sum; printf("enter any num"); scanf("%d",&n); d1=n%10; n=n/10; d2=n%10; n=n/10; d3=n%10; n=n/10; d4=n%10; n=n/10; d5=n%10; sum= d1+d2+d3+d4+d5; printf("%d\n",sum); return 0;}
#include<stdio.h>
int main()
{
int n,d1,d2,d3,d4,d5,sum;
printf("enter any num");
scanf("%d",&n);
d1=n%10;
n=n/10;
d2=n%10;
n=n/10;
d3=n%10;
n=n/10;
d4=n%10;
n=n/10;
d5=n%10;
sum= d1+d2+d3+d4+d5;
printf("%d\n",sum);
return 0;
}

OUTPUT:-
FOLLOW THIS WEBPAGE FOR MORE NEW PROGRAMS AND CODING SOLUTIONS.
Comments
Post a Comment