just starting learn how to program in loop, my professor give us those homework to require use of basic “while ” and “for” to write the program. please help Thank you very much. IN C++ use of “printf ….. ” ,”Scanf_s ….”. please
Write a program that achieves the following requirements 1. A while/do-While loop that will ask the user to input an integer on each iteration. The loop keeps a running total of the input and exits only when the total exceeds the first input times 10. 2. A for loop that does the exact same thing The program should print out the “new total each time the total is input.
Expert Answer
//First Program using While Loop
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); // clear the screen
int first, input, total;
cout<<“Enter the first input”;
cin>>first; // takes the first input
total=first;
cout<<“Total is :” <<total;
while(total<(first*10))
{
cout<<“Enter the input”;
cin>>input; //takes input
total=total+input; //calculates new total
cout<<“New Total is : “<<total;
}
getch(); // holds output screen until user press a key
}
// Second Program using For Loop
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); // clear the screen
int first, input, total;
cout<<“Enter the first input”;
cin>>first; // takes the first input
cout<<“Total is :” <<first;
for(total=first ;total<(first*10); )
{
cout<<“Enter the input”;
cin>>input; //takes input
total=total+input; //calculates new total
cout<<“New Total is : “<<total;
}
getch(); // holds output screen until user press a key
}
//First Program using While Loop using printf and scanf
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); // clear the screen
int first, input, total;
printf(“Enter the first input”);
scanf(“%d”,&first); // takes the first input
total=first;
printf(“Total is : %d”,first);
while(total<(first*10))
{
printf(“Enter the input”);
scanf(“%d”,&input); //takes input
total=total+input; //calculates new total
printf(“New Total is : %d”,total);
}
getch(); // holds output screen until user press a key
}
// Second Program using For Loop using Printf and Scanf
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr(); // clear the screen
int first, input, total;
printf(“Enter the first input”);
scanf(“%d”,&first); // takes the first input
printf(“Total is : %d”,first);
for(total=first ;total<(first*10); )
{
printf(“Enter the input”);
scanf(“%d”,&input); //takes input
total=total+input; //calculates new total
printf(“New Total is : %d”,total);
}
getch(); // holds output screen until user press a key
}