Posts

Showing posts from July, 2020

C++ Program to check whether a number is Armstrong or not?

Image
What is an Armstrong number? A three digit number is an Armstrong number if sum of cube of its digits is equal to itself. Program: #include<iostream> using namespace std; int armstrong(int x); int cube(int c); int main() { int num; cout<<"Enter a three digit Number="<<endl; cin>>num;     if(armstrong(num)==num)     {     cout<<"Number is Armstrong";     }     else     {     cout<<"Number is not Armstrong";     } return 0; } //Armstrong Function int armstrong(int x) { int a,sum=0,result; /* a=x%10; b=x/10; c=b%10; d=b/10; int result; result=cube(a)+cube(c)+cube(d); return result;*/ do { a=x%10; sum=sum+cube(a); x=x/10; }while(x!=0); result=sum; return result; } //Cube Function int cube(int c) { int r; r=c*c*c; return r; } Output:

C++ Program to print Fibonacci Series

Image
What is Fibonacci Series? A series of numbers in which each number is the sum of the two preceding numbers. For Example:: 1,1,2,3,5,8 Program: #include<iostream> using namespace std; int main() { int terms, n1=0, n2=1, next; cout<<"How many terms of Fibonacci series you want to show? "; cin>>terms; cout<<"Fibonacci series:"<<endl; for(int c=1;c<=terms;c++) { if(c==1) { cout<<n1<<" "; } else if(c==2) { cout<<n2<<" "; } else  { next=n1+n2;     n1=n2; n2=next;     cout<<next<<" "; } }     return 0; } Output:

C++ Program to find second minimum number

Image
Programs: #include<iostream> using namespace std; int main() { int num, min, sec_min; for(int n=1; n<=10; n++) { cout<<"Please Enter a Number= "; cin>>num; if(num<=min) { sec_min=min; min=num; } } cout<<"Minimum Number= "<<min<<endl; cout<<"Second Minimum Number= "<<sec_min; return 0; } Output:

C++ Program to check whether a number is Prime or not

Image
Program: #include<iostream> using namespace std; int main() { int n, a=2; bool r=false; cout<<"Enter a number="; cin>>n; for(int a=2; a<n; a++) { if(n%a==0) { r=true; } } if(r) { cout<<n<<" is not a prime number."; } else { if(n!=0&&n!=1) { cout<<n<<" is a prime number."; } else { cout<<"It is not a prime Number."; } } return 0; } Output:

C++ Program to swap two values

Image
Program: #include<iostream> using namespace std; int main() { int num1, num2, x; num1=5; num2=3; cout<<"Values before swaping:"<<endl; cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl; x=num1; num1=num2; num2=x; cout<<"Values after swaping:"<<endl; cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl; return 0; } Output:

C++ Program to take random numbers from user and calculate their sum

Image
Program: #include<iostream> using namespace std; int main() { int arr[10], sum=0; for(int a=0; a<=9; a++) { cout<<"Enter Random Number:"; cin>>arr[a]; } cout<<"\n\n\nPrint Random Numbers Entered by User...."; for(int a=0; a<=9; a++) { cout<<endl<<arr[a]; } for(int a=0; a<=9; a++) { sum=sum+arr[a]; } cout<<"\n\n\nSum of Random Numbers........"; cout<<"\nSum of Random Numbers is: "<<sum; return 0; } Output: