0% found this document useful (0 votes)
10 views

C++ Computer Program Gode

The document contains code snippets demonstrating different ways to calculate grades using C++. It shows using nested if-else statements, switch case statements, a for loop, and printing patterns using nested for loops. The code examples calculate grades based on marks input by the user and output the total, percentage, and grade.

Uploaded by

fa22bese0044
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C++ Computer Program Gode

The document contains code snippets demonstrating different ways to calculate grades using C++. It shows using nested if-else statements, switch case statements, a for loop, and printing patterns using nested for loops. The code examples calculate grades based on marks input by the user and output the total, percentage, and grade.

Uploaded by

fa22bese0044
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

// C++ Program to Calculate Sum of 5 Subjects and Find Percentage and

Grade NESTED IF-ELSE


#include <bits/stdc++.h>
using namespace std;

int main() {
// To store the values of five subjects
float sub_1, sub_2, sub_3, sub_4, sub_5;

float total = 0.00, average = 0.00, percentage = 0.00;


char grade;

cout << "Enter the marks of five subjects::\n";


cin >> sub_1 >> sub_2 >> sub_3 >> sub_4 >> sub_5;

// It will calculate the Total, Average and Percentage


total = sub_1 + sub_2 + sub_3 + sub_4 + sub_5;
average = total / 5.0;
percentage = (total / 500.0) * 100;

// It will calculate the Grade


if (average >= 90)
grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';

// It will produce the final output


cout << "\nThe Total marks = " << total << "/500\n";
cout << "The Average marks = " << average << "\n";
cout << "The Percentage = " << percentage << "%\n";
cout << "The Grade = '" << grade << "'\n";
return 0;
}
SWITCH CASE

# include <iostream>

# include <conio.h>

using namespace std;

int main()

int s1,s2,s3,s4,s5;

float per;

char grade;

cout<<"enter marks of five subjects"<<endl;

cin>>s1>>s2>>s3>>s4>>s5;

per=((s1+s2+s3+s4+s5)*100)/500;

cout<<per<<endl;

switch (per>=90&&per<100)

case 1:

cout<<"your grade is A";

break;

case 0:

switch(per>=80 &&per<90)

case 1:

cout<<"grade is B";

break;

case 0:

switch(per>=70&&per<80)

case 1:

cout<<"your grade is C";


break;

case 0:

switch (per>=60&&per<70)

case 1:

cout<<"grade is D";

break;

case 0:

switch (per>=50&&per<60)

case 1:

cout<<"grade is E";

break;

case 0:

cout<<"grade is F";

break;

break;

break;

break;

break;

}
USING FOR LOOP
#include <iostream>
using namespace std;

int main() {
int score, i, average;
float total=0;

cout<< "Enter marks of 5 subjects\n";

for(i=0; i<5; i++) {


cin >> score;
total += score;
}

average = total/5;

cout<<"Grade : ";
switch(average/10) {
case 9 :
cout << "A";
break;
case 8 :
case 7 :
cout << "B";
break;
case 6 :
case 5 :
cout << "C";
break;
default :
cout << "D";
}

return 0;
}
#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,

#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;
}
Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Example,

#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;
}

Output

* * * * *
* * * *
* * *
* *
*
Example,

#include <iostream>
using namespace std;

int main() {

int space, rows;

cout <<"Enter number of rows: ";


cin >> rows;

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


for(space = 1; space <= rows-i; ++space) {
cout <<" ";
}

while(k != 2*i-1) {
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}

Output

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Example,
#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i=1; i<=10; i=+3)
{
for(j=1; j<=i; j++)
cout<<"* ";
cout<<endl;
}
cout<<endl;
return 0;
}
Output:

You might also like