#include int main() int x = 6, y = 10; printf( \"Enter two numbers (separated by
ID: 3728438 • Letter: #
Question
#include int main() int x = 6, y = 10; printf( "Enter two numbers (separated by spaces) : " int values-read = scanf("%d %d", &x;, &y;); if (values-read != 2){ printf("scanf was unable to read both values n"); printf( "The value of x is %d and the value of y is %d ", x,y); return 0; Exercise : Using your program from the previous section as a basis, write a program which reads floating point values (including the value "O", if it appears) from the user until an invalid input (such as "asdf" or "q") is encountered, then prints the sum of all values entered up until the invalid input and exits. For example, if the user enters the sequence 6 100 1.7 1.87 0 qit, the output should be "Sum: 19.57". The program should be able to read as many values as the user can provide.Explanation / Answer
#include <stdio.h>
#include <ctype.h>
int main(){
//Variable to hold sum of values.
float sum=0;
float f;
// Infinite loop to have as many inputs as user want.
// As soon as a string is encountered loop will break and sum would be printed.
while(1){
int a = scanf("%f",&f);
// If entered type is valid then scanf returns 1 else return 0.
if(a==1)
sum+=f;
else
break;
}
// Printing the sum.
printf("Sum: %.2f ",sum);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.