Question

In: Computer Science

Write a series of codes using WHILE loops C++ 1. Ask the user for a number...

Write a series of codes using WHILE loops C++

1. Ask the user for a number and adds even numbers for 1 to the user entered number.

2. Write another piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers.

3. Write another piece of code that asks user for a file name and then add up all the numbers for the file.

Solutions

Expert Solution

First Code:

#include<bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cout<<"Enter the no: \n";

    cin>>t;

   int j=1;int sum=0;

    while(j<=t)

    {  

        if(j%2==0)

            sum++;

            j++;

   }   

    cout<<sum<<"\n";    

    return 0;

}

Second Code :

#include<bits/stdc++.h>

using namespace std;

int main()

{

    int x,y;

    cout<<"Enter the first number: ";

    cin>>x;

    cout<<"Enter the second number: ";

    cin>>y;

   int sum=0;

    while(x<=y)

    {  

            sum=sum+x;

            x++;

    }   

    cout<<sum<<"\n";    

    return 0;

}

Third Code:

#include <fstream>

#include <iostream>

#include <sstream>

using namespace std;

int main()

{

   ifstream fin;

    char filename[20];

cout<<"Enter The File Name With Extension\n";

cin>>filename;

fin.open(filename);

if (!fin)

   {

    cout<< "File  not found." << endl;

    return -1;

   }

   string line;

    getline(fin, line);

    istringstream str(line);

    int sum = 0, next = 0;

    while (str >> next)

sum += next;

   fin.close();

   cout<<sum<<"\n";

  return 0;

}


Related Solutions

C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
C++ Code While Loops. Ask user for file and open file. Do priming read and make...
C++ Code While Loops. Ask user for file and open file. Do priming read and make a while loop that: 1. Reads in numbers. 2. Counts how many there is. 3. Also for every 10 numbers in the file, print out the average of those 10 numbers. Ex: (If 20 numbers in the file. "With 10 numbers the average is .... and With 20 numbers the average is" and EX for a file with 5 numbers "There are 5 numbers...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
Write a while loop to ask the user to type number of hours(double) they work per...
Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than 0 and less than 5, then:  salary = numberofhours * 12, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 12, then: salary = numberofours * 14, loop continues, the user can type another number If...
C++ . It should all be in one code and using WHILE loops 2. Write a...
C++ . It should all be in one code and using WHILE loops 2. Write a piece of code that asks the user for a number and adds up the even numbers from 1 to that entered number. 3. Write a piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers. 4. Write another piece of code that asks the user for a random file name and adds all of...
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming read and make a while loop that A) Reads in numbers B) Sums them up. C) Counts how many there are. D) Prints "With 10 numbers, the average is blank.." every 10 numbers. So with every 10 numbers read in print the average. 3. After the loop calculate average of all numbers and print it. So for example once the file is open the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT