Input Example: Program To Print Positive Number Entered by The User // If User Enters Negative Number, It Is Skipped
Input Example: Program To Print Positive Number Entered by The User // If User Enters Negative Number, It Is Skipped
# include <iostream>
using namespace std;
int main()
{
float a, b, sum;
cout<<"\n please enter the first variable : ";
cin>>a;
cout<<"\n please enter the second variable : ";
cin>>b;
sum= a+b;
cout<<"\n sum of both a and b is "<<sum;
return(0);
}
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
if ( number >= 0)
{
cout << "You entered a positive integer: " << number
<< endl;
}
else
{
cout << "You entered a negative integer: " << number
<< endl;
}
return 0;
}
// Program to check whether an integer is
positive, negative or zero
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
if ( number > 0)
{
cout << "You entered a positive integer: " << number
<< endl;
}
else if (number < 0)
{
cout<<"You entered a negative integer: " << number <<
endl;
}
else
{
cout << "You entered 0." << endl;
}
cout << "This line is always printed.";
return 0;
}
//EVEN || ODD
#include <iostream>
using namespace std;
int main()
{
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
//Sum of two integer
# include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"\n please enter first number : ";
cin>>a;
cout<<"\n please enter second number : ";
cin>>b;
int sum=a+b;
cout<<"\n sum is "<<sum;
return(0);
}
//SWAPPING
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
return 0;
}
//TABLE
# include <iostream>
using namespace std;
int main()
{
int x;
cout<<"\n enter number";
cin>>x;
cout<<x<<"x1="<<x*1<<endl;
cout<<x<<"x2="<<x*2<<endl;
cout<<x<<"x3="<<x*3<<endl;
return(0);
}
//vowel||constant
#include <iostream>
using namespace std;
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
return 0;
}
// Program to built a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main()
{
char o;
float num1, num2;
switch (o)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1+num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1-num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1*num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1/num2;
break;
default:
// operator is doesn't match any case constant (+, -, *,
/)
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output
// C++ Program to find factorial of a number
// Factorial on n = 1*2*3*...*n
#include <iostream>
using namespace std;
int main()
{
int i, n, factorial = 1;
int main()
{
int n;
return 0;
}
Output
Enter an integer: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50