Computer Science, asked by dakshitavarma907, 8 months ago

write a c++ programme to print tge reasult is pass or fail using if-else​

Answers

Answered by savitawadhwa
0

if statements in C++ Programming

While writing computer programs, we need to check certain condition to instruct the program to branch accordingly. Like most of the programming languages, C++ has if statement to check the condition and make decision. Based on the number of conditions to be checked, we have different types of if statement. They are

if statement

if ... else statement

if ... else if ... if statement

nested if statement

if statement

if statement is a conditional statement which is used to make decision. It is used when a single condition is to be checked. A condition is enclosed in if statement which decides the sequence of execution of instruction. If the condition is true, the statements inside if statement are executed, otherwise they are skipped. 

Syntax of if statement

if (condition) {   statements;   ... ... ... }

Flowchart for if statement

Example of if statement

C++ program to check percentage of a student and display pass if it is greater than 40.

#include <iostream> #include <conio.h> using namespace std; int main() { float percent; cout<<"Enter your percentage: "; cin>>percent; cout<<"You scored "<<percent<<"%"<<endl; if (percent>=40) { cout<<"Congratulation: You have passed"; } getch(); return 0; }

In this program, the percentage of a student is entered by user and a message saying how much the student scored is displayed. If the student has scored more than or equal to 40%, he/ she is passed and a congratulation message is displayed on the output screen.

Output

Enter your percentage: 71 You scored 71% Congratulation: You have passedEnter your percentage: 34 You scored 34%

if ... else statement

if ... else statement is a two way branching statement. It is similar to if statement but the only difference is if the condition is false then a different block of statements are executed which is inside else statement.

Syntax of if...else statement

if (condition) { statements;   ... ... ... } else { statements;   ... ... ... }

Flowchart for if ... else statement

Example of if ... else statement

C++ program to check percentage of a student and display pass or fail.

#include <iostream> #include <conio.h> using namespace std; int main() { float percent;   cout<<"Enter your percentage: ";   cin>>percent;   cout<<"You scored "<<percent<<"%"<<endl;   if (percent>=40)   {   cout<<"Congratulation: You have passed";   }   else {   cout<<"Sorry: You have failed";   }   getch();   return 0; }

This program asks the percentage of student. If the percentage is equal to or greater than 40 then a congratulation message saying you have passed is displayed. Otherwise, i.e if percentage is below 40 then a sorry message is printed on the output screen.

Output

Enter your percentage: 86 You scored 86% Congratulation: You have passedEnter your percentage: 37 You scored 37% Sorry: You have failed

if ... else if ... else statement

if ... else if ... else statement is used for multiple branching. When there are two or more conditions that needs to be checked to decide which block of statement is to be executed, it is used. The number of else ifstatements depends upon the number of conditions to be checked.

Syntax of if...else if...else statement

if (condition 1) { statements;   ... ... ... } else if (condition 2) { statements;   ... ... ... } ... ... ... ... ... ... else if (condition n) { statements;   ... ... ... } else { statements;   ... ... ... }

Example of if ... else if ... else statement

C++ program to check percentage of a student and display division(distinction, first, second, third or fail).

#include <iostream> #include <conio.h> using namespace std; int main() { float percent;   cout<<"Enter your percentage: ";   cin>>percent;   cout<<"You scored "<<percent<<"%"<<endl;   if (percent>=80)   {   cout<<"You have passed with distinction";   }   else if (percent>=60)   {   cout<<"You have passed with first division";   }   else if (percent>=50)   {   cout<<"You have passed with second division";   }   else if (percent>=40)   {   cout<<"You have passed with third division";   } else {   cout<<"Sorry: You have failed";   }   getch();   return 0; }

This program asks the percentage of a student and display which division he/she has got. The criteria for division is displayed below:

   Percentage     Division>=80Distinction>=60 and <80First division>=50 and <60Second Division>=40 and <50Third Division<40Fail

According to the condition, the result is displayed.

Output

Enter your percentage: 87.67 You scored 87.67% You have passed with distinctionEnter your percentage: 34.50 You scored 34.5% Sorry: You have failedEnter your percentage: 45.83 You scored 45.83% You have passed .

Similar questions