C Program to find the sum of digits and reverse of a number with Algorithm
11-07-2020
This is the C program code and algorithm for finding the sum of digits and reverse of a number.
Aim:
Write a C program to find the sum of digits and the reverse of a number.
Algorithm:
Step 1: Start Step 2: Read number num Step 3: Set sum=0 and rev=0 Step 4: Repeat step 5 to 8 while num Step 5: Set d=num mod 10 Step 6: Set num=num/10 Step 7: Set sum=sum+d Step 8: Set rev=rev*10+d Step 9: Print sum Step 10: Print rev Step 11: Stop
Program code
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( )
int num,sum=0,rev=0,d;
printf("Enter the number: ");
scanf("%d",&n);
while(num){
d=num%10;
num=num/10;
sum=sum+d;
rev=rev*10+d;
}
printf("Sumof digits = %d",sum);
printf("\nReverse of the number = %d",rev);
getch( );
}
Output
Enter the number : 123 Sum of digits = 6 Reverse of the number = 321