Home | | Computer Science 11th std | Basic C++ Programs in Data Types, Variables and Expressions

Chapter: 11th Computer Science : Chapter 9 : Introduction to C++

Basic C++ Programs in Data Types, Variables and Expressions

Basic C++ Programs in Data Types, Variables and Expressions

Illustration 9.1: C++ Program to get 2 integer numbers and display their sum:

//Program to receive two integer numbers and display their sum

#include <iostream>

using namespace std; int main()

{

      int num1, num2, sum;

      //variables num1, num2, and sum are declared as integers

      cout << "\n Enter Number 1: ";

      cin >> num1;

      cout << "\n Enter Number 2: "; cin >> num2;

      sum = num1 + num2;

      cout << "\n The sum of " << num1 << " and " << num2 << " is " << sum;

}

Illustration 9.2: C++ Program to accept any character and display its next character

#include <iostream>

using namespace std;

int main()

{

      char ch;

      cout << "\n Enter a character: ";

      cin >> ch;

      ch = ch + 1;

      cout << "\n The Next character: " << ch;

}

The output produced by the program will be

Enter a character: A

The Next character: B


Illustration 9.3: C++ program to get an ASCII value and display the corresponding character

#include <iostream>

using namespace std;

int main ()

{

      int n;

      char ch;

      cout << "\n Enter an ASCII code (0 to 255): ";

      cin >> n;

      ch = n;

      cout << "\n Equivalent Character: " << ch;

}

 

The output

Enter an ASCII code (0 to 255): 100

Equivalent Character: d

Illustration 9.4: C++Program to find the area of circle

#include <iostream>

using namespace std;

int main()

{

      float r, area;

      cout << "\n Enter Radius: ";

      cin >> r;

      area = 3.14 * r * r;

      cout << "\n The Area of the circle is " << area;

}

 

Output:

Enter Radius: 6.5

The Area of the circle is 132.665

Illustration 9.5: C++ Program to find the size of data types

#include <iostream>

using namespace std;

int main()

{

      short a;

      nsigned short b;

      signed short c;

      int d;

      unsigned int e;

      signed int f;

      long g;

      unsigned long h;

      signed long i;

      char j;

      unsigned char k;

      signed char l;

      float m;

      double n;

      long double p;

      cout << "\n Size of short = " << sizeof(a);

      cout << "\n Size of unsigned short = " << sizeof(b);

      cout << "\n Size of signed short = " << sizeof (c);

      cout << "\n Size of int = " << sizeof(d);

      cout << "\n Size of unsigned int = " << sizeof(e);

      cout << "\n Size of signed int = " << sizeof(f);

      cout << "\n Size of long = " << sizeof(g);

      cout << "\n Size of unsigned long = " << sizeof(h);

      cout << "\n Size of signed long = " << sizeof(i);

      cout << "\n Size of char = " << sizeof(j);

      cout << "\n Size of unsigned char = " << sizeof(k);

      cout << "\n Size of signed char = " << sizeof(l);

      cout << "\n Size of float = " << sizeof(m);

      cout << "\n Size of double = " << sizeof(n);

      cout << "\n Size of long double = " << sizeof(p);

}

 

Output : (compiled and executed in Dev C++)

Size of short = 2

Size of unsigned short = 2

Size of signed short = 2

Size of int = 4

Size of unsigned int = 4

Size of signed int = 4

Size of long = 4

Size of unsigned long = 4

Size of signed long = 4

Size of char = 1

Size of unsigned char = 1

Size of signed char = 1

Size of float = 4

Size of double = 8

Size of long double = 12

Illustration 9.6 C++ Program to find the Curved Surface Area of a cylinder (CSA) (CSA = 2 pi r * h)

#include <iostream>

using namespace std;

int main()

{

      float pi = 3.14, radius, height, CSA;

      cout << "\n Curved Surface Area of a cylinder";

      cout << "\n Enter Radius (in cm): ";

      cin >> radius;

      cout << "\n Enter Height (in cm): ";

      cin >> height;

      CSA = (2*pi*radius)*height;

      system("cls");

      cout << "\n Radius: " << radius <<"cm";

      cout << "\n Height: " << height << "cm";

      cout << "\n Curved Surface Area of a Cylinder is " << CSA <<" sq. cm.";

}

 

Output:

Curved Surface Area of a cylinder

Enter Radius (in cm): 7

Enter Height (in cm): 20

Radius: 7cm

Height: 20cm

Curved Surface Area of a Cylinder is 879.2 sq. cm.


Illustration 9.7 C++ Program to illustrate dynamic initialization

#include <iostream>

using namespace std;

int main()

{

      int num1, num2;

      cout << "\n Enter number 1: ";

      cin >> num1;

      cout << "\n Enter number 2: ";

      cin >> num2;

      int sum = num1 + num2; // Dynamic initialization

      cout << "\n Average: " << sum /2;

} 

Output:

Enter number 1: 78

Enter number 2: 65

Average: 71

Illustration 9.8: C++ program to find the perimeter and area of a semi circle

#include <iostream>

using namespace std;

int main()

{

      int radius;

      float pi = 3.14;

      cout << "\n Enter Radius (in cm): ";

      cin >> radius;

      float perimeter = (pi+2)*radius; // dynamic initialization

      float area = (pi*radius*radius)/2; // dynamic initialization

      cout << "\n Perimeter of the semicircle is " << perimeter << " cm";

      cout << "\n Area of the semicircle is " << area << " sq.cm";

}

 

Output:

Enter Radius (in cm): 14

Perimeter of the semicircle is 71.96 cm

Area of the semicircle is 307.72 sq.cm

Illustration 9.9: C++ program to declare reference variable

#include <iostream>

using namespace std;

int main()

{

      int num;

      int &temp = num; //declaration of a reference variable temp

      num = 100;

      cout << "\n The value of num = " << num;

      cout << "\n The value of temp = " << temp;

}

 

The output of the above program will be

The value of num = 100

The value of temp = 100

Illustration 9.10: Program to Calculate Net Salary

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

      float basic, da, hra, gpf, tax, gross, np;

      char name[30];

      cout << "\n Enter Basic Pay: ";

      cin >> basic;

      cout << "\n Enter D.A : ";

      cin >> da;

      cout << "\n Enter H.R.A: ";

      cin >> hra;

      gross = basic+da+hra; // sum of basic, da nad hra

      gpf = (basic+da) * 0.10; // 10% 0f basic and da

      tax = gross * 0.10; //10% of gross pay

      np = gross - (gpf+tax); //netpay = earnings - deductions

      cout << setw(25) << "Basic Pay : " << setw(10)<< basic<< endl;

      cout << setw(25) << "Dearness Allowance : "<< setw(10)<<da<< endl;

      cout<<setw(25)<<"House Rent Allowance : "<<setw(10)<< hra<<endl;

      cout << setw(25) << "Gross Pay : " << setw(10) << gross << endl;

      cout << setw(25) << "G.P.F : " << setw(10) << gpf << endl;

      cout << setw(25) << "Income Tax : " << setw(10)<< tax << endl;

      cout << setw(25) << "Net Pay : " << setw(10) << np << endl;

}


The output will be,

Enter Basic Pay: 12000

Enter D.A : 1250

Enter H.R.A : 1450

Basic Pay : 12000

Dearness Allowance : 1250

House Rent Allowance : 1450

Gross Pay : 14700

G.P.F : 1325

Income Tax : 1470

Net Pay : 11905

(HOT: Try to make multiple output statements as a single cout statement) 

setprecision ( )

Example:

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

      float hra = 1200.123;

      cout << setprecision (5) << hra;

}

Example:

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

      cout.setf(ios::fixed);

      cout << setprecision(2)<<0.1;

}

(1) Implicit type conversion:

For example:

#include <iostream>

using namespace std;

int main()

{

      int a=6;

      float b=3.14;

      cout << a+b;

}

(2) Explicit type conversion

Example:

#include <iostream>

using namespace std;

int main( )

{

      float varf=78.685;

      cout << (int) varf;

}

Example:

#include <iostream>

using namespace std;

int main()

{

double varf=178.25255685;

cout << (float) varf << endl;

cout << (int) varf << endl;

}

 

Output:

178.253

178


1. C++ Program to find the total marks of three subjects

 

#include <iostream>

using namespace std;

int main()

{

      int m1, m2, m3, sum;

      cout << "\n Enter Mark 1: ";

      cin >> m1;

      cout << "\n Enter Mark 2: ";

      cin >> m2;

      cout << "\n Enter Mark 3: ";

      cin >> m3;

      sum = m1 + m2 + m3;

      cout << "\n The sum = " << sum;

}

Make changes in the above code to get the average of all the given marks.

 

2. C++ program to find the area of a circle

 

#include <iostream>

using namespace std;

int main()

{

      int radius;

      float area;

      cout << "\n Enter Radius: ";

      cin >> radius;

      area = 3.14 * radius * radius;

      cout << "\n The area of circle = " << area;

}

 

3. point out the errors in the following program:

#include <iostream>

Using namespace std;

int main( )

{

      cout << “Enter a value ”;

     cin << num1 >> num2

      num+num2=sum;

      cout >> “\n The Sum= ” >> sum;

}

 

4. point out the type of error in the following program:

#include <iostream>

using namespace std;

int main()

{

      int h=10; w=12;

      cout << "Area of rectangle " << h+w;

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 9 : Introduction to C++ : Basic C++ Programs in Data Types, Variables and Expressions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.