#include<iostream>
using namespace std;
int main()
{
int armstrong=0,num=0,result=0,check;
cout<<"Enter Number to find it is an Armstrong number?";
cin>>num;
check=num;
for(int i=1;num!=0;i++){
armstrong=num%10;
num=num/10;
armstrong=armstrong*armstrong*armstrong;
result=result+armstrong;
}
if(result==check){
cout<<check<<" is an Armstrong Number";
}
else{
cout<<check<<" is NOT an Armstrong Number";
}
return 0;
}
How to Learn a Programming Language. If you have an interest in creating computer programs, mobile apps, websites, games or any other piece of software. Learn the fundamentals of programming, explore programs made by others. Write your own programs and share them.
Saturday, 20 December 2014
Find Armstrong number in C++
Find Palindrome number in c++
- #include<iostream>
- using namespace std;
- int main()
- {
- int palindrome, reverse=0;
- cout<<"Enter number: ";
- cin>>palindrome;
- int num=0,key=palindrome;
- for(int i=1;palindrome!=0;i++){
- num=palindrome%10;
- palindrome=palindrome/10;
- reverse=num+(reverse*10);
- }
- if(reverse==key){
- cout<<key<<" is a Palindrome Number";
- }
- else{
- cout<<key<<"is NOT a Palindrome Number";
- }
- return 0;
C++ program to find Fibonacci Series
Find last prime number occur before enter number
#include<iostream>
using namespace std;
int main()
{
int num,count=0;
cout<<"Enter number to find last prime number occurs before it: ";
cin>>num;
for( int a=num-1;a>=1;a--)
{
for(int b=2;b<a;b++)
{
if(a%b==0)
count++;
}
if(count==0)
{
if(a==1)
{
cout<<"no prime number less than 2";
break;
}
cout<<a<<" is the last prime number before entered number";
break;
}
count=0;
}
return 0;
}
Friday, 19 December 2014
C++ program to swap the values of two integers
#include<iostream>
using namespace std;
int main()
{
int var1, var2, swap;
cout<<"Enter value for first integer: ";
cin>>var1;
cout<<"Enter value for second integer: ";
cin>>var2;
cout<<" Values Before swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
swap=var1;
var1=var2;
var2=swap;
cout<<" Values After swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
return 0;
}
using namespace std;
int main()
{
int var1, var2, swap;
cout<<"Enter value for first integer: ";
cin>>var1;
cout<<"Enter value for second integer: ";
cin>>var2;
cout<<" Values Before swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
swap=var1;
var1=var2;
var2=swap;
cout<<" Values After swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
return 0;
}
Thursday, 18 December 2014
C++ program to find greatest number between three numbers
- #include<iostream>
- using namespace std;
- int main()
- {
- int num1,num2,num3;
- cout<<" Enter value for first number";
- cin>>num1;
- cout<<" Enter value for second number";
- cin>>num2;
- cout<<" Enter value for third number";
- cin>>num3;
- if(num1>num2&&num1>num3)
- {
- cout<<" First number is greatest:"<<endl<<"whick is= "<<num1;
- }
- else if(num2>num1&&num2>num3)
- {
- cout<<" Second number is greatest"<<endl<<"whick is= "<<num2;
- }
- else
- {
- cout<<" Third number is greatest"<<endl<<"whick is= "<<num3;
- }
- return 0;
- }
Program to find factorial of Number in C++
- What is a Factorial of a number 'n'?
The factorial of a number 'n' is the product of all number from 1 upto the number 'n'it is denoted by n!. For example n=5 then factorial of 5 will be 1*2*3*4*5= 120. 5!= 120
Factorial program C++ Logic:
- First think what is the factorial of a number? How mathematically it can be calculated.
- If you got this info then it will be very easier to make a C++ Program logic to find the factorial.
- User enters a number and we have to multiply all numbers upto entered number.
- Like if user enters 6 then Factorial should be equal to factorial= 1*2*3*4*5*6.
- In this case a for Loop will be very helpful. It will start from one and multiply all numbers upto entered number after it loop will be terminated.
- Take a variable and initialized it to 1 and in loop store multiplication result into it like in below program a variable
- Factorial is used for this purpose.what is we does not initialized it to 1 and initialized it to zero or remain it uninitialized. In case of 0 our result will be zero in case of any number entered
- In case of not initializing it our answer will correct mostly but if variable contains garbage value then we will not be able to get correct result.
- It is recommended that to initialize it to one.
#include<iostream>
using namespace std;
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
using namespace std;
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
FIND PRIME NUMBER IN C++
What is a PRIME NUMBER?
" A Natural number greater than 1 which has only two divisor 1 and itself is called prime number ".
For Example:
5 is prime, because it has only two divisors 1 and itself.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- //clrscr();
- int number,count=0;
- cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
- cin>>number;
- for(int a=1;a<=number;a++)
- {
- if(number%a==0)
- {
- count++;
- }
- }
- if(count==2)
- {
- cout<<" PRIME NUMBER \n";
- }
- else
- {
- cout<<" NOT A PRIME NUMBER \n";
- }
- //getch();
- }
FIND THE PERFECT NUMBER IN C++
What is a perfect number?
"Perfect number is a positive number which sum of all positive divisors excluding that number."
For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
and 28 is also a Perfect Number
since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128
- #include<iostream.h>
- #include<conio.h>
- void main() //Start of main
- {
- clrscr();
- int i=1, u=1, sum=0;
- while(i<=500)
- { // start of first loop.
- while(u<=500)
- { //start of second loop.
- if(u<i)
- {
- if(i%u==0 )
- sum=sum+u;
- } //End of if statement
- u++;
- } //End of second loop
- if(sum==i)
- {
- cout<<i<<" is a perfect number."<<"\n";
- }
- i++;
- u=1; sum=0;
- } //End of First loop
- getch();
- } //End of main
Subscribe to:
Posts (Atom)