Write a console application to total a set of numbers indicated and provided by a user, using a while loop or a do…while loop according to the user’s menu choice as follows:
Example Menu 1. To total numbers using a while loop 2. To total numbers using a do...while loop Enter your menu choice: 1 How many numbers do you want to add: 2 Enter a value for number 1: 4 Enter a value for number 2: 5 The total is: 9 Example Menu 1. To total numbers using a while loop 2. To total numbers using a do...while loop. Enter your menu choice: 1 How many numbers do you want to add: 4 Enter a value for number 1: 3 Enter a value for number 2: 2 Enter a value for number 3: 5 Enter a value for number 4: 1 The total is: 11 |
Example Menu 1. To total numbers using a while loop 2. To total numbers using a do...while loop Enter your menu choice: 2 How many numbers do you want to add: 2 Enter a value for number 1: 4 Enter a value for number 2: 5 The total is: 9 Example Menu 1. To total numbers using a while loop 2. To total numbers using a do...while loop. Enter your menu choice: 2 How many numbers do you want to add: 4 Enter a value for number 1: 3 Enter a value for number 2: 2 Enter a value for number 3: 5 Enter a value for number 4: 1 The total is: 11 |
Requirements
// Sourcecode.c
#include<stdio.h>
int main(){
printf("Menu\n");
printf("1. To total numbers using a while loop\n");
printf("2. To total numbers using a do...while loop\n");
printf("Enter your menu choice: ");
int choice;
scanf("%d",&choice);
if(choice == 1){
int total = 0;
printf("How many numbers do you want to add: ");
int n;
scanf("%d",&n);
int a;
int i=1;
do{
printf("Enter a value for number %d: ",i);
scanf("%d",&a);
total += a;
i++;
}while(i<=n);
printf("The total is: %d",total);
}else if(choice == 2){
int total = 0;
printf("How many numbers do you want to add: ");
int n;
scanf("%d",&n);
int a;
int i=1;
while(i<=n){
printf("Enter a value for number %d: ",i);
scanf("%d",&a);
total += a;
i++;
}
printf("The total is: %d",total);
}
return 0;
}
// Output:
Get Answers For Free
Most questions answered within 1 hours.