Question

In: Computer Science

Use while loop for the num inputs #include #include using namespace std; int main() {   ...

Use while loop for the num inputs

#include
#include
using namespace std;

int main()
{
   long long int digit;
   long long int num1, num2, num3, num4, num5;
   int ave;
  
   cout << "Enter a 10 digit number: ";
   cin >> digit;
  
   num5 = digit %100;
   digit = digit / 100;
  
   num4 = digit %100;
   digit = digit / 100;

   num3 = digit %100;
   digit = digit / 100;

   num2 = digit %100;
   digit = digit / 100;

   num1 = digit %100;
   digit = digit / 100;
  
   cout << num1 << endl;
   cout << num2 << endl;
   cout << num3 << endl;
   cout << num4 << endl;
   cout << num5 << endl;
  
   ave = (double)(num1 + num2 + num3 + num4 + num5) / 5;
  
   cout << "Avg: " << ave << endl;
  
   switch(ave/10){
       case 9:
           cout << "Grade: A";
           break;
       case 8:
           cout << "Grade: B";
           break;
       case 7:
           cout << "Grade: C";
           break;
       case 6:
           cout << "Grade: D";
           break;
       default:
           cout << "Grade: F";
           break;
   }
  
   return 0;
}

Solutions

Expert Solution

Here is the completed code for this problem. After reading the question, there was a confusion whether you meant to rewrite the same program using while loops than using 5 modulo and division operations, or rewrite the program using while loops for reading 5 grades and find the average and grade at the end. So I wrote both versions of this question according to my understanding. Choose whichever you want. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//version 1

#include <iostream>

using namespace std;

int main()

{

    long long int digit;

    int num, i = 0;

    int ave;

    //asking and reading a 10 digit number, assuming user always enter valid input

    cout << "Enter a 10 digit number: ";

    cin >> digit;

    //looping for 5 times

    while (i < 5) {

        //extracting last 2 digits as num

        num = digit % 100;

        //adding to ave

        ave += num;

        //removing last 2 digits from digits

        digit = digit / 100;

        //updating i

        i++;

        //displaying current number

        cout << num << endl;

    }

    //finding average

    ave = ave / 5;

    //displaying average and grade

    cout << "Avg: " << ave << endl;

    switch (ave / 10) {

    case 9:

       cout << "Grade: A";

        break;

    case 8:

        cout << "Grade: B";

        break;

    case 7:

        cout << "Grade: C";

        break;

    case 6:

        cout << "Grade: D";

        break;

    default:

        cout << "Grade: F";

        break;

    }

    return 0;

}

/*OUTPUT*/

Enter a 10 digit number: 8080989075

75

90

98

80

80

Avg: 84

Grade: B

//version 2

#include <iostream>

using namespace std;

int main()

{

    long long int digit;

    int num, i = 1;

    int ave;

    //looping for 5 times

    while (i <= 5) {

                //asking for num

                cout<<"Enter num "<<i<<": ";

                //reading num

        cin>>num;

        //adding to ave

        ave += num;      

        i++;

    }

    //finding average

    ave = ave / 5;

    //displaying average and grade

    cout << "Avg: " << ave << endl;

    switch (ave / 10) {

    case 9:

        cout << "Grade: A";

        break;

    case 8:

        cout << "Grade: B";

        break;

    case 7:

        cout << "Grade: C";

        break;

    case 6:

        cout << "Grade: D";

        break;

    default:

        cout << "Grade: F";

        break;

    }

    return 0;

}

//OUTPUT

Enter num 1: 70

Enter num 2: 80

Enter num 3: 90

Enter num 4: 95

Enter num 5: 44

Avg: 76

Grade: C


Related Solutions

in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i<NUM; i++) cout << arr[i] <<" "; cout <<endl; return 0; } void prepareArr(int a[]) { //randomly generate NUM integers in the range [0,99] and save them in...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
Fill in the code only using pointer variables #include using namespace std; int main() { int...
Fill in the code only using pointer variables #include using namespace std; int main() { int longside; // holds longside (length) int wideside; // holds wideside(width) int total; // holds total (area) int *longsidePointer = nullpointer; // int pointer which will be set to point to length int *widthPointer = nullpointer; // int pointer which will be set to point to width cout << "Please input the longside of the rectangle" << endl; cin >> longside; cout << "Please input...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
I need a Flowgorithm chart of this #include using namespace std; int main() {    float...
I need a Flowgorithm chart of this #include using namespace std; int main() {    float CISBook[] = {55.5,35.8,67.5};    float MATHBook[] = {25.5, 54.5};    float MECBook[] = {65.0,75.5,86.8};    float sumCIS=0, sumMATH = 0, sumMEC = 0;              for (int i=0; i<4; i++)    {sumCIS += CISBook[i];}       for (int i=0; i<3; i++)    {sumMATH += MATHBook[i];}       for (int i=0; i<4; i++)    {sumMEC += MECBook[i];}       cout<<"\nTotal for CIS Books:...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;    double parellel;    cout << "how many resistors: ";    cin >> number_resistors;    while (number_resistors != 0)    {        cout << "Enter the values of" << number_resistors << " resistors ";        for (int i = 0; i < number_resistors; i++) {            cin >> Resistors;            series += Resistors;                parellel...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT