KEMBAR78
C++ Important Codes | PDF | Namespace | Computer Engineering
0% found this document useful (0 votes)
11 views39 pages

C++ Important Codes

The document contains a collection of important C++ programs demonstrating various concepts such as checking even/odd numbers, vowel/consonant identification, multiplication tables, factorial calculation, simple calculators, and pyramid patterns. It also includes examples of string manipulation, pointer usage, age calculation, and array initialization. Each example is presented with code snippets and comments to explain the functionality.

Uploaded by

qureshizada852
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)
11 views39 pages

C++ Important Codes

The document contains a collection of important C++ programs demonstrating various concepts such as checking even/odd numbers, vowel/consonant identification, multiplication tables, factorial calculation, simple calculators, and pyramid patterns. It also includes examples of string manipulation, pointer usage, age calculation, and array initialization. Each example is presented with code snippets and comments to explain the functionality.

Uploaded by

qureshizada852
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/ 39

IMPORTANT C++ PROGRAMS

Example 1:
Check Whether Number is Even or Odd using if else

#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;
}
Example 2:
C++ Program to Check Vowel or Consonant

#include <iostream>
using namespace std;
int main() {
int ch;
cout << "Enter an alphabet: ";
cin >> ch;
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'
|| ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O'
|| ch == 'u' || ch == 'U') {

cout<<"The character is a vowel.\n"<< endl;


}
else {
cout<<"The character is a consonant.\n”<< endl;
}

return 0;
}
Example 3:
Display Multiplication Table up to 10

#include <iostream>
using namespace std;

int main() {

int n;

cout << "Enter a positive integer: ";


cin >> n;

// run a loop from 1 to 10


// print the multiplication table
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}

return 0;
}
Example 4:
Find the Factorial of a Given Number
#include <iostream>
using namespace std;

int main() {
int n;
long factorial = 1.0;

cout << "Enter a positive integer: ";


cin >> n;

if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}

return 0;
}
Example 5:
Simple Calculator using switch statement

# include <iostream>
using namespace std;

int main() {

char op;
float num1, num2;

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


cin >> op;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch(op) {

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:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}

return 0;
}
Example 6:
Program to Print a Half-Pyramid Using *

*
**

***
****
*****

#include <iostream>
using namespace std;

int main() {

int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; i++) {


for(int j = 1; j <= i; j++) {
cout << "* ";
}
cout << "\n";
}
return 0;
}

Example 7:
Program to Print a Half-Pyramid Using Numbers
1
12
123
1234
12345

#include <iostream>
using namespace std;

int main() {

int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; i++) {


for(int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << "\n";
}
return 0;
}
Example 8:
Inverted Half-Pyramid Using *

*****
****
***
**
*

#include <iostream>
using namespace std;

int main() {

int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = rows; i >= 1; i--) {


for(int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;}

Example 9:
Inverted Half-Pyramid Using Numbers

12345
1234
123
12
1
#include <iostream>
using namespace std;

int main() {

int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = rows; i >= 1; i--)


{
for(int j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}

Example 10:
Print Floyd's Triangle.
1
23
456
7 8 9 10

#include <iostream>
using namespace std;

int main() {

int rows, number = 1;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; i++) {


for(int j = 1; j <= i; j++) {
cout << number << " ";
number++;
}

cout << endl;


}
return 0;
}

Example 11:
C++ Pointers (Address)
#include <iostream>
using namespace std;

int main()
{
// declare variables
int var1 = 3;
int var2 = 24;
int var3 = 17;

// print address of var1


cout << "Address of var1: "<< &var1 << endl;

// print address of var2


cout << "Address of var2: " << &var2 << endl;

// print address of var3


cout << "Address of var3: " << &var3 << endl;
}
Example 12:
String concatenation in c++
#include <iostream>
using namespace std;
int main () {
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
}

Example 13:
String concatenation in c++
#include <iostream>
#include <string>
using namespace std;
int main () {
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
return 0;
}
Example 14:
Numbers & strings in c++

#include <iostream>
#include <string>
using namespace std;
int main () {
string x = "10";
string y = "20";
string z = x + y;
cout << z;
return 0;
}

Example 15:
String length in c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
return 0;
}
Example 16:
Access string in c++

#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString = "PAKISTAN ZINDABAD";
cout << myString[0];
return 0;
}

Example 17:
Change string character in c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string myString = "Hello";
myString[0] = 'J';
cout << myString;
return 0;
}

Example 18:
Program to print positive number entered by the user if the user enters a 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;
}

Example 19:
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;
}

Example 20:
Program to check whether an integer is positive, negative or zero
#include <iostream>
Using namespace std;
int main() {
// variables to store the three numbers
int a, b, c;
cout<<”Enter values”<< endl;
cin>>a>>b>>c;
if (a == b) {
if (a == c) {
cout<<"Yes";
}
else
{
cout<<"No";
}
}
else
{
/cout<<"No";
}
return 0;
}

Example 21:
Program to generate a grading system using C++
#include <iostream>
Using namespace std;
int main() {
int marks;
cout <<“Enter Marks”;
cin >> marks;
if (marks >= 35)
{
if (marks >= 90)
{
cout<<"A";
} else if (marks >= 80)
{
cout <<"B";
}
else if (marks >= 70)
{
cout<<"C";
}
else if (marks >= 60)
{
cout<<"D";
} else
{
cout<<"E";
}
} else {
cout<<"F";
}
return 0;
}
Example 22:
Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std; int main()
{
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper)
{
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 doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
} //switch terminates here
return 0;
}

Example 23:
Program to build a factorial of a number using do while loop
#include<iostream>
using namespace std;
int main()
{
int fact=1,n,i;
cout<<"Enter number\n";
cin>>n;
i=1;
do
{
fact=fact*i;
i++;
}
while(i<=n);
cout<<fact;
}

Example 24:
Program to build sum of a number using do while loop
#include<iostream>
using namespace std;
int main()
{
int i,n,sum=0;
cout<<"Enter a positive integer\n";
cin>>n;
i=1;
do
{
sum=sum+i;
i++;
}
while(i<=n);
cout<<sum;
}
Example 25:
Program to build factorial of a number using while loop
#include <iostream>
using namespace std;
int main()
{
int fact=1,n,i;
cout<<"Enter the number\n";
cin>>n;
i=1;
while(i<=n)
{
fact=fact*i;
i++;
}
cout<<fact;
return 0;
}
Example 26:
Program to build a simple calculator using function
#include <iostream>
using namespace std;
float calculator()
{
float n1,n2,a,b,c,d;
cout << "enter value" << endl;
cin >> n1>>n2;
a=n1+n2;
b=n1-n2;
c=n1*n2;
d=n1/n2;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
}
int main ()
{
calculator ();
}
Example 27:
Program to build a pointers program and add up the pointers addresses
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
int *ptr1, *ptr2;
int sum;
cout <<"Enter first number:\n";
cin >> num1;
cout <<"Enter second number:\n ";
cin >> num2;
ptr1 = &num1;
ptr2 = &num2;
cout <<"First Pointer Value " <<*ptr1 <<endl;
cout <<"Second Pointer Value " <<*ptr2 << endl;
cout <<"First Pointer Address " <<ptr1 << endl;
cout <<"Second Pointer Address " <<ptr2 << endl;
sum = *ptr1 + *ptr2;
cout << "Sum is: " << sum;
return 0;
}
Example 28:
Program to build an Age Calculator
#include <iostream>
using namespace std;
int main()
{
int birthyear,birthmonth,birthdate;
int currentyear,currentmonth,currentdate;
int agey,agem,aged;
cout <<"\t\t\t\tAGE CALCULATOR USING C++ :" << endl;
cout << endl;
cout <<"Enter Birth Year : " ;
cin>>birthyear;
cout <<"Enter Birth Month : " ;
cin>>birthmonth;
cout <<"Enter Birth Date : " ;
cin>>birthdate;
cout <<"Enter Current Year : ";
cin>>currentyear;
cout <<"Enter Current Month : ";
cin>>currentmonth;
cout <<"Enter Current Date : " ;
cin>>currentdate;
agey=currentyear-birthyear;
agem=12-birthmonth;
aged=(currentyear - birthyear)
*365 + (currentmonth - birthmonth)
* 30 + (currentdate - birthdate);
cout<<"Your age in " << agey
<<" Years " <<agem <<" Months "
<<aged <<" Days " << endl;
}

