Sunteți pe pagina 1din 13

1. Program to find LCM of two numbers.

#include <iostream.h>
#include <conio.h>
void main (){
clrscr();
long fn, sn;
cout << "Enter first Number";
cin >> fn;
cout << "Enter second Number";
cin >> sn;
int g = hcf(fn,sn);
int lcm = (fn*sn)/g;
cout << "LCM of numbers " << fn << " and " << sn<< " is " << lcm;}

Method 1: OR Method 2:
int hcf(int a, int b){ int hcf(int a, int b){
while(a!=b){ int r = 1;
if (a>b) while (r != 0){
a = a-b; r = a % b;
else a = b;
b = b-a; } b = r; }
return a; } return a; }

2. Program to find sum of digits of a given number.


#include <iostream.h>
void main (){
int n, sum = 0;
cout << "Enter the Number";
cin >> n;
while(n!=0){
sum = sum +(n%10);
n = n/10;}
cout << sum;
}
3. Program to reverse the digits of a given number.
#include <iostream.h>
void main (){
int n, rn = 0;
while (n!=0){
int a = n%10;
rn = rn*10 + a;
n = n/10;}
cout << rn;}

4. Program to check whether a given number is Armstrong Number.


/* An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal
to the number itself. For example, 153 and 371 is an Armstrong number since 33 + 73 + 13 = 371.*/
#include <iostream.h>
void main()
{ int c=0,a,n,temp;
cout << "Enter a number\n";
cin >> n;
1
temp=n;
while(n>0) {
a=n%10;
n=n/10;
c=c+(a*a*a); }

if(temp==c)
cout << "An Armstrong Number.";
else
cout << "Not an Armstrong Number.";
}
4. Program to find whether a given number is Palindrome.
// Program to find palindrome.
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
unsigned long num, n, digit, rev;
cout << "\n Enter the number:";
cin >> num;
n = num;
do{
digit = num % 10;
rev = rev*10 + digit;
num = num/10;
}while (num) ;
cout << "The reverse of the number is :" << rev << "\n";
if(n == rev)
cout << "\n The number is a palindrome";
else
cout << "\n The number is not a palindrome";
getch();
}

6. Program to find greatest among three given numbers:


#include <iostream.h>
void main (){
long i, j, k; cout << “Enter three numbers ”; cin >>i>>j>>k;
if ((i > j) && (i>k))
cout << i<< " is Greatest among the given numbers";
else if (j>k)
cout << j<< " is Greatest among the given numbers";
else
cout <<k<< " is Greatest among the given numbers";}

7. Program to find whether a given year is Leap Year.


#include <iostream.h>
void main (){
long i;
cout << "Enter a number\n";
cin >> i;

2
if (i%100 ==0)

if (i%400 ==0)
cout <<i<< " is Leap Year";
else
cout <<i<< " is Normal Century Year";

else if (i%4==0)
cout << i<<" is Leap Year";
else
cout << i<<" is not a Leap Year";}

8. Program to make Four Function Calculator.


//Four Function Calculator
#include<iostream.h>
int main()
{ char ch;
float a, b, result;
cout << "Enter two numbers : ";
cin >> a >> b;
cout <<"\n" << " Enter the operator (+, -, *, /) : " ;
cin >> ch;
cout << "\n" ;
if (ch=='+')
result = a + b;
else
if (ch =='-')
result = a - b;
else
if (ch =='*')
result = a * b;
else
if (ch =='/')
result = a / b;
else
cout << "Wrong operator \n" ;
cout << "\n" << "The calculated result is : " << result << "\n" ;
return 0;
}

9. Program to perform arithmetic calculation using switch.


#include<iostream.h>
#include<conio.h>
void main()
{
float op1,op2,res=0;
char ch;
cout<<"Enter two numbers : ";
cin>>op1>>op2;
cout<<"\nEnter an opeartor (+, -, *, /, %) :";
cin>>ch;
switch(ch)

3
{
case '+': res = op1 + op2 ;
break;
case '-': res = op1 - op2 ;
break;
case '*': res = op1 * op2 ;
break;
case '/': if (op2 == 0)
cout << "Divide by zero error !!!" ;
else
res = op1 / op2 ;
break;
default: cout<<"\n Wrong Operator!!!";
break;
case '%': if (op2 == 0)
cout << "Divide by zero error !!!" ;
else
{ int r,q;
q = op1 / op2;
r = op1 - (q * op2);
res = r ;
}
break;

}
cout << "The calculated result is : " << res << "\n";
getch();
}

