You are on page 1of 11

C++ PROGRAMMING PRACTICE SET-2

Q1:- Write a program to print number from 1 to 10


#include<iostream.h> #include<conio.h> void main() { int i=1; while(i<=10) { cout<<i<<"\n"; i++; } getch();

} Q2: Write a program to calculate the sum of first 10 natural number.

#include<iostream.h> #include<conio.h> void main() { int i=1,sum=0; while(i<=10) { sum+=i; i++; } cout<<"Sum :"<<sum; getch(); }

Q3: - Write a program to find the factorial value of any number entered through the keyboard.
#include<iostream.h> #include<conio.h> void main() { int n,fact=1; cout<<"Enter any number : "; cin>>n; while(n>=1) {

By: - S U KHAN, PGT (COMP)

fact*=n; n--; } cout<<"Factorial :"<<fact; getch();

}
Q4: -Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the
power of another.

#include<iostream.h> #include<conio.h> void main() { int n,p,r=1; cout<<"Enter the base number and exponent "; cin>>n>>p; for(int i=1;i<=p;i++) r=r*n; cout<<"Result :"<<r; getch();

} Q5:- Write a program to reveres any given integer number.


#include<iostream.h> #include<conio.h> void main() { int n,t,r,rev=0; cout<<"Enter any number : "; cin>>n; t=n; while(t>0) { r=t%10; t=t/10; rev=rev*10+r; } cout<<"Reverse of number "<<n<<" is "<<rev; getch();

By: - S U KHAN, PGT (COMP)

Q6:- Write a program to check given number is prime or not.


#include<iostream.h> #include<conio.h> void main() { int n,flag=0; cout<<"Enter any number : "; cin>>n; for(int i=2;i<n;i++) { if(n%i==0) { flag=1; break; } } if(flag==0 && n>1) cout<<"Number is prime"; else cout<<"Number is not prime"; getch();

} Q7: - Write a program to calculate HCF of Two given number.


#include<iostream.h> #include<conio.h> void main() { int dividend, divisor, rem, hcf; cout<<"Enter two numbers : "; cin>>dividend>>divisor; while(rem!=0) { rem=dividend%divisor; if(rem==0) hcf=divisor; else { dividend=divisor; divisor=rem; }

By: - S U KHAN, PGT (COMP)

} cout<<"HCF is : "<<hcf; getch(); }

Q8: - Write a program to enter the numbers till the user wants and at the end it should display the count of
positive, negative and zeros entered.

#include<iostream.h> #include<conio.h> void main() { int n, sum_p=0, sum_n=0, sum_z=0; char choice; do { cout<<"Enter number "; cin>>n; if(n>0) sum_p++; else if(n<0) sum_n++; else sum_z++; cout<<"Do you want to Continue(y/n)? "; cin>>choice; }while(choice=='y' || choice=='Y'); cout<<"Positive Number :"<<sum_p<<"\nNegative Number :"<<sum_n<<"\nZero Number :"<<sum_z; getch(); }

Q8: - Write a program to enter the numbers till the user wants and at the end it should display the maximum and
minimum number entered.

#include<iostream.h> #include<conio.h>

By: - S U KHAN, PGT (COMP)

void main() { int n, max=0, min=32767; char choice; do { cout<<"Enter number : "; cin>>n; if(n>max) max=n; if(n<min) min=n; cout<<"Do you want to Continue(y/n)? "; cin>>choice; }while(choice=='y' || choice=='Y'); cout<<"Maximum Number :"<<max<<"\nMinimum Number :"<<min; getch(); }

Q10: - Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the
number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

#include<iostream.h> #include<conio.h> void main() { int n,digit1,digit2,digit3; for(int i=1;i<=500;i++) { digit1=i/100; digit2=i/10 - digit1*10; digit3=i%10; if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == i) cout<<i<<endl; } getch();

} Q11: -Write a program to print Fibonacci series of n terms where n is input by user : 0 1 1 2 3 5 8 13 24 .....
#include<iostream.h> #include<conio.h>

By: - S U KHAN, PGT (COMP)

void main() { int f=0,s=1,t,n; cout<<"Enter Number of terms of Series : "; cin>>n; cout<<f<<" "<<s<<" "; for(int i=3;i<=n;i++) { t=f+s; cout<<t<<" "; f=s; s=t; } getch(); }

