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

  • Thread starter Thread starter DiamondV
  • Start date Start date
  • Tags Tags
    Program Sum
Click For Summary
SUMMARY

The discussion centers on a C program that sums digit characters input from the keyboard and exits upon receiving 'Q' or 'q'. The code correctly implements a loop that checks for valid digit input and accumulates the total. The confusion arises from the logical operators used in the while loop condition; the correct implementation uses '&&' to ensure the loop continues until either 'Q' or 'q' is entered. This is based on de Morgan's laws, which clarify the logical equivalence between the conditions.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Knowledge of ASCII values and character manipulation
  • Familiarity with logical operators in programming
  • Basic concepts of loops and conditional statements in C
NEXT STEPS
  • Study C programming control structures, focusing on loops and conditionals
  • Learn about de Morgan's laws in logic and their application in programming
  • Explore character encoding and ASCII in depth
  • Practice error handling and input validation in C programs
USEFUL FOR

C programmers, computer science students, and anyone interested in improving their understanding of logical conditions and input handling in C programming.

DiamondV
Messages
103
Reaction score
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
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:
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').
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K