C C++ program to find sum of digits of given number

 

Flowchart

Sum of digits of given number flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int num,rem,sum=0;
printf(“Enter a number: “);
scanf(“%d”,&num);
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf(“Sum of digits = %d”,sum);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int num,rem,sum=0;
cout<<“Enter a number: “;
cin>>num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
cout<<“Sum of digits = “<<sum;
return 0;
}

 

Output

Sum of digits of given number output

 

Published by

admin

I am a computer teacher, programmer and web developer