// Program to check whether an integer is positive or negative
// This program considers 0 as a 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 statement is always printed.";
return 0;
}
Example Programs
#include <iostream>
using namespace std;
int main()
{
char name;
int password;
cout << "Enter the name: "; cin >> name;
cout << " Enter your password: "; cin >> password;
if (name == 'GG') {
if (password == 1346) {
cout << "Login successful";
}
else {
cout << "Incorrect PASSWORD, Try again.";
}
}
else {
cout << " Incorrect Login Details, Try again.";
}
}
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 30, c = 20;
if(a > b && a > c) {
cout << a << " is larger than " << b << " and " << c << endl;
}
if( b > c && b > a)
{
cout << b << " is larger than " << a << " and " << c << endl; // executed
}
else
{
cout << c << " is larger than " << a << " and " << b << endl;
}
return 0;
}
Display next Character
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<< "nEnter any character : ";
cin>>ch;
ch++;
cout<<"Next character is : "<<ch;
return 0;
}
// Check given character is alphabet
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a Character: ";
cin>>ch;
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
cout<<endl<<ch<<" is an Alphabet";
else
cout<<endl<<ch<<" isn't an Alphabet";
cout<<endl;
return 0;
}
// Check given character is lower or upper case
#include<iostream>
using namespace std;
int main()
{
char ch;
int ascii;
cout<<"Enter a Character: ";
cin>>ch;
ascii = ch;
cout<<endl;
if(ascii>=65 && ascii<=90)
cout<<"It is an Uppercase Alphabet";
else
{
if(ascii>=97 && ascii<=122)
cout<<"It is a Lowercase Alphabet";
else
cout<<"It is not an Alphabet";
}
cout<<endl;
return 0;
}
/* Simple If..Else Ladder Example Program In C++ */
/* If..Else Ladder Programs, Find Vowels,Control Programs,C++ Examples */
// Header Files
#include<iostream>
//Main Function
using namespace std;
int main() {
// Local Variable 'ch' Declaration
char ch;
cout << "Simple If..Else Ladder Statement Example Programn";
cout << "Enter the Letter (In Capital Letter): ";
cin >> ch;
if (ch == 'A') {
cout << "Your Character Is A. Your Character is Voweln";
} if (ch == 'E') {
cout << "Your Character Is E. Your Character is Voweln";
} if (ch == 'I') {
cout << "Your Character Is I. Your Character is Voweln";
} if (ch == 'O') {
cout << "Your Character Is O. Your Character is Voweln";
} if (ch == 'U') {
cout << "Your Character Is U. Your Character is Voweln";
} else {
cout << "Your Character is Not Vowel. Otherwise Not a Capital Lettern";
}
getch();
return 0;
}
/* write a program that inputs marks of three subjects. if the average is more
than 80, it displays two messages " you are above standard" admission is granted".
*/
#include <iostream.h>
Int main()
{
int sub1,sub2, sub3;
float avg;
cout<<"Enter the marks of three subjectsn";
cin>>sub1>>sub2>>sub3;
avg = (sub1 + sub2 + sub3)/3.0;
if(avg > 80)
{
cout<<"You are above standardn";
cout<<"Admission is grantedn";
}
return 0;
}
// C++ program to find if an integer is even or odd or neither (0)
// using nested if statements
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
// outer if condition
if (num != 0) {
// inner(nested) if condition
if ((num % 2) == 0) {
cout << "The number is even." << endl;
}
// inner(nested) else condition
else {
cout << "The number is odd." << endl;
}
}
// outer else condition
else {
cout << "The number is 0 and it is neither even nor odd." << endl;
}
cout << "This line is always printed." << endl;
return 0;
}
Write a C++ program to find a solution for the following equation:
Enter (X and Y) and print Z then display the message "Wrong Values", if the two conditions
above are not satisfied.
Sol:
#include <iostream>
using namespace std;
int main ()
{
float X, Y;
float Z;
cin >> X;
cin>>Y;
if ((X + Y) >= 0)
{if (X > 0)
Z = (sqrt (X + Y)) / X ;
cout << "The value of Z is:"<< Z;
}
else
{cout << "Wrong Values";
}
return 0;
Then display the below phrases according to their equivalent Fahrenheit degree:
1. “Cold” when F ≤ 41.
2. “Nice” when 41< F ≤ 77.
3. “Hot” when F >77.
Sol:
#include <iostream>
using namespace std;
int main ()
{
float C,F;
cin >> C;
F = (9 / 5) * C + 32;
cout << "F="<<F<<'n';
if (F <= 41)
{cout << "Cold"<<'n';}
else if (F > 41)
if(F <= 77)
{cout << " Nice"<<'n';}
else if (F > 77)
{cout << "Hot"<<'n';}
return 0;
}
Write a C++ program to choose your country. The program contains a list of countries (Iraq,
Germany, Lebanon, Egypt, and France). When we choose any of these countries, the city
center of these countries is displayed. The countries will represented from (0) to (3) and
fourth country will be any number except these three numbers.
Sol:
#include <iostream>
using namespace std;
int main ()
{
int n;
cin >> n;
switch (n)
{case 0:
cout<< "Baghdad"; break;
case 1:
cout<< "Berlin"; break;
case 2:
cout<< "Beirut";break;
case 3:
cout<< "Cairo";break;
default:
cout<< "Paris";break;
}
return 0;
}
Program to calculate the circumference of a circle
Sol:
#include <iostream>
using namespace std;
int main() {
float radius, circumference;
const float PI = 3.14159;
cout << "Enter the radius of the circle: ";
cin >> radius;
circumference = 2 * PI * radius;
cout << "The circumference of the circle is: " << circumference << endl;
return 0;
}
Program to convert kilometers to miles
#include <iostream>
using namespace std;
int main() {
float kilometers, miles;
const float conversionFactor = 0.621371;
cout << "Enter distance in kilometers: ";
cin >> kilometers;
miles = kilometers * conversionFactor;
cout << "Distance in miles: " << miles << endl;
return 0;
}
Program to find the absolute value of a
number
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number < 0)
number = -number;
cout << "The absolute value is: " << number << endl;
return 0;
}
Program to calculate the volume of a cylinder
#include <iostream>
using namespace std;
int main() {
float radius, height, volume;
const float PI = 3.14159;
cout << "Enter the radius and height of the cylinder: ";
cin >> radius >> height;
volume = PI * radius * radius * height;
cout << "The volume of the cylinder is: " << volume << endl;
return 0;
}
Program to check if a number is prime
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number == 2 || number == 3 || number == 5 || number == 7)
cout << number << " is a prime number." << endl;
else if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0 || number % 7 == 0)
cout << number << " is not a prime number." << endl;
else
cout << number << " is a prime number." << endl;
return 0;
}
Program to display the grade based on
marks
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 90)
cout << "Grade: A" << endl;
else if (marks >= 80)
cout << "Grade: B" << endl;
else if (marks >= 70)
cout << "Grade: C" << endl;
else if (marks >= 60)
cout << "Grade: D" << endl;
else
cout << "Grade: F" << endl;
return 0;
}
Program to calculate the simple interest
#include <iostream>
using namespace std;
int main() {
float principal, rate, time, interest;
cout << "Enter principal, rate of interest and time: ";
cin >> principal >> rate >> time;
interest = (principal * rate * time) / 100;
cout << "Simple interest: " << interest << endl;
return 0;
}
Program to check if a year is a leap year
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << year << " is a leap year." << endl;
else
cout << year << " is not a leap year." << endl;
return 0;
}
Program to find the square of a number
#include <iostream>
using namespace std;
int main() {
int number, square;
cout << "Enter a number: ";
cin >> number;
square = number * number;
cout << "Square of " << number << " is " << square << endl;
return 0;
}
Program to check if a character is a vowel
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
cout << ch << " is a vowel." << endl;
else
cout << ch << " is not a vowel." << endl;
return 0;
}
Program to convert Celsius to Fahrenheit
#include <iostream>
using namespace std;
int main() {
float celsius, fahrenheit;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (celsius * 9/5) + 32;
cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
return 0;
}

Example Programs of CPP programs ch 1.pptx

  • 1.
    // Program tocheck whether an integer is positive or negative // This program considers 0 as a 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 statement is always printed."; return 0; } Example Programs
  • 2.
    #include <iostream> using namespacestd; int main() { char name; int password; cout << "Enter the name: "; cin >> name; cout << " Enter your password: "; cin >> password; if (name == 'GG') { if (password == 1346) { cout << "Login successful"; } else { cout << "Incorrect PASSWORD, Try again."; } } else { cout << " Incorrect Login Details, Try again."; } }
  • 3.
    #include <iostream> using namespacestd; int main() { int a = 10, b = 30, c = 20; if(a > b && a > c) { cout << a << " is larger than " << b << " and " << c << endl; } if( b > c && b > a) { cout << b << " is larger than " << a << " and " << c << endl; // executed } else { cout << c << " is larger than " << a << " and " << b << endl; } return 0; }
  • 4.
    Display next Character #include<iostream> usingnamespace std; int main() { char ch; cout<< "nEnter any character : "; cin>>ch; ch++; cout<<"Next character is : "<<ch; return 0; }
  • 5.
    // Check givencharacter is alphabet #include<iostream> using namespace std; int main() { char ch; cout<<"Enter a Character: "; cin>>ch; if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) cout<<endl<<ch<<" is an Alphabet"; else cout<<endl<<ch<<" isn't an Alphabet"; cout<<endl; return 0; }
  • 6.
    // Check givencharacter is lower or upper case #include<iostream> using namespace std; int main() { char ch; int ascii; cout<<"Enter a Character: "; cin>>ch; ascii = ch; cout<<endl; if(ascii>=65 && ascii<=90) cout<<"It is an Uppercase Alphabet"; else { if(ascii>=97 && ascii<=122) cout<<"It is a Lowercase Alphabet"; else cout<<"It is not an Alphabet"; } cout<<endl; return 0; }
  • 7.
    /* Simple If..ElseLadder Example Program In C++ */ /* If..Else Ladder Programs, Find Vowels,Control Programs,C++ Examples */ // Header Files #include<iostream> //Main Function using namespace std; int main() { // Local Variable 'ch' Declaration char ch; cout << "Simple If..Else Ladder Statement Example Programn"; cout << "Enter the Letter (In Capital Letter): "; cin >> ch; if (ch == 'A') { cout << "Your Character Is A. Your Character is Voweln"; } if (ch == 'E') { cout << "Your Character Is E. Your Character is Voweln"; } if (ch == 'I') { cout << "Your Character Is I. Your Character is Voweln"; } if (ch == 'O') { cout << "Your Character Is O. Your Character is Voweln"; } if (ch == 'U') { cout << "Your Character Is U. Your Character is Voweln"; } else { cout << "Your Character is Not Vowel. Otherwise Not a Capital Lettern"; } getch(); return 0; }
  • 8.
    /* write aprogram that inputs marks of three subjects. if the average is more than 80, it displays two messages " you are above standard" admission is granted". */ #include <iostream.h> Int main() { int sub1,sub2, sub3; float avg; cout<<"Enter the marks of three subjectsn"; cin>>sub1>>sub2>>sub3; avg = (sub1 + sub2 + sub3)/3.0; if(avg > 80) { cout<<"You are above standardn"; cout<<"Admission is grantedn"; } return 0; }
  • 9.
    // C++ programto find if an integer is even or odd or neither (0) // using nested if statements #include <iostream> using namespace std; int main() { int num; cout << "Enter an integer: "; cin >> num; // outer if condition if (num != 0) { // inner(nested) if condition if ((num % 2) == 0) { cout << "The number is even." << endl; } // inner(nested) else condition else { cout << "The number is odd." << endl; } } // outer else condition else { cout << "The number is 0 and it is neither even nor odd." << endl; } cout << "This line is always printed." << endl; return 0; }
  • 10.
    Write a C++program to find a solution for the following equation: Enter (X and Y) and print Z then display the message "Wrong Values", if the two conditions above are not satisfied. Sol: #include <iostream> using namespace std; int main () { float X, Y; float Z; cin >> X; cin>>Y; if ((X + Y) >= 0) {if (X > 0) Z = (sqrt (X + Y)) / X ; cout << "The value of Z is:"<< Z; } else {cout << "Wrong Values"; } return 0;
  • 11.
    Then display thebelow phrases according to their equivalent Fahrenheit degree: 1. “Cold” when F ≤ 41. 2. “Nice” when 41< F ≤ 77. 3. “Hot” when F >77. Sol: #include <iostream> using namespace std; int main () { float C,F; cin >> C; F = (9 / 5) * C + 32; cout << "F="<<F<<'n'; if (F <= 41) {cout << "Cold"<<'n';} else if (F > 41) if(F <= 77) {cout << " Nice"<<'n';} else if (F > 77) {cout << "Hot"<<'n';} return 0; }
  • 12.
    Write a C++program to choose your country. The program contains a list of countries (Iraq, Germany, Lebanon, Egypt, and France). When we choose any of these countries, the city center of these countries is displayed. The countries will represented from (0) to (3) and fourth country will be any number except these three numbers. Sol: #include <iostream> using namespace std; int main () { int n; cin >> n; switch (n) {case 0: cout<< "Baghdad"; break; case 1: cout<< "Berlin"; break; case 2: cout<< "Beirut";break; case 3: cout<< "Cairo";break; default: cout<< "Paris";break; } return 0; }
  • 13.
    Program to calculatethe circumference of a circle Sol: #include <iostream> using namespace std; int main() { float radius, circumference; const float PI = 3.14159; cout << "Enter the radius of the circle: "; cin >> radius; circumference = 2 * PI * radius; cout << "The circumference of the circle is: " << circumference << endl; return 0; }
  • 14.
    Program to convertkilometers to miles #include <iostream> using namespace std; int main() { float kilometers, miles; const float conversionFactor = 0.621371; cout << "Enter distance in kilometers: "; cin >> kilometers; miles = kilometers * conversionFactor; cout << "Distance in miles: " << miles << endl; return 0; }
  • 15.
    Program to findthe absolute value of a number #include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; if (number < 0) number = -number; cout << "The absolute value is: " << number << endl; return 0; }
  • 16.
    Program to calculatethe volume of a cylinder #include <iostream> using namespace std; int main() { float radius, height, volume; const float PI = 3.14159; cout << "Enter the radius and height of the cylinder: "; cin >> radius >> height; volume = PI * radius * radius * height; cout << "The volume of the cylinder is: " << volume << endl; return 0; }
  • 17.
    Program to checkif a number is prime #include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; if (number == 2 || number == 3 || number == 5 || number == 7) cout << number << " is a prime number." << endl; else if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0 || number % 7 == 0) cout << number << " is not a prime number." << endl; else cout << number << " is a prime number." << endl; return 0; }
  • 18.
    Program to displaythe grade based on marks #include <iostream> using namespace std; int main() { int marks; cout << "Enter your marks: "; cin >> marks; if (marks >= 90) cout << "Grade: A" << endl; else if (marks >= 80) cout << "Grade: B" << endl; else if (marks >= 70) cout << "Grade: C" << endl; else if (marks >= 60) cout << "Grade: D" << endl; else cout << "Grade: F" << endl; return 0; }
  • 19.
    Program to calculatethe simple interest #include <iostream> using namespace std; int main() { float principal, rate, time, interest; cout << "Enter principal, rate of interest and time: "; cin >> principal >> rate >> time; interest = (principal * rate * time) / 100; cout << "Simple interest: " << interest << endl; return 0; }
  • 20.
    Program to checkif a year is a leap year #include <iostream> using namespace std; int main() { int year; cout << "Enter a year: "; cin >> year; if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) cout << year << " is a leap year." << endl; else cout << year << " is not a leap year." << endl; return 0; }
  • 21.
    Program to findthe square of a number #include <iostream> using namespace std; int main() { int number, square; cout << "Enter a number: "; cin >> number; square = number * number; cout << "Square of " << number << " is " << square << endl; return 0; }
  • 22.
    Program to checkif a character is a vowel #include <iostream> using namespace std; int main() { char ch; cout << "Enter a character: "; cin >> ch; if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') cout << ch << " is a vowel." << endl; else cout << ch << " is not a vowel." << endl; return 0; }
  • 23.
    Program to convertCelsius to Fahrenheit #include <iostream> using namespace std; int main() { float celsius, fahrenheit; cout << "Enter temperature in Celsius: "; cin >> celsius; fahrenheit = (celsius * 9/5) + 32; cout << "Temperature in Fahrenheit: " << fahrenheit << endl; return 0; }