About  Contact


Programs Using Loops

Let us write few more programs and study the use of loops practically-
Q26.Write a program to display the series - 0, 1, 1, 2, 3, 5, 8, .... till nth terms.
Q27.Write a program to display the series 1, 4, 9, 16, 25... till nth terms.
Q28.Write a program to to find the sum of all the multiples of 47 from 470 to 1000 (use while loop )
Q29.Write a program to to find the factorial of any given number.
Q30.Write a program to accept two numbers then find sum, product, quotient (divide the first number by second number) difference and average as per the user choice. Repeat the program until the user exits it
Program-26
#include <iostream.h>
void main(){
    int a=0, b=1, c, n, i=2;
    cout<< "Enter the number of terms ( >2 ):";
    cin>>n;
    cout<< a << "  " << b<< "  ";
    while(i <= n){
        c=a+b;
        cout<< c <<"  ";
        a=b;
        b=c;
        i++;
    }
}
This series is the famous fibonacii series ( use in stock market prediction ). Here we have to take first term as 0 and the second term as 1, then generate third term as sum of the first and second term, fourth term is the sum of second and third term and so on. Therefore we have taken variable "a" and "b" for first and second term and displayed it before the loop starts. Then inside the loop we have generated next term as "c=a+b" then we have displayed it. then see that "a" is replaced by "b" first and then "b" is replaced by "c". See that the variable "i" which is counting number of terms already displayed is initialized to 2 at the begining because the first two terms are displayed before the loop starts. Ofcourse the beauty of the series is visible if number of terms is more than 2.
Program-27
#include <iostream.h>
void main(){
    int n, i;
    cout<< "Enter the number of terms:";
    cin>>n;
    for(i=1; i<=n; i++){
        cout<< i*i <<"  ";
    }
}
This is square series and self explanatory.
Program-28
#include <iostream.h>
void main(){
    int sum=0, i=470;
    while(i<=1000){
        sum=sum+i;
        i=i+47;        
    }
    cout<< "Result=" << sum;
}
In the above program see that we have taken a variable "sum" assign it to 0, which is an accumulator. Everytime we are generating a term of the series and adding with "sum" as "sum=sum+i" where "i" is a term of the series. Ultimately "sum" gives us the result.
Program-29
#include <iostream.h>
void main(){
    int f=1, a, i;
	 cout<<"Enter the number=";
	 cin>>a;
    for(i=1; i<=a; i++){
    	f=f*i;
   }
	cout<<"Answer="<<f;
}
In the above program see that the variable "f" is assign to 1 because we are finding product of all the umbers from 1 to the given number. Each time we aregeneration the numbers in the variable "i" and multiplying it with "f" and at the end of the loop "f" gives us the result.
Program-30
#include <iostream.h>
#include <conio.h>

void main(){
    int a, b, s, p, q, d, opt, av;
    cout<< "Enter first number=";
    cin>>a;
	 cout<<"Enter second number=";
	 cin>>b;
	 do{
		 clrscr();
		 cout<<" 1. for sum\n";
		 cout<<" 2. for diffrence\n";
		 cout<<" 3. for product\n";
		 cout<<" 4. for quotient\n";
		 cout<<" 5. for average\n";
		 cout<<" 6. for Exit\n\n\n";
		 cout<<" Enter your option please:";
		 cin>> opt;
		 switch(opt){
                    case 1: s=a+b;
                        cout<<"Sum="<<s;
                        break;
                    case 2: d= a - b;
                        cout<<"Difference="<<d;
                        break;
                    case 3: p=a*p;
                        cout<<"Product="<<p;
                        break;
                    case 4: q=(float)a/b;
                        cout<<"Quotient="<<q;
                        break;
                    case 5: av=(a+b)/2.0;
                        cout<<"Average="<<av;
                    break;
                        case 6: cout<<"You are Exiting Hit any key";
                    break;
                        default:cout<<"Wrong option !! Program wil Exit";
					
		}
		cout<<"\nHit any key to continue";
		getch();
	}while(opt>0 && opt<6);
}

In the avove program see that the variable "q" for quotient and "av" for average is taken as float because of divide operation is involved. While finding quotient see that we have written "float" before the operation, this is called type casting. The variable "a" and "b" both are integer and so the operation will be carried out as integer then it will convert the result into float- e.g if "a=10" and "b=3" the answer would be 3.0 as the variable on the left side is float type. But in the program since the variable "a" is converted to float by type casting, the operation will be carried out as float and the fractional part of the will also come in the resulte.g 3.3333.

Type casting- This the mechanism by which a variable is converted from one form to another e.g int to float. One thing remember in C++ the upward conversion is done automatically and it is called intrinsic conversion or intrinsic type casting. The order of data type is - double>float>int>char. In an expression the final result will be of type of hightest data type. When we need to convert a data type to a lower data type we have to do it oursefl and it is called extrinsic conversion or extrinsic type casting.

The header file "conio.h" is included in this program due to the fact that getch() is used for waiting for a user response (to press any key) and clrscr() is used for clear the screen; these functions are defined in conio.h header file. In Dev C++ "clrscr()" function is not there we have to use system("cls") function and is is defined in windows.h header file.






Disclaimer: We have taken otmost care to keep the information error free. The site is not responsible for any kind of damage of whatsoever nature.
©Copy right reserved