C++ PROGRAM to find whether the given number is even or odd
#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
C++ Program to Check Leap Year or Not
#include<iostream>
using namespace std;
int main() {
int year;
cout<<"Enter the Year: ";
cin>>year;
if((year%4==0) && (year%100!=0))
cout<<"\nIt is a Leap Year";
else if(year%400==0)
cout<<"\nIt is a Leap Year";
else
cout<<"\nIt is not a Leap Year" << endl;
return 0;
}
C++ Program to finds largest of three numbers
#include<iostream>
using namespace std;
int main() {
float n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
return 0;
}
C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case
#include<iostream>
using namespace std;
int main() {
char op;
double n1,n2;
cout<<"Enter the Operator ( +, -, *, /)";
cin>>op;
cout<<"Enter two numbers";
cin>>n1>>n2;
switch(op)
{
case '+':
cout<< n1 << " + "<< n2<< " =" <<(n1+n2);
break;
case '-':
cout<< n1 << " - " << n2 << "="<<(n1-n2);
break;
case '*':
cout<< n1 << " * " << n2 << "= " <<(n1*n2);
break;
case'/':
if( n2!=0.0)
cout<< n1<<" / "<< n2<< " = "<<(n1/n2);
else
cout<< "its divide by zero situation";
break;
default:
cout<< op <<" is an invalid operator";
}
return 0;
}
C++ Program to Calculate Sum of Natural Numbers
#include<iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
C++ Program to Find Factorial of Number.
#include<iostream>
using namespace std;
int main()
{
int i,num,fact;
fact=1;
cout<<"/n Enter a Number:";
cin>>num;
for(i=1;i<=num;i++)
fact=fact*i;
cout<<" factorial of "<< num<<"is"<< fact;
return 0;
}
C++ Program to Generate Multiplication Table
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; i++)
{
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}