Write a program that will ask user to enter a digit character (i.e. ‘0’ or ‘1’ .... or ‘9’). If user enters a non-digit character then the program should display message to re-enter correct input. If user enters a correct character (i.e. a digit character) then your program should convert that character to a same digit but in integer type. Do this for five inputs. Finally, add all digits and display their sum. Do not use any library function or loops.
Example:
Enter a digit-character: 8 (please note that this is to be stored in char variable)
Enter a digit-character: 6
Enter a digit-character: 0
Enter a digit-character: +
Wrong character. Please enter again.
Enter a digit-character: 2
Enter a digit-character: 7
Sum of digits is 23.
using namespace std;
int main()
{
char c;
int Sum=0,n=0;
while(n<5)
{
c = 'a';
while(c<'0' || c >'9')
{
cout<<"\nEnter a number (#"<<n+1<<"): "; cin>>c;
}
Sum = Sum + (c-0x30);
n++;
}
cout<<"\n\tSum = "<<Sum;
return(0);
}
Comments
Leave a comment