10. Program to calculate commission as per given criteria.


//Calculate commision.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
float sales , comm;
cout << "Enter sales made by the salesman";
cin >> sales;
if (sales > 5000)
if (sales > 12000)
if (sales > 22000)
if (sales > 30000)
comm = sales * 0.15;
else
comm = sales * 0.10;
else
comm = sales * 0.07;
else
comm = sales * 0.03;
else
comm = 0;
cout << "\n" << "The comission is : " << comm << "\n";

4
return 0; }
11. Menu driven program to find area, perimeter and diagonal of a rectangle with exit option.
#include<iostream.h>
#include<conio.h>
#include<math.h> //for sqrt() function
#include<process.h> //for exit function
void main()
{float l,b,peri, area , diag;
char ch, ch1;
cout<<"\n Rectangle menu";
cout<<"\n 1. Area";
cout<<"\n 2. Perimeter";
cout<<"\n 3. Diagonal";
cout<<"\n 4. Exit" << "\n";
cout<<"Enter your choice";
do
{
cin >> ch;
if (ch == '1' || ch == '2' || ch == '3')
{ cout << "Enter length and breadth: ";
cin >> l >> b;
}
switch(ch)
{
case '1': area = l * b ;
cout << "area = " << area;
break;
case '2': peri = 2 * (l + b);
cout << "Perimeter " << peri;
break;
case '3': diag = sqrt((l * l) + (b * b));
cout << "Diagonal = " << diag;
break;
case '4': cout << "Breaking" ;
getch();
exit(0);
default: cout << "Wrong choice !!!";
cout << "Enter a valid one";
break;
}
cout << "\n Want to enter more (y/n) ? ";
cin >> ch1;
if (ch1 == 'y' || ch1 == 'Y')
cout << "Againe enter choice (1-4) :";
}while (ch1 == 'y' || ch1 == 'Y');
getch(); }

12. Program to display use of break & continue.


#include<iostream.h>
#include<conio.h>
int main()
{cout << "The loop with \'break\' produces output as :\n";

5
for (int i = 1; i<=10; ++i)
{
if (i % 3 == 0)
break;
else
cout << i << endl;
}
cout << "The loop with \'continue\' produces output as : \n" ;
for (i = 1; i<=10; ++i)
{
if (i % 3 == 0)
continue;
else
cout << i << endl;
}
getch(); }

13. Program to find Factorial of a given Number using For Loop


#include <iostream.h>
void main (){
long fact = 1, a, num;
cout << "Enter a number\n";
cin >> num;
for (a = 1; a <= num; a++) { fact= fact*a; }
cout<< fact;}

14. Program to find factorial of a number using while loop


#include <iostream.h>
void main (){
long fact = 1, i, num;
cout << "Enter a number\n";
cin >> num;
i = num;
while (i != 0) { fact = fact * i;
--i; }
cout<< "The factorial of " << num << " is " << fact;}

15. Program to find factorial of a number using do-while loop


#include <iostream.h>
void main (){ long fact = 1, i, num;
cout << "Enter a number\n";
cin >> num;
i = num;
do {
fact = fact * i;
--i; } while (i != 0);
cout<< "The factorial of " << num << " is " << fact;}

16. Program to convert binary number into decimal number.


//binary to decimal
#include <iostream.h>
#include <math.h>

6
void main(){
int i, r, c = 0;
long b, n = 0;
cout << "Enter binary number";
cin >> b;
while (b > 0){
r = b % 10;
n = n + r * pow(2, c);
c = c + 1;
b = b/10;
}
cout << "Decimal equivalent = " << n;
}

17. Program to find sum of series. 1 + x^1/2! + x^2/3! + ….. + x^n/(n + 1)!
//sum of series 1 + x^1/2! + x^2/3! + ….. + x^n/(n + 1)!
#include <iostream.h>
#include <math.h>
void main(){
int n, i, x, j;
float p = 1, f, p1, sum = 1;
cout << "Enter the value of n \n";
cin >> n;
cout << "\nEnter the value of x \n";
cin >> x;
for ( i = 2; i <= n; i++)
{ f = 1;
for ( j = 1; j <= i; j++)
f = f * j;
p = p * x;
p1 = p / f;
sum = sum + p1;
}
cout << "\nSum of series is -> " << sum; }

