Custom Search

Sunday, 15 January 2012

WAP, Write a C or C++ Program to read a four digit integer & print sum of the digit with output

WAP, Write a C or C++ Program to read a four digit integer & print sum of the digit with output


Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum=0;
clrscr();
printf("Enter the number");
scanf ("%d", &a);
while(a>0)
(
b=a%10;
a=a%10;
sum=sum + b;
}
printf("sum is: %d", sum);
getch();
}


Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.


Click here to check the output

No comments:

Post a Comment

Laptops