Close
Close

Practice questions on Loops

Level 1

1.
Print multiplication table of 24, 50 and 29 using loop.
#include<iostream>
using namespace std;
int main()
{
    for(int i=1;i<=10;i++)
    {
        cout<<"24 * "<<i<<"\t=\t"<<24*i<<"\n";
    }
    return 0;
}

2.
Take 10 integers from keyboard using loop and print their average value on the screen.
#include<iostream>
using namespace std;
int main()
{
    int sum = 0;
    for(int i=0; i<10; i++)
    {
        int x;
        cout << "Enter a number\n";
        cin>>x;
        sum = sum+x;
    }
    cout << "The average value is "<<sum/10.0<<"\n";
    return 0;
}									

3.
Print the following patterns using loop :
a.
*
**
***
****
b.
   *  
 *** 
*****
 *** 
   *  
c.
1010101
 10101 
  101  
   1   
#include <iostream>
int main()
{
	using namespace std;
	int i,j;
	for (j=1;j<=4;j++){
        for (i=1;i<=j;i++){
            cout << "*";
        }
        cout << "\n";
	}
    return 0;
}									

4.
Print ASCII values and their equivalent characters. ASCII value vary from 0 to 255.
#include<iostream>
using namespace std;
int main()
{
    char c;
    for(int i=0;i<=255;i++)
    {
        c = i;
        cout<<c<<"\n";
    }
    return 0;
}

5.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0
Write a C++ program to calculate factorial of a number.
#include <iostream>
int main()
{
	using namespace std;
	int j,number;
	cout << "Enter number" << "\n";
	cin >> number;
	int fact = 1;
	for (j=1;j<=number;j++){
 		fact = fact*j;
	}
	cout << fact << "\n";
	return 0;
}									

6.
Write a program to find greatest common divisor (GCD) or highest common factor (HCF) of given two numbers.
#include<iostream>
using namespace std;
int main()
{
    int x,y,gcd,lcm,t,b,a;
    cout<<"Enter two integers\n";
    cin>>x;
    cin>>y;
    a = x;
    b = y;
    while(b!=0)
    {
        t = b;
        b = a%b;
        a = t;
    }
    gcd = a;
    lcm = (x*y)/gcd;
    cout << "GCD is "<<gcd<<" and LCM is "<<lcm<<"\n";
    return 0;
}

7.
Take integer inputs from user until he/she presses q ( Ask to press q to quit after every integer input ). Print average and product of all numbers.

8.
Write an infinite loop.
A inifinte loop never ends. Condition is always true.

Level 2

1.
Take as input a fraction in the form a/b. Convert the same into lowest terms and print. (Lowest terms examples 3/12 = 1/4).

2.
Calculate the sum of digits of a number given by user. E.g.-
INUPT : 123        OUPUT : 6
INUPT : 12345        OUPUT : 15

3.
A three digit number is called Armstrong number if sum of cube of its digit is equal to number itself.
E.g.- 153 is an Armstrong number because (13)+(53)+(33) = 153.
Write all Armstrong numbers between 100 to 500.
#include<iostream>
using namespace std;
int main()
{
    for(int i=0;i<500;i++)
    {
        int sum = 0;
        int t = i;
        while(t!=0)
        {
            sum = sum+((t%10)*(t%10)*(t%10));
            t = t/10;
        }
        if(sum == i)
        {
            cout << i <<"\n";
        }
    }
    return 0;
}

4.
Write a program to print all prime number in between 1 to 100.

5.
Write a program to find prime factor of a number.
If a factor of a number is prime number then it is its prime factor.
#include<iostream>
using namespace std;
int main()
{
    int x;
    cout<<"Enter the number\n";
    cin>>x;

    cout<<"Prime factors of "<<x<<" are:\n";

    for(int i=2;i<=x;i++)
    {
        //checking for factor
        if(x%i==0)
        {
            //checking if i is prime or not
            int p = 0;
            for(int j=2;j<i;j++)
            {
              if(i%j==0)
              {
                //i is not prime
                p++;
                break;
              }
            }
            if(p==0)
            {
              //if p is 0
              //then i is prime
              cout<<i<<"\n";
            }
        }
    }
    return 0;
}

6.
Write a program to find the sum of the even and odd digits of the number which is given as input.

Level 3

Ask Yours
Post Yours