Example 29:
Program to initialize a simple array
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}

Example 30:
Program to initialize an array using for loop
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 3; i++) {
cout << cars[i] << "\n";
}
return 0;
}

Example 31:
Program to initialize an array using separation
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
cars[2] = "Ford";
cars[3] = "Mazda";
cars[4] = "Tesla";
for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
return 0;
}

Example 32:
Program to initialize an array that can take 6 working hours and show all hours’
value.

#include <iostream>
using namespace std;
int main()
{
int hours[6];
cout<< "enter the hour worked by six employes";
cin>> hours [0];
cin>> hours [1];
cin>> hours [2];
cin>> hours [3];
cin>> hours [4];
cin>> hours [5];
cout << "the hours you entered are";
cout << " " << hours[0];
cout << " " << hours[1];
cout << " " << hours[2];
cout << " " << hours[3];
cout << " " << hours[4];
cout << " " << hours[5];

Example 33:
Function with parameters
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2)
{
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main()
{
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}
Example 34:
Function with parameters (Add two numbers)
#include <iostream>
using namespace std;
// declaring a function
int add(int a, int b)
{
return (a + b);
}
int main()
{
int sum;
// calling the function and storing
sum = add(100, 78);
cout << "100 + 78 = " << sum << endl;
return 0;
}
Example 35:
Function Prototype
#include <iostream>
using namespace std;
int add(int, int); // function prototype
int main()
{
int sum;
sum = add(100, 78);
cout << "100 + 78 = " << sum << endl;
return 0;
}
int add(int a, int b)
{
return (a + b);
}
Example 36:
Largest number among three numbers
#include <iostream>
using namespace std;

int main() {
int num1, num2, num3;

// Taking input from the user


cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;

int largest; // Variable to store the largest number

// Checking conditions to find the largest number


if (num1 >= num2 && num1 >= num3) {
largest = num1;
} else if (num2 >= num1 && num2 >= num3) {
largest = num2;
} else {
largest = num3;
}
// Displaying the result
cout << "The largest of the three is: " << largest << endl;

return 0;
}

Example 37:
Cube of a number
#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Cube: " << num * num * num;
return 0;
}
Example 38:
Check if Character is Uppercase or Lowercase
#include <iostream>

using namespace std;

int main() {

char ch;

cout << "Enter a character: ";

cin >> ch;

if (ch >= 'A' && ch <= 'Z')

cout << "Uppercase";

else if (ch >= 'a' && ch <= 'z')

cout << "Lowercase";

else

cout << "Not an alphabet";

return 0;

}
Example 39:
ATM Machine Program
#include <iostream>
using namespace std;
int main()
{
int choice;
float balance = 10000.0;
float amount;
while (true)
{
cout << "\n===== ATM Menu =====" << endl;
cout << "1. Check Balance" << endl;
cout << "2. Deposit Money" << endl;
cout << "3. Withdraw Money" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Your current balance is: $"
<< balance << endl;
break;
case 2:
cout << "Enter amount to deposit: $";
cin >> amount;
if (amount > 0)
{
balance = balance + amount;
cout << "Amount deposited successfully."
<< endl;
}
else
{
cout << "Invalid amount!" << endl;
}
break;
case 3:
cout << "Enter amount to withdraw: $";
cin >> amount;
if (amount > 0 && amount <= balance)
{
balance = balance - amount;
cout << "Amount withdrawn successfully."
<< endl;
} else
{
cout << "Insufficient balance or invalid amount!"
<< endl;
}
break;
case 4:
cout << "Thank you for using the ATM. Goodbye!" << endl;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}

Example 40:
Write a program to generate PAKISTAN ZINDABAD 5000 times using for loop

#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1; i<=5000; i++)
{
cout<<"PAKSTAN ZINDABAD"<<endl;
}
}

You might also like