18. Program to reverse a given String.


#include<iostream.h>
#include<string.h>
void main(){ char string[31], ch;
int i, len=0;
cout << "Enter the string \n";
cin.getline(string, 30);
for (i = 0; string[i] !='\0'; ++i) len++;
cout << "\nThe sring entered is : ";
cout.write(string, len);
int len1 = len;
for (i = 0; i < len; i++, len--){ ch = string[i];
string [i] = string [len-1] ;
string[len-1] = ch; }
cout << "\n\nThe reverse string is : ";
cout.write(string, len1); }

7
19. Program to compare two strings.

#include <iostream.h>
#include <string.h>
void main(){
int strcmp1(char *p, char *q);
char a[5], b[5];
cout << "Enter string 1";
cin >> a;
cout << "Enter string 2";
cin >>b;
int r = strcmp1(a, b);
if (r==0) cout <<”Equal” ;
else cout << “Not Equal”; }

int strcmp1(char * p, char * q){


int x = strcmp(p, q);
if (x == 0) return 0;
else return -1; }
20. Switch Case Construct
#include <iostream.h>
#include <stdio.h>
#include <stringh>
void main (){ int rtg;
char name[20], rt[6];
cout << "Enter name\n";
gets(name);
cout << “ Enter numeric rating\n”;
cin>>rtg;
switch (rtg){ //the data type of expression in a switch must be char, short or int:
case 1 : strcmp(rt, "*"); //Each switch case label must be a single value:
break;
case 2 : strcmp(rt, "* *");
break;
case 3 : strcmp(rt, "* * *");
break;
case 4 : strcmp(rt, "* * * *");
break;
case 5 : strcmp(rt, "* * * * *");
break;
default : strcmp(rt, " ");
break; }
cout << nm << " has given rating as " << rt << “\n”); }

21. Switch Case Construct


#include <iostream.h>
void main (){
int ua = 0, ub = 0, uc = 0, fail = 0;
int i = 4;
switch(i++){
case 1:
case 2: ++ua;

8
cout<< "ua is incremented";
break;
case 3:
case 4: ++ub;
cout<< "ub is incremented";
break;
case 5:++uc;
cout<< "uc is incremented";
break;
default: ++fail;
cout<< "fail incremented";
}
cout<< "ua = " + ua + "\tub = " + ub + "\nuc = " + uc + "\tfail = " + fail;
cout<< i;

22. Program to find Prime Number


#include <iostream.h>
void main (){
int n, i,k=0;
cout << "Enter the number \n";
cin >> n;
for (i = 2 ; i < = n/2; i++)
{
if ((n % i) == 0)
{k=1;
cout<< "Not Prime";
break; } }
if (k==0) cout << "Prime";}
23. Program to count number of words in a sentence.
#include <iostream.h>
#include <string.h>
void main(){
char str[20], ch = ' ';
int count=0;
cout <<"Enter string";
gets(str);
for(int i = 0;i<strlen(str); i++) { if(str[i] == ch) count++; }
count ++;
cout << count;}
24. Printing a Pattern
#include <iostream.h> Output
#include<iomanip.h> & & & & & & &
void main(){ & & & & &
int i, j, k=7; & & &
for (i = 4; i >=1; i--) &
{
for (j= 1; j <= (2*i-1); j++)
{cout << "& "; }
cout << "\n";
if (i==4) cout << " ";
if (i==3) cout << " ";
if (i==2) cout << " ";

9
if (i==1) cout << " "; } }

25. Printing a Pattern


#include <iostream.h>
void main(){ Output
int i, j; A
char ch; A B
for (i = 1; i < 7; i++) A B C
{ ch ='A'; A B C D
for (j= 1; j <= i; j++) A B C D E
{cout << char (ch)<< " "; A B C D E F
ch++;}
cout << '\n'; } }

26. Find roots of a quadratic equation.


//Find roots of a quadratic equation.
#include<iostream.h>
#include<math.h>
int main() { float a, b, c, root1, root2, delta;
cout << "Enter three numbers a, b & c of "
<< " ax^2 + bx + c : \n";
cin >> a >> b >> c;
if (!a)
cout << "Value of \'a\' should not be zero"
<< "\n Aborting !!!!!" << "\n";
else
{
delta = b*b - 4 * a * c;
if (delta > 0)
{ root1 = (-b + sqrt(delta)) / 2 * a;
root2 = (-b - sqrt(delta)) / 2 * a;
cout << "root1 = " << root1
<< "root2 = " << root2 << "\n"; }
else if (delta == 0)
{ root1 = -b / 2 * a;
cout << "roots are real and equal" << "\n";
cout << "root1 = " << root1;
cout << "root2 = " << root2 << "\n"; }
else cout << "roots are complex and imaginary" << "\n"; }
return 0; }

27. Find whether a given character is alphabet, digit or any other character.
//input a character and print whether it is alphabet, digit or any other character.
#include<iostream.h>
int main() { char ch;
cout << "Enter a character : ";
cin >> ch;
if (((ch >='A') && (ch <= 'Z')) || ((ch >= 'a' ) && (ch <= 'z')))
cout << "You entered an alphabet" << "\n";
else
if (ch >='0'&& ch <= '9')

10
cout << "You entered a digit" << "\n";
else cout << "You entered a character other than "
<< " alphabets and digits" << "\n";
return 0; }

28. Find whether a given character is alphabet or digit using function.


#include <iostream.h>
#include <conio.h>
#include <ctype.h>
void main(){
clrscr();
char c;
cout << "Enter a character\n";
cin >> c;
if (isalnum(c))
if (isalpha(c)) cout << "\nThe character is alphabet";
else cout << "\nThe character is digit";
getch();}
29. Convert the characters of a string into lower case if it is in upper case or vice versa.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
void main(){
clrscr();
char c[20];
cout << "Enter a String\n";
gets(c);
for (int i = 0; c[i] != '\0'; ++i)
{if (islower(c[i]))
c[i] = toupper(c[i]);
else if (isupper(c[i]))
c[i] = tolower(c[i]) ;
}
puts(c); getch(); }
30. Find transpose of an Array.
#include <iostream.h>
#include <conio.h>
void main(){
clrscr();
int a[20][20], b[20][20], i,j,m,n;
cout << "Enter rows and column of matrix:\n";
cin >> m >> n;
cout << "Enter Matrix: \n";
for (i = 0; i < m; ++i){ for (j = 0; j < n; ++j) cin >> a[i][j];}
cout << "The Matrix is:\n";
for (i = 0; i < m; ++i){ cout << "\n";
for (j = 0; j < n; ++j)
cout << " " << a[i][j];}
for (i = 0; i < m; ++i){ for (j = 0; j < n; ++j) b[i][j] = a[j][i];}
cout << "\nTranspose of Matrix:\n";
for(i = 0; i < m; ++i){ cout << "\n"; for (j = 0; j < n; ++j) cout << " " << b[i][j];}

11
getch(); }
31. Swapping two variables without using third variable.

#include <iostream.h>
void main(){
int a, b, c;
cout << "Enter the value of first number ";
cin >> a;
cout << "Enter the value of second number ";
cin >> b;
a=a+b;
b = a-b;
a = a-b;
cout << "The value of first number = " << a << "\nThe value of second number = " << b;
}

32. Write a program to find the sum of the following series:

#include <math.h>
#include <iostream.h>

int fact(int i);

int fact(int i)
{
int output = 1;
for(int j =1; j<=i; ++j)
{
output *=j;
}
return output;
}

int main()
{
double result = 0;
int x;
cout <<"Enter a Num"<<endl;
cin>>x;
cout <<"Enter No of terms: "<<endl;
cin>>n;

for(int i=0; i<=n; ++i)


{
result = result + pow(-1.0, i) * (pow(x,i+1.0) / (fact(i+1)));
}

cout <<"\n\n\n\t\t"<<result;

33. Write a C++ program to obtain fibonacci series. (0 1 1 2 3 5 8 13 21 34 ….)

int n1=0,n2=1,n3,i,count=10;
cout<<n1<<" "<<n2;//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed

12
{
n3=n1+n2;
cout<<" "<<n3;
n1=n2;
n2=n3; }

13

S-ar putea să vă placă și