Q12: - Write a program to calculate the sum of following series where n is input by user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +1/n

#include<iostream.h> #include<conio.h> int main() { int i,n; float sum=0; cout<<"Enter the value of n "; cin>>n; for(i=1;i<=n;i++) sum += 1.0/i; cout<<"Sum : "<<sum; getch(); }

Q13: -Compute the natural logarithm of 2, by adding up to n terms in the series


1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n where n is a positive integer and input by user

#include<iostream.h> #include<conio.h> void main() { int i,n,sign=-1; float sum=0; cout<<"Enter the value of n "; By: - S U KHAN, PGT (COMP)

cin>>n; for(i=1;i<=n;i++) { sign *= -1; sum += sign*1.0/i; } cout<<"log 2 : "<<sum; getch();

} Q14: Write a program to print following : solution ii)

i)

********** ********** ********** ********** * *** ***** ******* *********

* ** *** **** *****


1 222 33333 4444444 555555555

iii)

* ** *** **** *****


1 212 32123 4321234 543212345

iv)

v)

vi)

(i) #include<iostream.h> #include<conio.h> void main() { int i,j; for(i=1;i<=4;i++) { for(j=1;j<=10;j++) cout<<'*'; cout<<endl; } getch(); } (ii) #include<iostream.h>

By: - S U KHAN, PGT (COMP)

#include<conio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) cout<<'*'; cout<<endl; } getch(); } (iii) #include<iostream.h> #include<conio.h> void main() { int i,j,k; for(i=1;i<=5;i++) { for(j=5;j>i;j--) cout<<' '; for(k=1;k<=i;k++) cout<<'*'; cout<<endl; } getch(); } (iv) #include<iostream.h> #include<conio.h> void main() { int i,j,k; for(i=1;i<=5;i++) { for(j=5;j>i;j--) cout<<' '; for(k=1;k<2*i;k++) cout<<'*'; cout<<endl;

By: - S U KHAN, PGT (COMP)

} getch(); } (v) #include<iostream.h> #include<conio.h> Void main() { int i,j,k; for(i=1;i<=5;i++) { for(j=5;j>i;j--) cout<<' '; for(k=1;k<2*i;k++) cout<<i; cout<<endl; } getch(); } (vi) #include<iostream.h> #include<conio.h> void main() { int i,j,k,l; for(i=1;i<=5;i++) { for(j=5;j>i;j--) cout<<' '; for(k=i;k>=1;k--) cout<<k; for(l=2;l<=i;l++) cout<<l; cout<<endl; } getch();

} Q15: - Write a program to compute sinx for given x. The user should supply x and a positive integer n. We compute the sine of x
using the series and the computation should use all terms in the series up through the term involving xn sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ........

By: - S U KHAN, PGT (COMP)

#include<iostream.h> #include<conio.h> void main() { int i,j,n,fact,sign=-1; float x, p=1,sum=0; cout<<"Enter the value of x : "; cin>>x; cout<<"Enter the value of n : "; cin>>n; for(i=1;i<=n;i+=2) { fact=1; for(j=1;j<=i;j++) { p=p*x; fact=fact*j; } sign=-1*sign; sum+=sign*p/fact; } cout<<"sin "<<x<<"="<<sum; getch();

} Q16: - Write a program to compute the cosine of x. The user should supply x and a positive integer n. We compute the cosine of x
using the series and the computation should use all terms in the series up through the term involving xn cos x = 1 - x2/2! + x4/4! - x6/6! .....

#include<iostream.h> #include<conio.h> void main() { int i,j,n,fact,sign=-1; float x, p=1,sum=0; cout<<"Enter the value of x : "; cin>>x; cout<<"Enter the value of n : "; cin>>n; for(i=2;i<=n;i+=2)

10

By: - S U KHAN, PGT (COMP)

{ fact=1; for(j=1;j<=i;j++) { p=p*x; fact=fact*j; } sum+=sign*p/fact; sign=-1*sign; } cout<<"cos "<<x<<"="<<1+sum; getch();

Write a program to calculate HCF of Two given number

11

By: - S U KHAN, PGT (COMP)

You might also like