Professional Documents
Culture Documents
C Programming Assignment 2
C Programming Assignment 2
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:
CODE:
#include <stdio.h>
int main() {
float percentage;
char grade;
char remarks[50];
scanf("%f", &physics);
scanf("%f", &chemistry);
scanf("%f", &biology);
scanf("%f", &mathematics);
scanf("%f", &computer);
// Calculate percentage
grade = 'A';
sprintf(remarks, "Congratulations!");
grade = 'B';
grade = 'C';
grade = 'D';
grade = 'E';
sprintf(remarks, "It's not the worst!");
} else {
grade = 'F';
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:
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!"
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:
2. Declare and initialize variables to store marks in Physics, Chemistry, Biology, Mathematics, and
Computer, and total marks for all subjects
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() {
scanf("%d", &rows);
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
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.
This algorithm can be implemented in any programming language that supports loops and
input/output functions.
OUTPUT: