0% found this document useful (0 votes)
145 views

Input Example: Program To Print Positive Number Entered by The User // If User Enters Negative Number, It Is Skipped

The document contains multiple C++ programs that demonstrate basic programming concepts like input/output, if/else statements, loops, functions, and arithmetic operations. The programs cover topics such as calculating sums, determining positive/negative numbers, even/odd checks, swapping values, generating tables, and simple calculators. Overall the document serves as an example reference of basic C++ programming structures and techniques.

Uploaded by

Ahmad Khawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Input Example: Program To Print Positive Number Entered by The User // If User Enters Negative Number, It Is Skipped

The document contains multiple C++ programs that demonstrate basic programming concepts like input/output, if/else statements, loops, functions, and arithmetic operations. The programs cover topics such as calculating sums, determining positive/negative numbers, even/odd checks, swapping values, generating tables, and simple calculators. Overall the document serves as an example reference of basic C++ programming structures and techniques.

Uploaded by

Ahmad Khawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

// Input Example

# 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);
}

//Program to print positive number entered


by the user
// If user enters negative number, it is
skipped
#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;

// checks if the number is positive


if ( number > 0)
{
cout << "You entered a positive integer: " << number <<
endl;
}

cout << "This statement is always executed.";


return 0;

// Program to check whether an integer is positive or negative


// This program considers 0 as positive
number
#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
{
cout << "You entered a negative integer: " << number
<< endl;
}

cout << "This line is always printed.";


return 0;
}
// LARGEST USING ELSE IF
#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;
}

// 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;

cout << "Enter an integer: ";


cin >> 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;

cout << "Before swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

temp = a;
a = b;
b = temp;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

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;

cout << "Enter an alphabet: ";


cin >> c;

// evaluates to 1 (true) if c is a lowercase vowel


isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u');

// evaluates to 1 (true) if c is an uppercase vowel


isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' ||
c == 'O' || c == 'U');

// evaluates to 1 (true) if either isLowercaseVowel or


isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a constant.";

return 0;
}
// Program to built a simple calculator using switch Statement

#include <iostream>
using namespace std;

int main()
{
char o;
float num1, num2;

cout << "Enter an operator (+, -, *, /): ";


cin >> o;

cout << "Enter two operands: ";


cin >> 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

Enter an operator (+, -, *, /): +


-
Enter two operands: 2.3
4.5
2.3 - 4.5 = -2.2



// 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;

cout << "Enter a positive integer: ";


cin >> n;

for (i = 1; i <= n; ++i) {


factorial *= i; // factorial = factorial * i;
}

cout<< "Factorial of "<<n<<" = "<<factorial;


return 0;
}

























//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;
}
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

You might also like