Sum Digits & Exit with 'Q': A Program Solution

If either of those conditions is not met, then the loop should continue.Another way to think about it is that if input is not 'q' AND it is not 'Q', then it must be some other character that you want to process. If it was 'q' OR 'Q', then your program should exit.
  • #1
DiamondV
103
0

Homework Statement


Write a program that reads in any characters from the keyboard, sums up only characters corresponding to a digit and prints the result on the screen. The program will exit when the character 'Q' is entered (upper or lowercase).

Example:
Input = 9 8 q
Output = The total is 17.

Homework Equations

The Attempt at a Solution


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){
char input;
int total = 0;
scanf("%c", &input);
while(input!='q' && input!='Q'){
if(input>'9' || input<'0'){ //Input is not a valid integer
printf("Input is not a digit\n");
}
else{//Input is a valid integer
//ASCII value for 0 is 48 .
total = total + (input-48); // Takes previous total and adds new valid integer onto it
}
scanf(" %c", &input);//Scans for new Input
}
printf("The total is %d\n", total);



return 0;
}This is my code that works perfectly, I don't understand one thing though. At the line that is bold I have said that while the input is not q AND not Q. Initially I used a || there, so it was while the input is not Q or q, that didnt work. Its not working as an OR(||) but its working as AND(&&). Why is that? Surely the input can't be Q and q at the same time? I mean only 1 would need to appear to stop the code, so why is it && and not ||.
 
Physics news on Phys.org
  • #2
If input is not 'Q' AND it's not 'q' then it must be something you want to process further.

If you used OR then you might as well have no test because your Boolean will always be TRUE. Every character you nominate will test true on one side or the other in each "not something" condition, meaning both expressions can never be simultaneously false.
 
Last edited:
  • #3
Your logic is such that
Code:
if (input == 'q' || input == 'Q)
your program should exit. In your loop, if the logical expression above is false, you want the program to proceed with another iteration of the loop.
In other words, if the expression ~(input == 'q' || input == 'Q') is false. Due to one of de Morgan's laws, ~ (P || Q) is equivalent to (~P && ~Q), where P and Q represent the logical expressions that make up your test. In your case, ~(input == 'q' || input == 'Q') is equivalent to (input != 'q' && input != 'Q').
 

1. What is the purpose of the "Sum Digits & Exit with 'Q': A Program Solution" program?

The purpose of this program is to take in a series of numbers from the user and continuously sum the digits until the user enters the letter 'Q' to exit the program.

2. How does the program determine the sum of the digits?

The program uses a loop to continuously add the digits of each number entered by the user. For example, if the user enters the number 123, the program will add 1+2+3 to get a sum of 6.

3. What happens if the user enters a non-numeric character?

If the user enters a non-numeric character (other than 'Q'), the program will display an error message and ask the user to enter a valid number.

4. How does the program handle negative numbers?

The program can handle negative numbers by treating the negative sign as a separate character and continuing to sum the digits of the number. For example, if the user enters -123, the program will add 1+2+3 to get a sum of 6.

5. Can the program handle decimal numbers?

No, the program is designed to only work with whole numbers (integers). If the user enters a decimal number, the program will only sum the digits before the decimal point and ignore any digits after the decimal point.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
929
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Back
Top