C Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr(); int n,sum=0; printf("Enter a number"); scanf("%d",&n); while(n>0) { sum=sum+n%10; //adding last digit n=n/10; //removing last digit } printf("Sum of digits of the number = %d",sum); getch(); }
C++ Program
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n,sum=0;
cout<<"Enter a number";
cin>>n;
while(n>0)
{
sum=sum+n%10; //adding last digit
n=n/10; //removing last digit
}
cout<<"Sum of digits of the number ="<<sum;
getch();
return 0;
}
No comments :
Post a Comment