C Programming Assignment 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

C PROGRAMMING ASSIGNMENT #2

Q1) USE ELSE IF LADDER AND WRITE A C PROGRAM TO INPUT MARKS OF FIVE SUBJECTS
PHYSICS, CHEMISTRY, BIOLOGY,MATHEMATICS AND COMPUTER. CALCULATE
PERCENTAGE AND GRADE ACCORDING TO FOLLOWING:

PERCENTAGE >= 90%: GRADE A


PERCENTAGE >= 80%: GRADE B
PERCENTAGE >= 70%: GRADE C
PERCENTAGE >= 60%: GRADE D
PERCENTAGE >= 40%: GRADE E
PERCENTAGE < 40%: GRADE F

CODE:
#include <stdio.h>

int main() {

float physics, chemistry, biology, mathematics, computer;

float total_marks = 500; // Total marks for all subjects

float percentage;

char grade;

char remarks[50];

// Input marks for each subject


printf("Enter marks for Physics: ");

scanf("%f", &physics);

printf("Enter marks for Chemistry: ");

scanf("%f", &chemistry);

printf("Enter marks for Biology: ");

scanf("%f", &biology);

printf("Enter marks for Mathematics: ");

scanf("%f", &mathematics);

printf("Enter marks for Computer: ");

scanf("%f", &computer);

// Calculate percentage

percentage = (physics + chemistry + biology + mathematics + computer) / total_marks * 100;

// Determine grade and remarks based on percentage

if (percentage >= 90) {

grade = 'A';

sprintf(remarks, "Congratulations!");

} else if (percentage >= 80) {

grade = 'B';

sprintf(remarks, "Well done!");

} else if (percentage >= 70) {

grade = 'C';

sprintf(remarks, "Come on, you can do it!");

} else if (percentage >= 60) {

grade = 'D';

sprintf(remarks, "It's not the worst. Better luck next time!");

} else if (percentage >= 40) {

grade = 'E';
sprintf(remarks, "It's not the worst!");

} else {

grade = 'F';

sprintf(remarks, "I’m so sorry but you’ve failed!");

// Print grade, percentage, and remarks

printf("Percentage: %.2f%%\n", percentage);

printf("Grade: %c\n", grade);

printf("Remarks: %s\n", remarks);

return 0;

EXPLANATION:

The given program in C language prompts the user to input the marks obtained in five subjects - Physics,
Chemistry, Biology, Mathematics, and Computer. It then calculates the total percentage of the marks obtained
and assigns a grade based on the percentage. The grade is assigned based on the following criteria:

 Percentage >= 90% : Grade A

 Percentage >= 80% : Grade B

 Percentage >= 70% : Grade C

 Percentage >= 60% : Grade D

 Percentage >= 40% : Grade E

 Percentage < 40% : Grade F

The program then prints the percentage and grade obtained by the user on the console. Additionally, the
program has been modified to print remarks based on the grade obtained. The congratulatory remarks are as
follows:

 Grade A: "Congratulations!"

 Grade B: "Well done!"

 Grade C: "Come on, you can do it!"

 Grade D: "Don't give up!"

 Grade E: "It's not the worst. Better luck next time!"


 Grade F: "I’m so sorry but you’ve failed!"

The program uses if-else if ladder to determine the grade and remarks based on the percentage. It also uses
the sprintf function to format the remarks string based on the grade. Finally, the program prints the
percentage, grade, and remarks using printf function with appropriate format specifiers.

Overall, the program allows the user to input the marks obtained in five subjects, calculates the total
percentage and grade, and gives remarks based on the grade obtained. Here's the algorithm for the program:

1. Start the program

2. Declare and initialize variables to store marks in Physics, Chemistry, Biology, Mathematics, and
Computer, and total marks for all subjects

3. Prompt the user to input the marks for each subject

4. Calculate the total percentage of the marks obtained

5. Determine the grade based on the percentage as follows:

a. If percentage >= 90, assign grade A

b. Else if percentage >= 80, assign grade B

c. Else if percentage >= 70, assign grade C

d. Else if percentage >= 60, assign grade D

e. Else if percentage >= 40, assign grade E

f. Else assign grade F

6. Determine the remarks based on the grade as follows:

a. If grade A, assign "Congratulations!"

b. Else if grade B, assign "Well done!"

c. Else if grade C, assign "Come on, you can do it!"

d. Else if grade D, assign "Don't give up!"

e. Else if grade E, assign "It's not the worst!"

f. Else assign "Fail"

7. Print the percentage, grade, and remarks on the console

8. End the program

OUTPUT:
Q2) USE THE CONCEPT OF NESTED FOR LOOP TO MAKE A PATTERN LIKE A PYRAMID
WITH NUMBERS INCREASED BY 1.

1
23
456
7 8 9 10

CODE:
#include <stdio.h>

int main() {

int rows, i, j, num = 1;

// Input the number of rows for the pyramid pattern

printf("Enter the number of rows for the pyramid pattern: ");

scanf("%d", &rows);

// Loop through each row and column to create the pattern

for (i = 1; i <= rows; i++) {

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

printf("%d ", num);

num++;

}
printf("\n");

return 0;

EXPLANATION:

This program creates a pattern that looks like a pyramid with numbers increasing by 1.

First, the user is asked to input the number of rows for the pattern. Then, the program uses two
nested for loops to create the pattern. The outer loop iterates through each row of the pattern,
and the inner loop prints the numbers on each row.

The program uses a variable ‘num’ to keep track of the current number to be printed on the
pattern. This variable is initialized to 1 and incremented after each number is printed. The inner
loop runs ‘1’ times for each row, printing the numbers in increasing order.

Finally, the program moves to the next line after printing each row, using the ‘ printf("\d”)’
statement.

When the program is executed with an input of 4 rows, it will output the following pattern:

23

456

7 8 9 10

This pattern has numbers increasing by 1 on each row, creating a pyramid-like structure. The
program can be modified to change the number of rows and/or the starting number of the
pattern if desired.

Here's the algorithm for creating a pyramid pattern with numbers increasing by 1:

1. Start

2. Input the number of rows for the pyramid pattern

3. Initialize a variable num to 1

4. For i = 1 to rows do the following steps


i. For j = 1 to i do the following steps a. Print the value of num b. Increment num
by 1

ii. Print a newline character to move to the next row

5. End

In this algorithm, we first ask the user to input the number of rows for the pyramid pattern. We
then initialize a variable ‘num’ to 1, which we will use to keep track of the current number to be
printed on the pattern.

We then use two nested loops to create the pattern. The outer loop iterates through each row of
the pattern, and the inner loop prints the numbers on each row. For each row, we start the inner
loop at 1 and iterate up to the value of ‘I‘. Within the inner loop, we print the value of ‘num’,
which starts at 1 and increments by 1 after each number is printed.

Finally, after printing all the numbers on a row, we move to the next row by printing a newline
character. We repeat this process for each row of the pyramid pattern.

Once we have printed the entire pattern, we end the algorithm.

This algorithm can be implemented in any programming language that supports loops and
input/output functions.

OUTPUT:

You might also like