Posts

Showing posts from 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:

12.c++ program to print numbers from 1 to 10 using for loop

Image
//C++ program to print numbers from 1 to 10 INPUT: #include<iostream> using namespace std; int main() {     int i;     for(i=1;i<=10;i++)         {             cout<<i<<endl;         }     return 0; } OUTPUT:  

10.c++ program to check if a leap year or not

Image
// C++ program to check if a year is leap year or not INPUT: #include<iostream> using namespace std; int main() {     int z;     cout<<"Enter a year:";     cin>>z;     if(z%4==0){         if(z%100==0){             if(z%400==0)                 cout<<"\n\t"<<z<<" is a leap year";             else                 cout<<"\n\t"<<z<<" is not a leap year";         }         else             cout<<"\n\t"<<z<<" is a leap year";     }     else         cout<<"\n\t"<<z<<" is not a leap year";     return 0; } OUTPUT:

9.c++ program to print number entered by user

Image
  // C++ program to print number entered by user INPUT: #include<iostream> using namespace std; int main() {     int a;     cout<<"Enter an integer : ";     cin>>a;     cout<<"\n"<<" You Entered "<<a<<endl;     return 0; } OUTPUT:

8.c++ program to check whether the number is even or odd

Image
FIRST METHOD:   // C++ program to check whether the number is even or odd using if else INPUT: #include<iostream> using namespace std; int main() {     int z;     cout<<"\n Enter an integer:";     cin>>z;     if(z%2==0)         cout<<"\n\t"<<z<<" is Even "<<endl;     else         cout<<"\n\t"<<z<<" is Odd "<<endl;     return 0; } SECOND METHOD: // C++ program to check whether the number is even or odd using ternary operators INPUT: #include<iostream> using namespace std; int main() {     int z;     cout<<"Enter an integer : ";     cin>>z;     (z%2==0) ? cout<<"\n\t"<<z<<" is Even "<<endl : cout<<"\n\t"<<z<<" is Odd "<<endl;     return 0; } OUTPUT:

7.c++ program to swap two numbers using 6 methods

Image
SWAPPING NUMBERS USING 6 METHODS: FIRST METHOD: // C++ program to swap numbers using temporary variable INPUT: #include<iostream> using namespace std; int main() {     int x,y,z;     cout<<"Enter The Value of x : ";     cin>>x;     cout<<"Enter The Value of y : ";     cin>>y;     cout<<"\n\t Before Swapping"<<endl;     cout<<"x = "<<x<<endl;     cout<<"y = "<<y<<endl;     z=x;     x=y;     y=z;     cout<<"\n\t After Swapping"<<endl;     cout<<"x = "<<x<<endl;     cout<<"y = "<<y<<endl;     return 0; } SECOND METHOD: // C++ program to swap numbers without using Temporary variables using addition and subtraction operator INPUT: #include<iostream> using namespace std; int main() {     int x,y;     cout<<"Enter The Value of x : ";     cin>>x;     cout<<&

6.c++ program to get an ASCII value and display the corresponding character

Image
 // C++ program to get an ASCII value and display the corresponding character #include<iostream> using namespace std; int main() {     int a;     char b;     cout<<"\n Enter ASCII code (0 to 255):";     cin>>a;     b=a;     cout<<"\n Equivalent Character:"<<b<<endl; } OUTPUT:

4.c++ program to compute quotient and remainder

Image
  // C++ program to compute quotient and remainder INPUT: #include<iostream> using namespace std; int main() {     int divisor,dividend,quotient,remainder;     cout<<"Enter Dividend:";     cin>>dividend;     cout<<"Enter Divisor:";     cin>>divisor;     quotient=dividend/divisor;     remainder=dividend%divisor;     cout<<"\n Quotient = "<<quotient<<endl;     cout<<"\n Remainder = "<<remainder<<"\n";     return 0; } OUTPUT:

3.c++ Program to accept any character and display its next character

Image
 // C++ Program to accept any character and display its next character INPUT: #include<iostream> using namespace std; int main() {     char a;     cout<<"\n Enter a character:";     cin>>a;     a=a+1; cout<<"\n The Next character:"<<a<<"\n"; } OUTPUT:

5.c++ program to get 2 integer numbers and display their sum

Image
  Introduction to fundamental Data types:          Fundamental data types are predefined data types available with C++.There are five fundamental data types in  C++:                                 - char                                        - int                                        - float                                        - double                                        - void.                  Actually, these are the keywords for defining the data types. (1) int data type:          Integers are whole numbers without any fraction. Integers can be positive or negative. Integer data types accepts and returns only integer numbers. if a variable is declared as an int, C++ compiler allows storing only integer values into it. if you try to store a fractional value in an int type variable it will accept only the integer portion of the fractional value..      // C++ program to get 2 integer numbers and display their sum INPUT: #include<iostream> using namespace std; int main(

2.c++ program to find the area of a circle

Image
// C++ program to find the area of a circle INPUT: #include<iostream> using namespace std; int main() {     int radius;     float area;     cout<<"\n Enter Radius:";     cin>>radius;     area = 3.14 * radius * radius;     cout<<"\n The area of circle = "<<area; } OUTPUT:

C++ Program to print a string

Image
  //C++ program to print a string INPUT: #include<iostream> using namespace std; int main() {        cout<< "Subscribe our channel";        return 0; } OUTPUT:

1.c++ program to find the total marks and average of three subjects

Image
 //  C++ program to find the total marks and average of three subjects  INPUT: #include<iostream>  using namespace std;  int main()  {          int a,b,c,sum,avg;          cout<<"\n Enter Mark 1:";         cin>>a;          cout<<"\n Enter Mark 2:";          cin>>b;          cout<<"\n Enter Mark 3:";         cin>>c; sum = a + b + c;         avg = sum / 3;          cout<<"\n The sum = "<<sum;         cout<<"\n The average = "<<avg<<"%"; } OUTPUT: