Monday, May 24, 2010

Getting input and add the value in C++?

I want a program that get numeric inputs from user. User inputs a single value like 123 but the program treat it and add the value like 1+2+3=6.


I'm not a programmer, its just a subject in my class so please help me in this.

Getting input and add the value in C++?
Just keep trying if you want to learn. Well answer to your this question is:-


#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


clrscr();


int n,r,sum;


cout%26lt;%26lt;"Enter the number:";


cin%26gt;%26gt;n;


while(n%26gt;0)


{


r=n%10;


sum=r+sum;


n=n/10;


}


cout%26lt;%26lt;"Result:"%26lt;%26lt;sum;


}
Reply:I'm sorry but i won't help you on this issue. If you are taking a class that means you are learning some programming. When i took my 2 year computer program it included some C++, Visual Basic, and other languages. I never asked someone else to write the program just cause i wasn't a programmer. If i had done that what would I have learnt? and the same goes for you, you are in a course that has some programming so what will you learn if someone else does all the work for you?





The best thing is to ask the instructor for some help.
Reply:This is the logic of this programme. I am also a student %26amp; giving you a code of TURBO C++.








int number,sum=0,rem=1;


cout%26lt;%26lt;"Enter a number ";


cin%26gt;%26gt;number;


while(number%26gt;0)


{


rem=number%10;


sum=rem+sum;


number=number/10;


}


cout%26lt;%26lt;"Sum = "%26lt;%26lt;sum;


No comments:

Post a Comment