All important Programs of C++ Language

 

Programs of Cpp-Language 

In This Article, You will find out the Important programs in Cpp-Language. That will help you to perform accurately in your Paper.

Before going towards programs let us know what Cpp-Language is?


C++ language

Stroustrup's C++ built upon the C programming language, developed by Dennis Ritchie at Bell Labs. Bjarne Stroustrup joined the 1127 Computing Science Research Center of AT&T Bell Laboratories in 1979. 




Program # 1

Hello World!

#include <iostream>
using namespace std;
int main()
{
      cout<<"Hello World!";
Return 0;

}

Output
Hello World! 


Program # 2

Sum of Characters

#include <iostream>
using namespace std;
int main()
{
       char  ch1ch2sum;
ch1 = '2';
ch2 = '6';
sum=ch1+ch2;
      cout<<"Sum =  "<< sum;
Return 0;

}

Output
Sum = 104

Explanation
The sum of character '2' and character '6' is equal to 104 because, The ASCII code for character '2 is 50, and character '6' is 54.



Program # 3

Use of Comments

#include <iostream>
using namespace std;
int main()
{
/*
            Name: Hamza Asif
            Date: 20/06/22
*/
      cout<<"This is My First Program.";
Return 0;
}

Output
This is My First Program.

Explanation
Symbols (/*) and (*/) are used to comment multi-line comments and (//) symbol is used to print single-line comments.



Program # 4

Use of Increment Operator

#include <iostream>
using namespace std;
int main()
{

       Int IjkL;
       = 10;
       = ++j;
       = 10;
       k++;     
       cout<<"is  \n"<< i;
       cout<<"is  \n"<<j;
       cout<<"is \n"<<k;
       cout<<"is  "<<L;

Return 0;
}

Output
I is 11
j is 11
k is 11
L is 10



Program # 5

Print Area of Square

#include <iostream>
using namespace std;
int main()
{
       int  heightwidtharea;
height = 4;
width = 4;
areaheight width;
      cout<<"Area of square =  "<< area;
Return 0;

}

Output
Area of Square = 16



Program # 6

Sum of two floating numbers

#include <iostream>
using namespace std;
int main()
{
        float num1num2sum;
num1 = 24.27;
num2 = 41.50;
      cout<<"Sum =  " sum;

Return 0;

}

Output
Sum = 65.77

Explanation       
We have initialized three variables num1, num2, and sum in float data type. cout<< statement showed the output in decimals(float data type).



Program # 7

Use of Escape sequence

#include <iostream>
using namespace std;
int main()
{
cout<<"\n new line escape sequence tutorial";
cout<<"\t 15 \t 400";
cout<<"\n Second line \n";
Return 0;

}

Output
__
new line escape sequence tutorial    15    400
Second line
--



Program # 8

Convert Kilometers into Meters

#include <iostream>
using namespace std;
int main()
{
        double meterkilometer;
        cout<<"Please enter the distance in Kilometers>";
       cin>>kilometer;
       meter kilometer * 1000
       cout<<"Given Kms in meters =  " <<meter;

Return 0;

}

Output
Please enter the distance in kilometers>45
Given Kms in meters = 45000.000000



Program # 9

Maximum between 3 numbers

#include <iostream>
using namespace std;
int main()
{
        int ab, c;
        cout<<"Please enter three numbers\n";
        cin>>a>>b>>c;
       if(ab && ac)
               {      
                    cout<<a<<" is the maximum number";
                } else  if (c &&  b>a)
                        {
                             cout<<b<<" is the maximum number";
                         }  else if (b < c && ac
                                  {
                                      cout<<c<<" is the maximum number";
                                  else 
                                       {
                                        cout<<" Invalid Input";
                                        }    
Return 0;          
}

Output
Since we have to take input from the user therefore we have not written any output.



Program # 10


The number is negative, positive, or zero.

#include <iostream>
using namespace std;
int main()
{
        int a;
        cout<<"Please enter a number\n";
       cin>>a;
       if(a == 0)
               {      
                   cout<<a<< " is Zero";
                } else  if (a > 0)
                        {
                              cout<<a<<" is the positive number";
                         }  else if (a < 0
                                  {
                                      cout<<a<<" is the negative number";
                                  else 
                                       {
                                       cout<<"Please Enter the Number";
                                        }       
return 0;       
}


Output
Since we have to take input from the user therefore we have not written any output.



Program # 11

Number is Even or Odd.

#include <iostream>
using namespace std;
int main()
{
        int num;
       cout<<"Please enter a numbers\n";
       cin>>num;
       if(num % 2 == 0)
               {      
                  cout<<num<<" is an Even number";
                } else  if (! num  % 2 == 0)
                        {
                           cout<<num<<" is an Od number";
                         }   else 
                                       {
                                        cout<<"Please Enter the valid Number";
                                        }  
return 0;            
}


Output
Since we have to take input from the user therefore we have not written any output.



Program # 12

Year is leap or not

#include <iostream>
using namespace std;
int main()
{
        int year;
        cout<<"Please enter a year\n";
       cin>>year;
       if(year % 4 == 0)
               {      
                   cout<<year<<" is leap year";
                }   else 
                           {
                                cout<<year<<" is not a leap year";
                           }      
Return 0;        
}


Output
Since we have to take input from the user therefore we have not written any output.


Program # 13

Find Factorial of any Number

#include <iostream>
using namespace std;
int main()
{
        long float a=1f=2n;
     cout<<"Enter any Number\n";
       cin>>n;
while(a <= n)
{
ff*a;
a++;
cout<<"Factorial=  "<<f;

Return 0;
}

Output
Since we have to take input from the user therefore we have not written any output.

Program # 14

Enter the day number to print the day name

#include <iostream>
using namespace std;
int main()
{
        int day;
        cout<<"Please Enter day Number\n";
       cin>>day;
       if(day == 1)
               {      
                    cout<<"The day is Monday";
                } else  if (day == 2)
                        {
                             cout<<"The day is Tuesday";
                         }   else  if (day == 3)
                                       
                                             cout<<"The day is Wednesday";
                                       } 
            else  if (day == 4)
               {
                      cout<<"The day is Thursday";
                
 else  if (day == 5)
   {
      cout<<"The day is Friday";
    else  if (day == 6)
              {
                   cout<<"The day is Saturday";
               }         
                    else  if (day == 7)
                        {
                           cout<<"The day is Sunday";
                              
             else 
               {
                 cout<<"Please enter Valid day Number";  
               }
Return 0;
}

Output
Since we have to take input from the user therefore we have not written any output.

Program # 15

Input n students & Marks and display pass and fail

#include <climits>
#include <iostream>
using namespace std;

int main()
{
// int maxNum=INT_MIN;
//int minNum=INT_MAX;
int maxNum=0;
int minNum=1100;
int n,m;

string student[n],maxNums,minNums;

    cout<<"Please enter the total numbers of students: ";
    cin>>n;
    m=n;
    int marks[m],Total_marks,percentage;
    cout<<"\nPlease enter the Total Marks: ";
    cin>>Total_marks;

    
    for(int i=0;i<n;i++)
    {
    cout<<"\nPlease enter Students name: ";
    cin>>student[n];
       
   
     
    cout<<"\nObtained marks: \n";
    cin>>marks[m];
    percentage= marks[m]*0.1;
    if(percentage>33)
      {
          cout<<"Student: "<<student[n]<<" has passed the exams with marks: "<<marks[m];
      } else {
          cout<<"Student: "<<student[n]<<" has failed the exams with marks: "<<marks[m];
      }       

     for(int i =0;i<=n;i++)
     {
         if(marks[m]>maxNum)
         {
             maxNum=marks[m];
             maxNums= student[n];
                }  if(marks[m]< minNum)
             {
                 minNum=marks[m];
                 minNums=student[n];
             }
     }} cout<<"\nThe Highest Marks are: "<<maxNum<<" of Student: "<<maxNums;
  cout<<"\nThe Lowest Marks are: "<<minNum<<" of Student: "<<minNums;

    return 0;
}

   


Practice Statements:

1. Write a C++ program to perform mathematical expression: (((-4)*(i++))-(6%4)) where i = -2 and display the output on screen.

2. Write a C++ program to swap the values of two numbers entered by the user. OR Write a C++ to swap the value of a and b, where a=10 and b=25.

3. Write a C++ program to display the following output on the screen:

     *
  *     *
*         *
  *     *
     *
with help of "\t" and "\n".

4. Write a C++ program using the 'backspace' escape sequence to display the word 'Hello Geeks' from the Word 'Hello Geeks'.

5. Write a C++ program to display your mobile number with '/a' sequence.

6. Write a C++ program to Display 'Hello endsfri' with the string "Hello Fri\rends".

7. Write a C++ program to calculate the distance between two points in x and y coordinates.

8. Write a C++ program to calculate the roots of a quadratic equations.

9. Write a C++ program that takes a salary as input and calculates and display :
Dearness allowance = 28%
Medical allowance = 17%
House rent allowance = 48%
and Gross salary.

10. Write a C++ program to convert 2.5 miles into kilometers. (Hint: 1 mile = 1.609 km).

11. Write a C++ program to display counting from 1 to 100 using a while loop.

12. Write a C++ program to display numbers from 1 to 100 and display their sum.

13. Write a C++ program to display even and odd numbers from 1 to 1000 and also display the sum of even and odd numbers separately.

14.  Write a C++ program to display even and odd numbers between the starting and ending point entered by the user and display the sum of even and odd numbers separately.


15. Identify the output of the program:

#include <iostream>
using namespace std;
int main()
{
for(int j = 10; j>=0; j--)
    {
      for(int i =0 ; i<=j; i++)
         {
           cout<<i;
         }
      cout<<endl;   
    }
return 0;
}


16. Write a C++ program that takes age as input and display that user can vote or not. (age>18) can vote.

17. Write a C++ program that take 3 numbers input and display the greater number on screen.

18. Write a C++ program to check whether a number entered by user is even or odd.

19. Write a C++ program that take number as input from user and display it is multiple of 5 or not.

20. Write a C++ program to display the grade of students by taking marks as input. 
conditions: 
(90<= marks && marks <=100) than Grade A
(80<= marks && marks <90) than Grade B
(70<= marks && marks <80) than Grade C
(60<= marks && marks <70) than Grade D
(50<= marks && marks <60) than Grade E
(marks < 50) Fail

21. Write a C++ program to check whether the alphabet entered by use is vowel or consonant.

22. Write a C++ program to Display the week day by day number entered by user.

23. Write a C++ program to perform addition, subtraction, multiplication and division.

Post a Comment

0 Comments