Make a while loop that asks for a person’s age and only accepts an age between 1 and 100.

Question

  1.  Make a while loop that asks for a person age and only accepts an age between 1 and 100.
  2.  Make loop that adds together all the number from 1 to 10.
  3.  Make a void function that asks for a user name and license plate and output the information to main using pass by reference.
  4. Make a function that takes in a number and output true if its odd.
  5.  Make a double function that takes in a price and quantity is between 0 and 10 there is 10% discount, so if quantity is between 11 and 20 there is a 15% discount, if quantity is greater than 20 there is a 20% discount. Return the price with the discount factored in.
  6.  Have the user choose a number between 1 and 100.  So Use a while loop to let the computer guess the random number. The user should tell the computer if the guess is HIGH of LOW and the computer should adjust accordingly, until it figures out the number.
  7.  Make a function that takes in number and outputs a box of stars of that size.
  8.  Make a function that takes in a number and outputs all the prime up until and including the number.
  9.  Make a function named ALICE that calls another name BOB. So Each function should say its named when it is called.
  10.  Make a function that takes in X and adds 2 to it using pass by reference.

Summary

Here we have given 10 questions for each question we will write a c++ code to answer them so that to create a function or to use the loops or calling the functions.

Explanation

1. In the first question we are asked to enter the age of the person between 1 and 100. So we declare the variable as the age to which the user will enter his age.

2. In the second question, we have to create a loop for the sum of numbers between 1 and 10. So We use a for loop for this sum.

3. In the third question we have to pass a function with the argument so we have the person’s name and the license plate number.

4. In the fourth question we have to use an if-else statement to check if the number which is passed to the function is even or odd.  So If the number is even it will return true or if the number is odd it will return false.

5. In the fifth question we have to pass the price and the quantity through which the discount will get printed.

6. In the sixth question user will enter the number but the computer will take it as a random number and then adjust it according to the user enter the number.

7. In the seventh question we have to create a box of stars for which the user will enter the number.

 

8. In the eighth question we will create a functioning prime to which we will pass a number from 1 so to that number a prime number will get printed.

 

9. In the ninth question in the main method we passed a function name ALICE in that function there is a Another function name BOB and in that bob function, there is a cout statement.

10. In the last question we passed a number to the function to make changes in it without returning it to the function.

Code

1

#include <iostream>
using namespace std;
int main()
{
    int Age;
    while(true){
        cout<<"Enter age: ";
        cin>>Age;
        if(Age>=1 && Age<=100)
        break;
        cout<<"Age should be between 1 and 100. Try again!"<<endl;
    }
    cout<<"Age is "<<Age<<endl;
}

 

2

#include <iostream>
using namespace std;
int main()
{
    int sum=0;
    for(int i=1;i<=10;i++)
    sum=sum+i;
    cout<<"Sum = "<<sum<<endl;
}

 

3

#include <iostream>
using namespace std;
void function(string &user, string &license)
{
    cout<<"Enter username: "; 
    cin>>user;
    cout<<"Enter license plate number: ";
    cin>>license;
}
int main()
{
    string userName, license;
    function(userName, license);
    cout<<"\nUsername: "<<userName<<endl;
    cout<<"License number: "<<license<<endl;
}

 

4

#include <iostream>
using namespace std;
bool function(int num)
{
    if(num%2==0)
    return true;
    else 
    return false;
}
int main()
{
    int num=10;
    if(function(num))
    cout<<"Even"<<endl;
    else 
    cout<<"Odd"<<endl;
}

 

5

#include <iostream>
using namespace std;
double function(double price,int quantity)
{
    int dis;
    if(quantity>=0 && quantity<=10)
    dis=10;
    if(quantity>=11 && quantity<=20)
    dis=15;
    if(quantity>20)
    dis=20;
    price=price-(price*(dis/100.0));
    return price;
}
int main()
{
    double price=20.0;
    int quantity=5;
    cout<<function(price,quantity)<<endl;
}

 

6

#include <iostream>
using namespace std;
int main()
{
    cout<<"Enter a number: ";
    int n;
    cin>>n;
    int p=rand()%100;
    if(p>n)
    {
        cout<<"Adjust it by "<<p-n<<endl;
        p=p-(p-n);
    }
    else{
    p=p+(n-p);
    cout<<"Adjust it by "<<n-p<<endl;
    }
    cout<<"The number is "<<p<<endl;
}

 

7

#include <iostream>
using namespace std;
void function(int n)
{
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
            cout<<"*";
        cout<<endl;
    }
}
int main()
{
    int n=4;
    function(4);
}

 

8

#include <iostream>
using namespace std;
#include <cmath>
bool prime(int n)
{
    if(n==2)
    return true;
    else 
    {
        for(int i=2;i<n;i++)
        if(n%i==0)
        return false;
        return true;
    }
}
int main()
{
    int n=10;
    for(int i=2;i<=10;i++)
    if(prime(i))
    cout<<i<<"  ";
}

 

9

#include <iostream>
using namespace std;
#include <cmath>
void BOB()
{
    cout<<"BOB"<<endl;
}
void ALICE()
{
    BOB();
    cout<<"ALICE"<<endl;
}
int main()
{
    ALICE();
}

 

10

#include <iostream>
using namespace std;
#include <cmath>
void function(int &n)
{
    n=n+2;
}
int main()
{
    int n=4;
    function(n);
    cout<<n<<endl;
}

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *