Posts

Showing posts from August, 2021

16.c++ program to check Armstrong number or not

Image
FIRST METHOD:  // C++ program to check Armstrong number or not of 3 digits INPUT: #include<iostream> using namespace std; int main() {     int n,r,sum=0,z;     cout<<"Enter the number : ";     cin>>n;     z=n;     while(n>0)     {         r=n%10;         sum = sum+(r*r*r);         n=n/10;     }     if(z==sum)         cout<<"\n\t"<<z<<" is an Armstrong number "<<endl;     else         cout<<"\n\t"<<z<<" is not an Armstrong number "<<endl;     return 0; SECOND METHOD: // C++ program to check Armstrong number of N digits INPUT: #include<iostream> #include<cmath> using namespace std; int main() {     int num,a,r,n=0,res=0,pwr;     cout<<"Enter an integer : ";     cin>>num;     a=num;     while(a!=0)     {         a=a/10;         n++;     }     a=num;     while(a!=0)     {         r=a%10;         pwr = round(pow(r,n));         res = res + p

15.c++ program to display numbers from 1 to 10 using while loop

Image
// C++ program to display numbers from 1 to 10 using while loop INPUT: #include<iostream> using namespace std; int main() {     int i=1;     while(i<=10)     {         cout<<i<<"\n";         i++;     } } OUTPUT:  

14.c++ program to find factorial

Image
// C++ program to find factorial of a given number INPUT: #include <iostream> using namespace std; int main() {     int n,i;     long double fact =1;     cout<<"Enter a positive number : ";     cin>>n;     for(i=1;i<=n;i++){             fact = fact * i;     }     cout<<"\n Factorial of " <<n<<" is : "<<fact<<endl;     return 0; } OUTPUT:

13.c++ program to calculate sum of natural numbers

Image
//  C++ program to calculate sum of natural numbers INPUT: #include<iostream> using namespace std; int main() {     int x,y=0,i;     cout<<"Enter a positive integer : ";     cin>>x;     for(i=1;i<=x;i++)         {             y=y+i;         }     cout<<"\n Sum = "<<y<<endl;     return 0; } OUTPUT:  

11.c++ program to multiply two numbers

Image
  // C++ program to multiply two numbers INPUT: #include<iostream> using namespace std; int main() {     float a,b,c;     cout<<"\n Enter a first value  : ";     cin>>a;     cout<<"\n Enter a second value : ";     cin>>b;     c = a * b;     cout<<"\n"<<" Product = "<<c<<endl;     return 0; } OUTPUT: