Program To Find LCM of Two Numbers.: C, A, N, Temp
Program To Find LCM of Two Numbers.: C, A, N, Temp
#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; }
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();
}
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";}
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();
}
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(); }
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(); }
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; }
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”; }
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;
9
if (i==1) cout << " "; } }
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; }
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;
}
#include <math.h>
#include <iostream.h>
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;
cout <<"\n\n\n\t\t"<<result;
int n1=0,n2=1,n3,i,count=10;
cout<<n1<<" "<<n2;//printing 0 and 1
12
{
n3=n1+n2;
cout<<" "<<n3;
n1=n2;
n2=n3; }
13