Solutions for even numbered questions in C++ exercises for beginners

 solutions for even numbered questions in C++ exercises for beginners 

#Q2)  we can solve this problem by using arrays but for now as a beginner we solve it  by declaring five variables to store the scores of those students.

#include<iostream>

using namespace std;

int main(){

float num1,num2,num3,num4,num5,average;

cout<<"Enter the test scores respectively : "<<endl;

cin>>num1>>num2>>num3>>num4>>num5;

average=(num1+num2+num3+num4+num5)/5.0;

cout<<"the average of the given test score is "<<average<<endl;


return 0;

}

#Q4) let us do it by using if..else statments 

#include<iostream>

using namespace std;

int main(){

double  x,y,z;

  cout<<"Enter the three numbers respectively "<<endl;

cin>>x>>y>>z; // accepting input from user 

cout<<"the numer that you entered is "<<x<<" , "<<y<<" , "<<z<<endl;

  if (x>y && y>z)

         cout<<"the ascending order is "<<z<<"<"<<y<<"<"<<x<<endl;

  else if ((x>y && z>y) && x>z)

cout<<"the ascending order is "<<y<<"<"<<z<<"<"<<x<<endl;

  else if (y>z && z>x)

        cout<<"the ascending order is "<<x<<"<"<<z<<"<"<<y<<endl;

  else if ((y>z && x>z ) && y>x)

   cout<<"the ascending order is "<<z<<"<"<<x<<"<"<<y<<endl;

else if (z>x && x>y)

        cout<<"the ascending order is "<<y<<"<"<<x<<"<"<<z<<endl;

else if ((z>x && y>x ) && z>y )

           cout<<"the ascending order is "<<x<<"<"<<y<<"<"<<z<<endl;

 else cout <<"Input Blash!"<<endl;


return 0;

}

#Q6) // this is the program that dispaly the given triangle is either right angle or not 

    

    

   int side_1, side_2, side_3;

   cout<<"enter the three sides of th triangle respectively "<<endl;

   cin>>side_1>>side_2>>side_3;

   cout<<endl;

   cout<<"the sides that you entered is "<<side_1<<" ,"<<side_2<<" and "<<side_3;

   cout<<endl;

   if (side_1 + side_2<= side_3)

          cout <<"first of all it is not a triangle 😭"<<endl;

 else  if (side_3 + side_2<= side_1)

        cout <<"first of all it is not a triangle 😭"<<endl;  

  else if (side_3 + side_1<= side_2 )

           cout <<"first of all it is not a triangle 😭"<<endl;

 else   if (side_1==sqrt(pow(side_2,2) + pow(side_3, 2)))

        cout<<"it is right angle triangle with hypotunes "<<side_1<<endl;

 else   if (side_2==sqrt(pow(side_1,2) + pow(side_3, 2)))

            cout<<"it is right angle triangle with hypotunes "<<side_2<<endl;

  else     if (side_3== sqrt(pow(side_2,2) + pow(side_1, 2)))

           cout<<"it is right angle triangle with hypotunes "<<side_3<<endl;

  else cout<<"It is not right angle triangle "<<endl;

   

   return 0;

}

#Q8)  let us do by using do...while loop

#include<iostream>

using namespace std;

int main(){

int num, temp, sum; 

 cout << "Enter a positive integer: "<<endl;

 cin >> num; 

 cout << endl; 

 temp = num; 

 sum = 0;

 do { sum = sum + num % 10; // ectracting the last digit and adding it to sum

 num = num / 10; //remove the last digit }

 while (num > 0);

 cout << "The sum of the digits = " << sum << endl;

 if (sum % 9 == 0)

     cout<< temp << " is divisible by 3 and 9" << endl; 

 else if (sum % 3 == 0) 

     cout<< temp << " is divisible by 3, but not 9" << endl; 

 else 

 cout<< temp << " is not divisible by 3 or 9" << endl; 

 return 0;

}

#Q10)  use for loop

#include<iostream>

using namespace std;

int main(){

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

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

cout<<j*2<<" ";

}

cout<<endl;

}


return 0;

}


# Q12)  USE FUNCTION

#include<iostream>

using namespace std;

float larger (float x, float y){

    if(x>y)

    return x;

    return y;

    }

float copm3(float x, float y, float z){

    return larger(x,larger(y,z));

}

int main ()

{

    int x ,y,z;

    cout<<"enter three numbers respectively "<<endl;

      cin>>x>>y>>z;

    cout<<"the larger of "<<x <<" , "<<y<<" and "<<z<<" is "<<larger(x,larger(y,z))<<endl;

  return 0;

}

                                                           THANKS FOR CHOOSING US

























       


















































Comments

Popular posts from this blog

C++ sample Exams with their answers