MOST IMPORTANT PROGRAMS
Write a Program in C++to Print Largest Number when given two numbers
using Conditional Operator
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
cout<<” Enter two Numbers “<<endl;
cin>>a>>b;
c=a>b?a:b
cout<<” largest Number =”<<c;
getch();
}
Write a Program in C++ to Print given number is Prime or Not.
#include<iostream.h>
#include<conio.h>
Void main()
{
int a,b,c ;
cout<<” Enter a Number “<<endl;
cin>>a;
If(a==1)
{
cout<<” 1 is not a Prime number “;
}
else
{
for ( b=2 ; b<a ; b++ )
{
break ;
}
}
}
If(a==b)
{
cout<<a<<” is a prime number “;
}
else
{
cout<<a<<” is not a prime number “;
}
getch();
}
Write a program in C++ to print the given number is Palindrome or Not .
#include <iostream.h>
#include <conio.h>
void main()
{
int a, b, c = 0, d;
clrscr(); FLOWCHART
cout << "Enter any number: ";
cin >> a;
d = a;
while (a > 0)
{
b = a % 10;
c = (c * 10) + b;
a = a / 10;
}
if (c == d)
{
cout << "The given number is a palindrome";
}
else
{
cout << "Not a palindrome number";
}
getch();
}
Write a program in C++ to print Reverse of a given number.
#include <iostream.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter a number: ";
cin>>a;
c=0;
while(a>0)
{
b=a%10;
c=(c*10)+b;
a=a/10;
}
cout<<"Reverse number is: "<<c;
getch();
}
Write a program in C++ to print the given number is Armstrong or Not.
#include <iostream.h>
#include <conio.h>
void main()
{
int a,b,c=0,d;
clrscr();
cout<<"Enter a number: ";
cin>>a;
d=a; // original number save
while(a>0)
{
b=a%10;
c=c+(b*b*b);
a=a/10;
}
if(d==c)
cout<<"Armstrong Number";
else
cout<<"Not Armstrong Number";
getch();
}
Write a program in C++ to Print Fibonacci Series.
#include <iostream.h>
#include <conio.h>
void main()
{
int a=0, b=1, c, n, i;
clrscr();
cout<<"How many terms? ";
cin>>n;
cout<<"Fibonacci series: ";
for(i=1; i<=n; i++)
{
cout<<a<<" ";
c = a + b;
a = b;
b = c;
}
getch();
}
Write a program in C++ to Print Factorial Of a Number.
#include <iostream.h> FLOWCART
#include <conio.h>
void main()
{
int a, b = 1, c = 1;
clrscr();
cout << "Enter a number: ";
cin >> a;
while (c <= a)
{
b = b * c;
c++;
}
cout << "Factorial = " << b;
getch();
}
Write a program in C++ to print i.e., find Addition of 3*3 Matrix.
#include <iostream.h>
#include <conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3];
int d, e;
clrscr();
cout << "Enter A Matrix:\n";
for (d = 0; d < 3; d++)
{
for (e = 0; e < 3; e++)
{
cin >> a[d][e];
}
}
cout << "Enter B Matrix:\n";
for (d = 0; d < 3; d++)
{
for (e = 0; e < 3; e++)
{
cin >> b[d][e];
}
}
// Addition
for (d = 0; d < 3; d++)
{
for (e = 0; e < 3; e++)
{
c[d][e] = a[d][e] + b[d][e];
}
}
cout << "Result Matrix:\n";
for (d = 0; d < 3; d++)
{
for (e = 0; e < 3; e++)
{
cout << c[d][e] << " ";
}
cout << endl;
}
getch();
}
Write a program in C++ to print Square root of a Number.
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
float num, result;
clrscr();
cout << "Enter a number: ";
cin >> num;
result = sqrt(num);
cout << "Square Root = " << result;
getch();
}
Write a program in C++ to print Area Of Circle.
#include <iostream.h>
#include <conio.h>
void main()
{
float r, area;
clrscr();
cout << "Enter radius: ";
cin >> r;
area = 3.14 * r * r;
cout << "Area of Circle = " << area;
getch();
}
Write a Program in C++ to Print GCD (HCF) of a given Number.
#include <iostream.h>
#include <conio.h>
void main()
{
int a, b, temp;
clrscr();
cout << "Enter two numbers: ";
cin >> a >> b;
while (b != 0)
{
temp = b;
b = a % b;
a = temp;
}
cout << "GCD = " << a;
getch();
}
Write a Program in C++ to Print LCM of a given Number.
#include <iostream.h>
#include <conio.h>
void main()
{
int a, b, x, y, temp, gcd, lcm;
clrscr();
cout << "Enter two numbers: ";
cin >> a >> b;
x = a;
y = b;
while (y != 0)
{
temp = y;
y = x % y;
x = temp;
}
gcd = x;
lcm = (a * b) / gcd;
cout << "LCM = " << lcm;
getch();
}
Write a Program in C++ to print Largest Number when given Three numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
int a , b, c ;
cout<<” Enter any three numbers “;
cin>>a>>b>>c ;
if (a>b)
{
if( a>c )
{
cout<<” Largest No. = “<<a;
}
}
if ( b>a)
{
if ( b>c )
{
cout<<” Largest No. =”<<b;
}
}
if ( c>a )
{
if ( c>b )
{
cout<<” Largest No. = “<<c;
}
}
getch();
}
Write a program in C++ to Print GCD and LCM when given two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
int a, b, i, gcd, lcm;
clrscr();
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
// Finding GCD
for(i = 1; i <= a && i <= b; i++)
{
if(a % i == 0 && b % i == 0)
{
gcd = i;
}
}
// Finding LCM
lcm = (a * b) / gcd;
cout<<"GCD = "<<gcd<<endl;
cout<<"LCM = "<<lcm;
getch();
}
Write a program in C++ to Print the Even Number in between 50 to 100.
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
cout<<"Even numbers between 50 and 100 are:"<<endl;
for(i = 50; i <= 100; i++)
{
if(i % 2 == 0)
{
cout<<i<<" ";
}
}
getch();
}
Write a Program in C++ to Print the given Number is Odd or Not.
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
clrscr();
cout<<"Enter a number: ";
cin>>num;
if(num % 2 != 0)
{
cout<<"The number is Odd.";
}
else
{
cout<<"The number is Not Odd (It is Even).";
}
getch();
} 1
01
Write a Program in C++ to Print following output :
101
#include<iostream.h> 0101
#include<conio.h>
void main()
{
Int a , b , c ; c=1;
clrscr() ;
for ( a=1 ; a<=5 ; a++ )
{
for ( b=1 ; b<=a ; b++ )
{
If ( (a+b) % 2 == 0 )
{
cout<<1;
}
Else
{
cout<<0;
}
}
cout<<endl;
}