//C++ Program to add subtract and Multiply Complex Numbers
#include<iostream>
using namespace std;
#include<math.h>
//Declare structure
struct complex
{
int real,img;
};
int main()
{
complex a,b,c;
int choice;
cout<<"Enter First Complex Number:"<<endl;
cout<<"Enter Real Part of First Complex No=";
cin>>a.real;
cout<<"Enter imaginary Part of First Complex No.=";
cin>>a.img;
cout<<endl;
cout<<"Enter Second Complex Number:"<<endl;
cout<<"Enter Real Part of Second Complex No =";
cin>>b.real;
cout<<"Enter imaginary Part of Second Complex No.= ";
cin>>b.img;
cout<<endl;
cout<<"\tEnter 1 to add two complex number \n";
cout<<"\tEnter 2 to subtract two complex numbers\n";
cout<<"\tEnter 3 to multiply two complex numbers\n";
cout<<"Enter your choice ";
cin>>choice;
switch(choice)
{
case 1:
{
//add real with real
c.real=a.real+b.real;
//add imaginary with imaginary
c.img=a.img+b.img;
cout<<"********************************************************"<<endl;
//to check whether the imaginary part is positive or negative
if (c.img<0)
cout<<"\nThe Sum Of 1st and 2nd Complex No. = "<<c.real<<c.img<<"i"<<endl;
else
cout<<"\nThe Sum Of 1st and 2nd Complex No. = "<<c.real<<"+"<<c.img<<"i"<<endl;
break;
}
case 2:
{
c.real=a.real-b.real;
c.img=a.img-b.img;
if (c.img<0)
{
cout<<"\nThe Difference of 1st and 2nd Complex No is = "<<c.real<<c.img<<"i"<<endl;
}
else
cout<<"\nThe Difference of 1st and 2nd Complex No is = "<<c.real<<"+"<<c.img<<"i"<<endl;
break;
}
case 3:
{
//multipication of complex Nos start here
c.real=(a.real*b.real+(a.img*b.img)*-1);
c.img=a.img*b.real+b.img*a.real;
if (c.img<0)
{
cout<<"\nThe Product of 1st and 2nd Complex No is = "<<c.real<<c.img<<"i"<<endl;
}
else
cout<<"\nThe Product of 1st and 2nd Complex No is = "<<c.real<<"+"<<c.img<<"i"<<endl<<endl;
break;
}
}
return 0;
}
C++ program to find average of ten numbers
//program to find average of ten numbers enter by user
#include <iostream>
using namespace std;
int main()
{
int num,sum=0,count=0,avg;
cout<<"Enter ten numbers to find its average"<<endl;
for(int i=0;i<10;i++)
{
cout<<"Enter number "<<i+1<<" : ";
cin>>num;
sum=sum+num;
count++;
}
avg=sum/count;
cout<<"\nAverage of ten Numbers is = "<<avg<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int num,sum=0,count=0,avg;
cout<<"Enter ten numbers to find its average"<<endl;
for(int i=0;i<10;i++)
{
cout<<"Enter number "<<i+1<<" : ";
cin>>num;
sum=sum+num;
count++;
}
avg=sum/count;
cout<<"\nAverage of ten Numbers is = "<<avg<<endl;
return 0;
}
C++ program to convert seconds into hours minutes and seconds
//program to convert seconds into hours minutes and seconds
#include <iostream>
using namespace std;
int main()
{
int sec,min,hr,sec1;
cout<<"Enter seconds : ";
cin>>sec;
hr=sec/3600;
sec=sec%3600;
min=sec/60;
sec1=sec%60;
cout<<"\nNumber of hours = "<<hr<<endl;
cout<<"Number of minutes = "<<min<<endl;
cout<<"Number of seconds = "<<sec1<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int sec,min,hr,sec1;
cout<<"Enter seconds : ";
cin>>sec;
hr=sec/3600;
sec=sec%3600;
min=sec/60;
sec1=sec%60;
cout<<"\nNumber of hours = "<<hr<<endl;
cout<<"Number of minutes = "<<min<<endl;
cout<<"Number of seconds = "<<sec1<<endl;
return 0;
}
C++ program to print multipication table of a number
#include <iostream>
using namespace std;
int main()
{
int num,i=1;
cout<<"Enter Number to Make its table: ";
cin>>num;
while(i<=10)
{
cout<<num<<" * "<<i<<" = "<<num*i<<endl;
i++;
}
return 0;
}
using namespace std;
int main()
{
int num,i=1;
cout<<"Enter Number to Make its table: ";
cin>>num;
while(i<=10)
{
cout<<num<<" * "<<i<<" = "<<num*i<<endl;
i++;
}
return 0;
}
C++ perfect number program
/*program to check whether a number is perfect
or not. Perfect numbers are those whose factors
sum is equal to the number itself e.g 6 and 28 are perfect
factorsSum 1+2+3=6 1+2+4+7+14=28 */
#include <iostream>
using namespace std;
//function to find perfect number
void perfectNum(int x)
{
int factorSum=0;
for(int i=1;i<x;i++)
{
if(x%i==0)
{
//nested if to print factors 1+2+4....
if(i==1)
{
cout<<"\n"<<i;
factorSum=factorSum+i;
}
else
{
cout<<"+"<<i;
factorSum=factorSum+i;
}
}
}
if(factorSum==x)
{
cout<<"="<<x;
cout<<"\n\nThe number "<<x<<" is perfect\n";
}
else
{
cout<<" not = "<<x;
cout<<"\n\nThe number "<<x<<" is not perfect"<<endl;
}
}
int main()
{
int num;;
cout<<"Enter a number ";
cin>>num;
perfectNum(num);
return 0;
}
or not. Perfect numbers are those whose factors
sum is equal to the number itself e.g 6 and 28 are perfect
factorsSum 1+2+3=6 1+2+4+7+14=28 */
#include <iostream>
using namespace std;
//function to find perfect number
void perfectNum(int x)
{
int factorSum=0;
for(int i=1;i<x;i++)
{
if(x%i==0)
{
//nested if to print factors 1+2+4....
if(i==1)
{
cout<<"\n"<<i;
factorSum=factorSum+i;
}
else
{
cout<<"+"<<i;
factorSum=factorSum+i;
}
}
}
if(factorSum==x)
{
cout<<"="<<x;
cout<<"\n\nThe number "<<x<<" is perfect\n";
}
else
{
cout<<" not = "<<x;
cout<<"\n\nThe number "<<x<<" is not perfect"<<endl;
}
}
int main()
{
int num;;
cout<<"Enter a number ";
cin>>num;
perfectNum(num);
return 0;
}
C++ program to find Permutation and Combination
// Program to find permutation(nPr) and combination(nCr)
#include <iostream>
using namespace std;
//recursive function to find factorial
int fact(int a)
{
if(a>1)
{
return a*fact(a-1);
}
else
{
return 1;
}
}
int main()
{
int num1,num2,num3,n,r,choice;
cout<<"Enter the value of n ";
cin>>n;
cout<<"Enter the value of r ";
cin>>r;
if(n<r)
{
cout<<"The value of n must be greater than r "<<endl;
cout<<"Enter again ";
cin>>n>>r;
}
cout<<"\tEnter 1 to find permutation "<<endl;
cout<<"\tEnter 2 to find Combination\n "<<endl;
cout<<"Enter Your choice ";
cin>>choice;
switch(choice)
{
case 1:
{
num1=fact(n);
num2=fact(n-r);
num3=num1/num2;
cout<<"nPr = "<<num3<<endl;
break;
}
case 2:
{
num1=fact(n);
num2=fact(n-r)*fact(r);
num3=num1/num2;
cout<<" nCr = "<<num3<<endl;
break;
}
default:
{
cout<<"You Entered an incorrect choice :"<<endl;
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
//recursive function to find factorial
int fact(int a)
{
if(a>1)
{
return a*fact(a-1);
}
else
{
return 1;
}
}
int main()
{
int num1,num2,num3,n,r,choice;
cout<<"Enter the value of n ";
cin>>n;
cout<<"Enter the value of r ";
cin>>r;
if(n<r)
{
cout<<"The value of n must be greater than r "<<endl;
cout<<"Enter again ";
cin>>n>>r;
}
cout<<"\tEnter 1 to find permutation "<<endl;
cout<<"\tEnter 2 to find Combination\n "<<endl;
cout<<"Enter Your choice ";
cin>>choice;
switch(choice)
{
case 1:
{
num1=fact(n);
num2=fact(n-r);
num3=num1/num2;
cout<<"nPr = "<<num3<<endl;
break;
}
case 2:
{
num1=fact(n);
num2=fact(n-r)*fact(r);
num3=num1/num2;
cout<<" nCr = "<<num3<<endl;
break;
}
default:
{
cout<<"You Entered an incorrect choice :"<<endl;
break;
}
}
return 0;
}
C++ program to make simple calculator
#include <iostream>
using namespace std;
int main()
{
int num1,num2,numStore,choice;
cout<<"Enter first number : ";
cin>>num1;
cout<<"Enter second number : ";
cin>>num2;
cout<<"\t\tEnter 1 for addition\n";
cout<<"\t\tEnter 2 for subtraction\n";
cout<<"\t\tEnter 3 for multipication\n";
cout<<"\t\tEnter 4 for Division\n";
cout<<"Enter Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
{
numStore=num1+num2;
cout<<"The sum of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
case 2:
{
numStore=num1-num2;
cout<<"The difference of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
case 3:
{
numStore=num1*num2;
cout<<"The product of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
case 4:
{
if(num2==0)
{
cout<<"Division by Zero is not Possible";
break;
}
numStore=num1/num2;
cout<<"The result of division of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
}
cout << endl;
return 0;
}
using namespace std;
int main()
{
int num1,num2,numStore,choice;
cout<<"Enter first number : ";
cin>>num1;
cout<<"Enter second number : ";
cin>>num2;
cout<<"\t\tEnter 1 for addition\n";
cout<<"\t\tEnter 2 for subtraction\n";
cout<<"\t\tEnter 3 for multipication\n";
cout<<"\t\tEnter 4 for Division\n";
cout<<"Enter Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
{
numStore=num1+num2;
cout<<"The sum of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
case 2:
{
numStore=num1-num2;
cout<<"The difference of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
case 3:
{
numStore=num1*num2;
cout<<"The product of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
case 4:
{
if(num2==0)
{
cout<<"Division by Zero is not Possible";
break;
}
numStore=num1/num2;
cout<<"The result of division of two numbers "<<num1<<" and "<<num2<<" is = "<<numStore;
break;
}
}
cout << endl;
return 0;
}
C++ program to find factors of number entered by User
//C++ program to find factors of a number
#include <iostream>
using namespace std;
int main()
{
int number,i ;
cout << "Enter the number to find its factors : ";
cin >> number;
cout << "The factors of " << number << " are : ";
for(i=1;i<number;i++)
{
if(number%i==0)
{
cout<<i<<" ";
}
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int number,i ;
cout << "Enter the number to find its factors : ";
cin >> number;
cout << "The factors of " << number << " are : ";
for(i=1;i<number;i++)
{
if(number%i==0)
{
cout<<i<<" ";
}
}
cout << endl;
return 0;
}
C++ program to find Factorial of a number
//C++ program to find factorial of number using function
#include <iostream>
using namespace std;
void fact(int a)
{
int num=1;
for(int i=1;i<=a;i++)
{
num=num*i;
}
cout<<"\nThe factorial of the number "<<a<<" is = "<<num;
}
int main()
{
int num;
cout<<"Enter a number to find its Factorial ";
cin>>num;
fact(num);
return 0;
}
#include <iostream>
using namespace std;
void fact(int a)
{
int num=1;
for(int i=1;i<=a;i++)
{
num=num*i;
}
cout<<"\nThe factorial of the number "<<a<<" is = "<<num;
}
int main()
{
int num;
cout<<"Enter a number to find its Factorial ";
cin>>num;
fact(num);
return 0;
}
C++ program to find Factorial of number using Recursion
//C++ program to find factorial of number using recursive function
#include <iostream>
using namespace std;
int fact(int a)
{
if(a>1)
{
return a*fact(a-1);
}
else
{
return 1;
}
}
int main()
{
int num,num1;
cout<<"Enter a number to find its Factorial ";
cin>>num;
num1=fact(num);
cout<<"The factorial of the number "<<num<<" is = "<<num1;
return 0;
}
#include <iostream>
using namespace std;
int fact(int a)
{
if(a>1)
{
return a*fact(a-1);
}
else
{
return 1;
}
}
int main()
{
int num,num1;
cout<<"Enter a number to find its Factorial ";
cin>>num;
num1=fact(num);
cout<<"The factorial of the number "<<num<<" is = "<<num1;
return 0;
}
C++ program to find sum of Natural numbers
/*this program find sum of
natural numbers upto the range entered by user*/
#include <iostream>
using namespace std;
int main()
{
int sumStore=0,i,Srange,Erange;
cout<<"Enter start range ";
cin>>Srange;
cout<<"Enter End range ";
cin>>Erange;
for(i=Srange;i<=Erange;i++)
{
sumStore=sumStore+i;
}
cout<<"\nSum of Natural numbers from "<<Srange<<" to "<<Erange<<" is= "<<sumStore;
cout<<endl;
return 0;
}
natural numbers upto the range entered by user*/
#include <iostream>
using namespace std;
int main()
{
int sumStore=0,i,Srange,Erange;
cout<<"Enter start range ";
cin>>Srange;
cout<<"Enter End range ";
cin>>Erange;
for(i=Srange;i<=Erange;i++)
{
sumStore=sumStore+i;
}
cout<<"\nSum of Natural numbers from "<<Srange<<" to "<<Erange<<" is= "<<sumStore;
cout<<endl;
return 0;
}
C++ program to Reverse a number
#include <iostream>
using namespace std;
int main()
{
int sumStore=0,num,rem,num1;
cout<<"Enter a number to Reverse it ";
cin>>num;
num1=num;
while(num>0)
{
rem=num%10;
//to store number
sumStore=(sumStore*10)+rem;
num=num/10;
}
cout<<"\nThe Reverse of "<<num1<<" is "<<sumStore<<endl;
return 0;
}
using namespace std;
int main()
{
int sumStore=0,num,rem,num1;
cout<<"Enter a number to Reverse it ";
cin>>num;
num1=num;
while(num>0)
{
rem=num%10;
//to store number
sumStore=(sumStore*10)+rem;
num=num/10;
}
cout<<"\nThe Reverse of "<<num1<<" is "<<sumStore<<endl;
return 0;
}
C++ Palindrome number program
#include <iostream>
using namespace std;
int main()
{
int sumStore=0,num,rem,palindrome,num1;
cout<<"Enter a number ";
cin>>num;
palindrome=num;
num1=num;
//loop to check every digit of number Entered
while(num!=0)
{
rem=num%10;
//to store number
sumStore=(sumStore*10)+rem;
num=num/10;
}
//to check Entered number is palindorme or not
if(sumStore==palindrome)
{
cout<<"The Number "<<num1<<" is Palindrome"<<endl;
}
else
{
cout<<"The Number "<<num1<<" is not Palindrome"<<endl;
}
return 0;
}
using namespace std;
int main()
{
int sumStore=0,num,rem,palindrome,num1;
cout<<"Enter a number ";
cin>>num;
palindrome=num;
num1=num;
//loop to check every digit of number Entered
while(num!=0)
{
rem=num%10;
//to store number
sumStore=(sumStore*10)+rem;
num=num/10;
}
//to check Entered number is palindorme or not
if(sumStore==palindrome)
{
cout<<"The Number "<<num1<<" is Palindrome"<<endl;
}
else
{
cout<<"The Number "<<num1<<" is not Palindrome"<<endl;
}
return 0;
}
C++ program to find sum of Odd numbers
#include <iostream>
using namespace std;
int main()
{
int sumStore=0,i,Srange,Erange;
cout<<"Enter start range ";
cin>>Srange;
cout<<"Enter End range ";
cin>>Erange;
for(i=Srange;i<=Erange;i++)
{
if(i%2!=0)
{
sumStore=sumStore+i;
}
}
cout<<endl;
cout<<"Sum of odd numbers from "<<Srange<<" to "<<Erange<<" is = "<<sumStore<<endl;
return 0;
}
using namespace std;
int main()
{
int sumStore=0,i,Srange,Erange;
cout<<"Enter start range ";
cin>>Srange;
cout<<"Enter End range ";
cin>>Erange;
for(i=Srange;i<=Erange;i++)
{
if(i%2!=0)
{
sumStore=sumStore+i;
}
}
cout<<endl;
cout<<"Sum of odd numbers from "<<Srange<<" to "<<Erange<<" is = "<<sumStore<<endl;
return 0;
}
C++ program to convert Decimal to Binary
#include <iostream>
using namespace std;
int main()
{
int sumStore=0,base=1,dec,num,rem;
cout<<"Enter a decimal number : ";
cin>>num;
dec=num;
while(num!=0)
{
rem=num%2;
sumStore=sumStore+(rem*base);
base=base*10;
num=num/2;
}
cout<<"\nThe binary Equivalent of Decimal No "<<dec<<" is = "<<sumStore<<endl;
return 0;
}
using namespace std;
int main()
{
int sumStore=0,base=1,dec,num,rem;
cout<<"Enter a decimal number : ";
cin>>num;
dec=num;
while(num!=0)
{
rem=num%2;
sumStore=sumStore+(rem*base);
base=base*10;
num=num/2;
}
cout<<"\nThe binary Equivalent of Decimal No "<<dec<<" is = "<<sumStore<<endl;
return 0;
}
C++ program to convert Binary to Decimal
#include <iostream>
using namespace std;
int main() {
int sumStore=0,base=1,bin,num,num1,rem;
cout<<"Enter a binary number : ";
cin>>num;
bin=num;
num1=num;
//Loop to check the number is binary or not
while(num!=0)
{
if((num%10!=0 )&& (num%10!=1))
{
cout<<"The number "<<bin<<" is not binary\n";
return 0;
}
num=num/10;
}
//if number is binary then this loop will execute
while(num1!=0)
{
rem=num1%10;
sumStore=sumStore+(rem*base);
base=base*2;
num1=num1/10;
}
cout<<"\nThe decimal Equivalent of binary No "<<bin<<" is = "<<sumStore<<endl;
return 0;
}
using namespace std;
int main() {
int sumStore=0,base=1,bin,num,num1,rem;
cout<<"Enter a binary number : ";
cin>>num;
bin=num;
num1=num;
//Loop to check the number is binary or not
while(num!=0)
{
if((num%10!=0 )&& (num%10!=1))
{
cout<<"The number "<<bin<<" is not binary\n";
return 0;
}
num=num/10;
}
//if number is binary then this loop will execute
while(num1!=0)
{
rem=num1%10;
sumStore=sumStore+(rem*base);
base=base*2;
num1=num1/10;
}
cout<<"\nThe decimal Equivalent of binary No "<<bin<<" is = "<<sumStore<<endl;
return 0;
}
C++ program to find Sum of digits of a number Entered by User
#include <iostream>
using namespace std;
int main(void) {
int sum=0,count=0,i,a,rem,num;
cout<<"Enter a number to sum its digits : ";
cin>>num;
a=num;
for(i=0;num!=0;i++)
{
rem=num%10;
sum=sum+rem;
num=num/10;
count++;
}
cout<<"\nThe sum of "<<count<<" digited number "<<a<<" is = "<<sum<<endl;
return 0;
}
using namespace std;
int main(void) {
int sum=0,count=0,i,a,rem,num;
cout<<"Enter a number to sum its digits : ";
cin>>num;
a=num;
for(i=0;num!=0;i++)
{
rem=num%10;
sum=sum+rem;
num=num/10;
count++;
}
cout<<"\nThe sum of "<<count<<" digited number "<<a<<" is = "<<sum<<endl;
return 0;
}
C++ program to swap two numbers
#include<iostream>
using namespace std;
void swap(int & i,int & j);
int main()
{
int num1, num2;
cout<<"Enter first Number :";
cin>>num1;
cout<<"Enter Second Number :";
cin>>num2;
cout<<endl<<endl;
cout << "Before swapping:"<<endl;
cout<<"First Number is = " << num1<<endl;
cout<<"Second number is = " << num2<<endl<<endl;
swap(num1,num2);
cout << "After swapping"<<endl;
cout<<" First number is = " << num1 <<endl;
cout<<" Second number is = " << num2 <<endl<<endl;
return 0;
}
void swap(int& i, int& j)
{
int t = i;
i = j;
j = t;
}
using namespace std;
void swap(int & i,int & j);
int main()
{
int num1, num2;
cout<<"Enter first Number :";
cin>>num1;
cout<<"Enter Second Number :";
cin>>num2;
cout<<endl<<endl;
cout << "Before swapping:"<<endl;
cout<<"First Number is = " << num1<<endl;
cout<<"Second number is = " << num2<<endl<<endl;
swap(num1,num2);
cout << "After swapping"<<endl;
cout<<" First number is = " << num1 <<endl;
cout<<" Second number is = " << num2 <<endl<<endl;
return 0;
}
void swap(int& i, int& j)
{
int t = i;
i = j;
j = t;
}
C++ program to print diamond pattern
//C++ program to print diamond pattern
#include<iostream>
using namespace std;
int main()
{
int rows,i,j;
cout<<"Enter Number of rows ";
cin>>rows;
for(i=1;i<=rows;i++)
{
for(j=rows-i;j>=1;j--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;
}
for(i=rows-1;i>=1;i--)
{
for(j=rows-i;j>=1;j--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int rows,i,j;
cout<<"Enter Number of rows ";
cin>>rows;
for(i=1;i<=rows;i++)
{
for(j=rows-i;j>=1;j--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;
}
for(i=rows-1;i>=1;i--)
{
for(j=rows-i;j>=1;j--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;}
return 0;
}
C++ program to print Inverted triangle
#include<iostream>
using namespace std;
int main()
{
int rows,i,j;
cout<<"Enter Number of rows ";
cin>>rows;
cout<<endl;
for(i=rows-1;i>=1;i--)
{
for(j=rows-i;j>=1;j--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;}
return 0;
}
using namespace std;
int main()
{
int rows,i,j;
cout<<"Enter Number of rows ";
cin>>rows;
cout<<endl;
for(i=rows-1;i>=1;i--)
{
for(j=rows-i;j>=1;j--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;}
return 0;
}
C++ program to print Triangle pattern of asterisks
#include<iostream>
using namespace std;
int main()
{
int rows;
cout<<"Enter Number of rows ";
cin>>rows;
for (int j=0;j<rows;++j)
{
for(int k=rows-j;k>0;--k)
{
cout<<" ";
}
for (int m=0;m<j;++m)
{
{
cout<<"*"<<" ";
}
}
cout<<" "<<endl<<" ";
}
return 0;
}
using namespace std;
int main()
{
int rows;
cout<<"Enter Number of rows ";
cin>>rows;
for (int j=0;j<rows;++j)
{
for(int k=rows-j;k>0;--k)
{
cout<<" ";
}
for (int m=0;m<j;++m)
{
{
cout<<"*"<<" ";
}
}
cout<<" "<<endl<<" ";
}
return 0;
}
C++ program to print Fabonacci series upto n terms
#include <iostream>
using namespace std;
int main()
{
int i=0,j=1,sum=0,limit;
cout<<"Enter limit for series :";
cin>>limit;
cout<<"The fabanacci series "<<"upto "<<limit<<" term is "<<i<<" ";
for (int k=1;k<limit;k++)
{
sum=i+j;
cout<<sum<<" ";
i=i+j;
j=i-j;
}
cout<<endl;
return 0;
}
using namespace std;
int main()
{
int i=0,j=1,sum=0,limit;
cout<<"Enter limit for series :";
cin>>limit;
cout<<"The fabanacci series "<<"upto "<<limit<<" term is "<<i<<" ";
for (int k=1;k<limit;k++)
{
sum=i+j;
cout<<sum<<" ";
i=i+j;
j=i-j;
}
cout<<endl;
return 0;
}
C++ Tower of Hanoi program
#include <iostream>
using namespace std;
#include<math.h>
int move;
void movetower(int n,char from,char to,char temp);
void movedisk(char from,char to);
int main()
{
int disks,moves;
cout<<"\t\tWelcome to Tower of Hanoi Program : "<<endl;
cout<<"\t\tEnter number of disks ";
cin>>disks;
cout<<endl;
movetower(disks,'1','2','3');
// to count total number of moves of disk
moves=(pow(2,disks))-1;
cout<<"\n\t\tTotal moves is = "<<moves<<endl;
return 0;
}
void movetower(int n,char from,char to,char temp)
{
if (n==1)
{
movedisk(from,to);
}
else
{
movetower(n-1,from,temp,to);
movedisk(from,to);
movetower(n-1,temp,to,from);
}
}
void movedisk(char from,char to)
{
cout<<"\t\tMove top disk from "<<from<<" to "<<to<<endl;
}
using namespace std;
#include<math.h>
int move;
void movetower(int n,char from,char to,char temp);
void movedisk(char from,char to);
int main()
{
int disks,moves;
cout<<"\t\tWelcome to Tower of Hanoi Program : "<<endl;
cout<<"\t\tEnter number of disks ";
cin>>disks;
cout<<endl;
movetower(disks,'1','2','3');
// to count total number of moves of disk
moves=(pow(2,disks))-1;
cout<<"\n\t\tTotal moves is = "<<moves<<endl;
return 0;
}
void movetower(int n,char from,char to,char temp)
{
if (n==1)
{
movedisk(from,to);
}
else
{
movetower(n-1,from,temp,to);
movedisk(from,to);
movetower(n-1,temp,to,from);
}
}
void movedisk(char from,char to)
{
cout<<"\t\tMove top disk from "<<from<<" to "<<to<<endl;
}
C++ program to find Prime Numbers between two Numbers
//C++ program to Find prime Numbers b/w two intervals
#include <iostream>
#include<conio.h>
using namespace std;
int prime(int a)
{
int count=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==0)
{
cout<<a<<" ";
}
return a;
}
int main()
{
int a,b;
cout<<"Enter two numbers to find prime Number b/w it "<<endl;
cout<<"Enter first No : ";
cin>>a;
cout<<"Enter second No : ";
cin>>b;
cout<<"\nThe prime number b/w "<<a<<" and "<<b<<" are "<<endl<<endl;
for(int i=a+1;i<b;i++)
{
prime(i);
}
getch();
return 0;
}
#include <iostream>
#include<conio.h>
using namespace std;
int prime(int a)
{
int count=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==0)
{
cout<<a<<" ";
}
return a;
}
int main()
{
int a,b;
cout<<"Enter two numbers to find prime Number b/w it "<<endl;
cout<<"Enter first No : ";
cin>>a;
cout<<"Enter second No : ";
cin>>b;
cout<<"\nThe prime number b/w "<<a<<" and "<<b<<" are "<<endl<<endl;
for(int i=a+1;i<b;i++)
{
prime(i);
}
getch();
return 0;
}
Subscribe to:
Posts (Atom)