Professional Documents
Culture Documents
#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) {
}
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();
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>
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>
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(); }
#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)
i)
iii)
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>
#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;
} 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! ........
#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
11