SKC

 

LAB:1

a. Write a C program to print Hello World.

     i. Problem Solving Logic – None

 ii. Algorithm Step 1. Start the program execution. 

Step 2. Include the necessary header file (stdio.h) for input and output operations. 

Step 3. Declare the main function, which serves as the entry point for the program.

 Step 4. Inside the main function: a) Use the printf function to print the string "Hello World" followed by a newline character (\n) to the console. b) Return 0 to indicate successful program execution.

 Step 5. End the program.



b. Write a C program to calculate the area of a circle

i. Problem Solving Logic a) Input the radius: Prompt the user to input the radius of the circle.

 b) Calculate the area: Once you have the radius, use the formula for the area of a circle, which is π * r^2, where r is the radius and π is a constant value.

 c) Output the result: Display the calculated area.

 ii. Algorithm Step 1. Start: Begin the program. 

Step 2. Prompt User: Ask the user to input the radius. 

Step 3. Input Radius: Get the radius value from the user.

 Step 4. Calculate Area: a) Multiply the radius by itself (square it). b) Multiply the result by the constant value of PI (approximately 3.14159). 

Step 5. Display Result: Show the calculated area along with the provided radius. 

Step 6. End: Conclude the program.

c. Write a C program to experiment on escape sequences.

Problem Solving Logic a) Print single quote, double quote, question mark, and backslash: Use printf to display the characters \', \", \?, and \\ using escape sequences.

 b) Print backspace, new page, newline, carriage return, horizontal tab, and vertical tab: Demonstrate the effect of escape sequences like \b, \f, \n, \r, \t, and \v by printing them in specific combinations

 c) Return 0 to indicate successful execution: The return 0; statement at the end of the main function signals that the program executed successfully. 

ii. Algorithm Step 1. Include necessary header file: Include the standard input-output library (#include ).

 Step 2. Define the main function: Start the program execution by defining the main function. 

Step 3. Print single quote, double quote, question mark, and backslash: Use printf to display the characters \', \", \?, and \\ using escape sequences. 

Step 4. Print backspace, new page, newline, carriage return, horizontal tab, and vertical tab: Demonstrate the effect of escape sequences like \b, \f, \n, \r, \t, and \v by printing them in specific combinations. 

Step 5. Return 0 to indicate successful execution: Conclude the main function by returning 0, indicating successful program execution. 

Step 6. End of algorithm: End the algorithm, as the program logic is complete.




LAB:2

a. Write a C program to test if a number is zero, positive or negative

i. Problem Solving Logic a) Check for zero: Use an if statement to check if the entered number is equal to zero. If true, print a message indicating that the number is zero.

 b) Check for positivity: If the number is not zero, use an else if statement to check if the number is greater than zero. If true, print a message indicating that the number is positive. 

c) Check for negativity: If the number is not zero and not positive, use the else statement to conclude that the number is negative. Print a message indicating that the number is negative.

 ii. Algorithm Step 1. Start: Begin the program execution. 

Step 2. Input: Declare an integer variable to store the input number. Prompt the user to enter a number. Read and store the entered number in the declared variable. 

Step 3. Check for Zero: If the entered number is equal to zero: Print "The number is zero." Else, proceed to the next step. 

Step 4. Check for Positivity: If the entered number is greater than zero: Print "The number is positive." Else, proceed to the next step. 

Step 5. Check for Negativity: If the entered number is not zero and not positive: Print "The number is negative." 

Step 6. End: Exit the program. 


b. Write a C program to find minimum of three numbers.

Problem Solving Logic a) Initialize a variable to the first number: Inside the findMinimum function, a variable min is initialized with the value of the first number (num1). 

b) Check if the second number is smaller: Using conditional statements (if), the program checks if the second number (num2) is smaller than the current minimum (min). If true, update min with the value of num2. 

c) Check if the third number is smaller: Similarly, the program checks if the third number (num3) is smaller than the current minimum (min). If true, update min with the value of num3.

 d) Return the minimum value: The findMinimum function returns the final minimum value determined by the comparisons.

Algorithm Step 1. Start: Begin the program execution. 

Step 2. Input: Prompt the user to enter three integers: num1, num2, and num3. 

Step 3. Function Definition: Define a function findMinimum that takes three integers (num1, num2, num3) as parameters. 

Step 4. Initialization: Inside the findMinimum function, initialize a variable min with the value of the first number (num1). 

Step 5. Comparison (1): Check if the second number (num2) is less than the current minimum (min). If true, update min with the value of num2. 

Step 6. Comparison (2): Check if the third number (num3) is less than the current minimum (min). If true, update min with the value of num3. 

Step 7. Return Minimum: Return the final minimum value (min) from the findMinimum function. 

Step 8. Call Function: In the main function, call the findMinimum function with userinput numbers as arguments. Store the result in a variable called minimum. 

Step 9. Output: Print the minimum value along with the original three numbers.

 Step 10. End: End the program.

c. Write a C program to display the roots of a quadratic equation.

Problem Solving Logic a) Calculate Discriminant: Compute the discriminant using the formula: discriminant = b^2 - 4ac. 
b) Check Discriminant: ➢ If discriminant > 0, there are two real and distinct roots. ➢ If discriminant = 0, there is one real root (double root). ➢ If discriminant < 0, there are complex roots.

 c) Compute Roots: ➢ For discriminant > 0, calculate two real roots using the quadratic formula. ➢ For discriminant = 0, calculate the single real root. ➢ For discriminant < 0, calculate complex roots with real and imaginary parts. 

ii. Algorithm Step 1. Input Coefficients: Prompt the user to input the coefficients a, b, and c of the quadratic equation. 

Step 2. Calculate Discriminant: Compute the discriminant using the formula: discriminant = b^2 - 4ac. 

Step 3. Check Discriminant: 
➢ If discriminant is greater than 0: Calculate two distinct real roots using the quadratic formula.
 ➢ If discriminant is equal to 0: Calculate a single real root (double root) using the quadratic formula. ➢ If discriminant is less than 0: Calculate complex roots with real and imaginary parts using the quadratic formula.

Step 4. Display Roots: Print the roots based on the nature of the discriminant
: ➢ For two real roots: display both roots.
 ➢ For one real root: display the single root. 
➢ For complex roots: display both roots in the form "real + imaginary i" and "real - imaginary i".

OUTPUT:

LAB:3
a. Write a C program which uses the formula C= (5/9)*(F-32) to print a table of Fahrenheit temperatures and their centigrade or Celsius equivalents from 1 to 300 Fahrenheit temperature.

i. Problem Solving Logic
 a) Define Variables: 
➢ fahrenheit: This variable is used to store the Fahrenheit temperature values from 1 to 300. 
➢ celsius: This variable is used to store the corresponding Celsius equivalents.

 b) Calculate Celsius Equivalents in a Loop:
 ➢ A for loop is used to iterate through Fahrenheit temperatures from 1 to 300.
 ➢ Inside the loop, the Celsius equivalent for the current Fahrenheit temperature is calculated using the provided formula: Celsius = (5/9) * (Fahrenheit - 32). 
➢ The calculated Celsius value is then printed along with the corresponding Fahrenheit value in a tabular format.

 c) Repeat Until 300 Fahrenheit: 
➢ The loop continues until the value of fahrenheit reaches 300.

ii. Algorithm Step 
1. Start.

 Step 2. Include the necessary header file stdio.h. 

Step 3. Declare the following variables: 
➢ fahrenheit (integer): to store the Fahrenheit temperature.
 ➢ celsius (float): to store the Celsius equivalent of the Fahrenheit temperature. 

Step 4. Print the table header: 
➢ Print "Fahrenheit" and "Celsius" with appropriate spacing to format the table. 
➢ Print a line of dashes for visual separation. 

Step 5. Start a for loop with fahrenheit initialized to 1, and loop until fahrenheit reaches 300, incrementing by 1 in each iteration. 

Step 6. Inside the loop:
 ➢ Calculate celsius using the formula celsius = (5.0 / 9.0) * (fahrenheit - 32). 
➢ Print the current fahrenheit value followed by a tab ("\t") for spacing, then print the corresponding celsius value. Step 7. End the loop. 

O/P:


b. Write a C program to perform matrix addition and multiplication .

i. Problem Solving Logic a) Matrix Addition: 

➢ The program checks if the dimensions of both matrices are the same.

 ➢ If they are, it proceeds to add the matrices element-wise using nested loops.

 ➢ The result is stored in another matrix. 

b) Matrix Multiplication: ➢ The program checks if the number of columns of the first matrix is equal to the number of rows of the second matrix.

➢ If they are, it proceeds to multiply the matrices using nested loops. 

➢ It iterates over each element of the resulting matrix and computes the dot product of the corresponding row of the first matrix and the corresponding column of the second matrix.

 ii. Algorithm Step 1. Start. 

Step 2. Declare constant MAX_SIZE for the maximum size of matrices. 

Step 3. Declare functions for matrix addition, matrix multiplication, and displaying matrices.

 Step 4. Define the add_matrices function: 

➢ Accept two matrices (mat1 and mat2), their dimensions (rows and cols), and a result matrix (result). ➢ Iterate through each element of the matrices using nested loops.

 ➢ Add corresponding elements of mat1 and mat2 and store the result in the corresponding element of result. 

Step 5. Define the multiply_matrices function:

 ➢ Accept two matrices (mat1 and mat2), their dimensions (rows1, cols1, and cols2), and a result matrix (result). 

➢ Iterate through each element of the resulting matrix (result) using nested loops. 

➢ For each element result[i][j], iterate through the columns of mat1 or rows of mat2.

 ➢ Calculate the dot product of the ith row of mat1 and the jth column of mat2, and store the result in result[i][j]. 

Step 6. Define the display_matrix function: 

➢ Accept a matrix (mat), its dimensions (rows and cols).

 ➢ Iterate through each element of the matrix and print its value. 

Step 7. In the main function: ➢ Declare variables for matrices mat1, mat2, and result, and variables for matrix dimensions (rows1, cols1, rows2, cols2). 

➢ Prompt the user to input the dimensions of matrix 1 (rows1 and cols1).

 ➢ Prompt the user to input the elements of matrix 1 and store them in mat1. 

➢ Prompt the user to input the dimensions of matrix 2 (rows2 and cols2). 

➢ Prompt the user to input the elements of matrix 2 and store them in mat2. 

➢ Check if matrices can be added or multiplied based on their dimensions: 

➢ If the dimensions are incompatible, print an error message and exit. 

➢ If the dimensions are compatible, proceed with addition and multiplication. 

Step 8. Print the input matrices. 

Step 9. Perform matrix addition and display the result. 

Step 10. Perform matrix multiplication and display the result.

 Step 11. End.




O/P:

LAB:4

a) Write a C program to determine the type of a triangle using functions

i. Problem Solving Logic

 a) Check Triangle Validity: 

➢ Check if the given side lengths form a valid triangle.

 ➢ For a triangle to be valid, the sum of the lengths of any two sides must be greater than the length of the third side. 

➢ If the condition is not met, print a message indicating that the given sides do not form a triangle and exit the program. 

b) Determine Triangle Type: 

➢ If all three sides are equal, print that it is an equilateral triangle. 

➢ If at least two sides are equal, print that it is an isosceles triangle. 

➢ If none of the sides are equal, print that it is a scalene triangle.

 ii. Algorithm Step 1. Start: Begin the algorithm. 

Step 2. Include Necessary Libraries: Include the stdio.h header file for standard input-output operations. Step 3. Define Constants and Functions: 

➢ Define a function triangleType to determine the type of the triangle. 

➢ Declare the function prototype with parameters representing the lengths of the sides of the triangle. 

Step 4. Declare Variables: Declare variables side1, side2, and side3 to store the lengths of the sides of the triangle. 

Step 5. Prompt for User Input: Display a message asking the user to enter the lengths of the sides of the triangle. 

Step 6. Read User Input: Read three integer values entered by the user and store them in variables side1, side2, and side3. 

Step 7. Check Triangle Validity: ➢ Ensure that the sum of the lengths of any two sides is greater than the length of the third side.

 ➢ If not, display a message indicating that the given sides do not form a triangle and terminate the program. 

Step 8. Call Triangle Type Function: If the triangle is valid, call the triangleType function with the lengths of the sides as arguments. 

Step 9. Determine Triangle Type: Inside the triangleType function, determine the type of triangle based on the lengths of its sides.

 ➢ If all sides are equal, print that it is an equilateral triangle. 

➢ If at least two sides are equal, print that it is an isosceles triangle. 

➢ If none of the sides are equal, print that it is a scalene triangle. 

Step 10. Display Triangle Type: Print the type of the triangle determined by the triangleType function. 

Step 11. End the algorithm.


b) Write a C program to find the following using iteration and recursion

 • fibonacci numbers

i. Problem Solving Logic 

a) Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes like this: 0, 1, 1, 2, 3, 5, 8, 13, ... 

b) Iteration Approach: ➢ Initialize variables a and b to 0 and 1 respectively. 

➢ For n = 0, return a. ➢ For n = 1, return b. 

➢ For i from 2 to n, update next to the sum of a and b, then update a to b and b to next.

 ➢ Finally, return next.

 c) Recursion Approach: ➢ If n is less than or equal to 1, return n. 

➢ Otherwise, return the sum of fibonacci_rec(n - 1) and fibonacci_rec(n - 2).

 ii. Algorithm Step 1. Start

 Step 2. Prompt User ➢ Display a message asking the user to enter the number of terms in the Fibonacci sequence. 

➢ Read the input and store it in variable n. 

Step 3. Iteration Approach ➢ Initialize variables a and b to 0 and 1 respectively. 

➢ If n is 0, return a.

 ➢ If n is 1, return b. 

➢ For i from 2 to n: • Calculate next as the sum of a and b. • Update a to b and b to next. 

➢ Return next. 

Step 4. Recursion Approach 

➢ If n is less than or equal to 1, return n. 

➢ Otherwise, return the sum of: • fibonacci_rec(n - 1) (recursive call with n - 1) • fibonacci_rec(n - 2) (recursive call with n - 2) 

Step 5. Main Function ➢ Call both the iteration and recursion functions with input n to generate the Fibonacci sequence. 

➢ Print the Fibonacci sequence generated by both methods.

 Step 6. End.

O/P


• Factorial 
i. Problem Solving Logic
 a) The factorial of a non-negative integer 𝑛n is the product of all positive integers less than or equal to 𝑛n. 
b) Iterative Solution: 
➢ Initialize a variable fact to store the factorial, initially set to 1. 
➢ Iterate from 1 to 𝑛n, multiplying fact by each number in the range. 
➢ Return the value of fact after the loop.
 
c) Recursive Solution: 
➢ Base case: If 𝑛n is 0, return 1 (the factorial of 0 is 1). 
➢ Recursive case: Otherwise, return 𝑛×factorial(𝑛−1)n×factorial(n−1). 

ii. Algorithm Step 1. Start 

Step 2. Initialize variables: ➢ num: Input number for which factorial needs to be calculated.
 ➢ fact_iter: Variable to store factorial using iteration. 
➢ fact_rec: Variable to store factorial using recursion.

 Step 3. Prompt the user to enter a number num.

 Step 4. Read the value of num from the user.

 Step 5. Calculate factorial using iteration: 
➢ Initialize fact_iter to 1. 
➢ Start a loop from 1 to num. 
➢ Within the loop, multiply fact_iter by the loop variable. 
➢ After the loop, fact_iter will store the factorial of num. 

Step 6. Display the factorial calculated using iteration (fact_iter). 

Step 7. Calculate factorial using recursion:
 ➢ Define a recursive function factorial_rec that takes an integer parameter n. 
➢ If n is 0, return 1. 
➢ Otherwise, return n multiplied by the result of factorial_rec(n - 1). 

Step 8. Call the factorial_rec function with the input num and store the result in fact_rec. 

Step 9. Display the factorial calculated using recursion (fact_rec). 

Step 10. End.
O/P




• Power
i. Problem Solving Logic 
a) Iterative Approach: 
➢ Initialize a variable result to store the result, and set it to 1 initially. 
➢ Using a loop: • Multiply the result by the base for exponent number of times
• Decrement the value of exponent in each iteration. 
➢ Once the loop finishes, result will contain the calculated power.

 b) Recursive Approach: 
➢ Define a recursive function power_rec that takes the base and the exponent as parameters. 
➢ Base case: If the exponent is 0, return 1. 
➢ Recursive case: Otherwise, multiply the base by the result of calling power_rec recursively with the base and exponent - 1.
 ➢ This function will keep multiplying the base recursively until the exponent becomes 0, and then it will return 1. 

ii. Algorithm Step 1. Start 

Step 2. Initialize variables: 
➢ base (double) to store the base value entered by the user.
 ➢ exponent (integer) to store the exponent entered by the user.
 ➢ result_iter (double) to store the result of the iterative calculation. 
➢ result_rec (double) to store the result of the recursive calculation. 

Step 3. Prompt the user to enter the base value and read the input into the variable base. 

Step 4. Prompt the user to enter the exponent value and read the input into the variable exponent.
 ➢ Calculate power using iteration:
 ➢ Initialize result_iter to 1.0. 
➢ Repeat the following steps exponent number of times: 
• Multiply result_iter by base. 
• Decrement exponent by 1 in each iteration. 
➢ Store the final value of result_iter. 

Step 5. Calculate power using recursion:
 ➢ Define a recursive function power_rec that takes base and exponent as parameters.
 ➢ If exponent is 0, return 1. 
➢ Otherwise, return base multiplied by the result of calling power_rec recursively with base and exponent - 1. 

Step 6. Call the power_rec function with the provided base and exponent values and store the result in result_rec. 

Step 7. Print the calculated power using both the iterative and recursive approaches: 
➢ Print "Using iteration: base ^ exponent = result_iter". 
➢ Print "Using recursion: base ^ exponent = result_rec".

 Step 8. End


O/P
LAB:5

a) Write a C program to swap two numbers using pointers

i. Problem Solving Logic 
a) Declare variables: int num1, num2, *ptr1, *ptr2; 

b) Input numbers: Prompt user to enter num1 and num2. 

c) Assign pointers: Assign addresses of num1 and num2 to ptr1 and ptr2 respectively. 

d) Swap numbers: Use pointer arithmetic to swap the values pointed to by ptr1 and ptr2. 
➢ Store the value pointed to by ptr1 in a temporary variable.
 ➢ Assign the value pointed to by ptr2 to the address pointed to by ptr1. 
➢ Assign the value stored in the temporary variable to the address pointed to by ptr2.

 e) Output results: Print num1 and num2 to display the swapped numbers.

 ii. Algorithm Step 1. Start 
Step 2. Declare Variables: ➢ Declare four variables: num1, num2 (to store the numbers to be swapped), *ptr1, *ptr2 (pointer variables). 

Step 3. Input Numbers: 
➢ Prompt the user to enter the first number (num1). 
➢ Prompt the user to enter the second number (num2).

 Step 4. Assign Pointers: ➢ Assign the address of num1 to ptr1.
 ➢ Assign the address of num2 to ptr2. 

Step 5. Declare Temporary Variable: ➢ Declare a temporary variable temp.
 Step 6. Swap Numbers: 
➢ Store the value pointed to by ptr1 in the temp variable. 
➢ Assign the value pointed to by ptr2 to the address pointed to by ptr1. 
➢ Assign the value stored in the temp variable to the address pointed to by ptr2.

 Step 7. Output Results:
 ➢ Print a message indicating that the numbers have been swapped. 
➢ Print the swapped numbers num1 and num2.

 Step 8. End



b) Write a C program to perform the following using pointers 

I. String copy:

Problem Solving Logic a) Declare necessary variables and functions.

 b) Implement the string copy function: 

➢ Function: void stringCopy(char *dest, const char *src)

 ➢ Loop through each character of the source string using a pointer. 

➢ Copy each character to the destination string using pointer arithmetic.

 ➢ Add a null terminator to the end of the destination string. 

c) Implement the string compare function: 

➢ Function: int stringCompare(const char *str1, const char *str2) ➢ Loop through each character of the strings using pointers. 

➢ Compare corresponding characters of both strings until a mismatch is found or the null terminator is encountered. 

➢ Return a positive value if str1 is greater, negative value if str2 is greater, or 0 if both strings are equal. 

d) Implement the string length function: 

➢ Function: int stringLength(const char *str) 

➢ Initialize a counter variable to count the characters.

 ➢ Loop through each character of the string using a pointer until the null terminator is encountered.

 ➢ Increment the counter for each character encountered. 

➢ Return the count as the length of the string. 

e) In the main function: ➢ Declare variables to store strings.

 ➢ Input strings from the user. 

➢ Call the string copy, string compare, and string length functions as needed. 

➢ Output the results.

 f) End of program.

 ii. Algorithm Step 1. Start 

Step 2. Declare necessary variables and functions: 

➢ Declare variables for storing strings and function prototypes for string copy, string compare, and string length functions. 

Step 3. Implement the string copy function: 

➢ Function: void stringCopy(char *dest, const char *src) 

➢ Initialize a pointer srcPtr to point to the source string src. 

➢ Initialize a pointer destPtr to point to the destination string dest. 

➢ Iterate through each character of the source string using a loop.

 ➢ Copy each character from srcPtr to destPtr. 

➢ Increment both pointers after each character copy. 

➢ Add a null terminator \0 to the end of the destination string. 

Step 4. Implement the string compare function: 

➢ Function: int stringCompare(const char *str1, const char *str2) 

➢ Initialize a pointer ptr1 to point to the first string str1. 

➢ Initialize a pointer ptr2 to point to the second string str2. 

➢ Iterate through each character of both strings using a loop. 

➢ Compare each character pointed by ptr1 and ptr2. 

➢ If the characters are not equal, return the difference between them.

 ➢ If both strings are equal until the null terminator, return 0. 

Step 5.Implement the string length function: 

➢ Function: int stringLength(const char *str)

 ➢ Initialize a pointer ptr to point to the string str. 

➢ Initialize a variable length to store the length of the string and set it to 0. 

➢ Iterate through each character of the string using a loop. 

➢ Increment the length for each character until the null terminator \0 is encountered. 

➢ Return the length of the string. 

Step 6.In the main function: 

➢ Declare variables to store strings and variables to store the results of string operations. ➢ Input strings from the user. 

➢ Call the string copy function, passing the source and destination strings. 

➢ Call the string compare function, passing the two strings to compare. 

➢ Call the string length function, passing the string for which length is to be calculated. 

➢ Output the results ofstring copy, string compare, and string length operations.

 Step 7.End



LAB:6


a. Write a C program to create and display a record for each student in your class with fields Reg no, Name and Age.

i. Problem Solving Logic 

a) Define the Structure: ➢ Create a structure named Student with fields for registration number, name, and age. 

b) Get the Number of Students: 

➢ Prompt the user to input the number of students. Store this number in a variable. 

c) Create an Array of Structures:

➢ Based on the number of students, create an array to hold the student structures. 

d) Input Loop: ➢ Use a for loop to input the details for each student. 

➢ Inside the loop, prompt the user to enter the registration number, name, and age for each student.

 ➢ Store these values in the respective fields of the current student structure. 

e) Output Loop: ➢ Use another for loop to iterate through the array of student structures. 

➢ Inside the loop, print the registration number, name, and age for each student. 

ii. Algorithm Step 1. Start 

Step 2. Define Structure ➢ Define a structure Student with fields: 

➢ int reg_no 

➢ char name[50]

 ➢ int age 

Step 3. Get Number of Students 

➢ Prompt the user to enter the number of students (n). 

➢ Read the number n. 

Step 4. Declare Array of Structures ➢ Declare an array students of type Student with size n. 

Step 5. Input Student Details 

➢ For i from 0 to n-1 do:

 ➢ Print "Enter details for student i+1:"

 ➢ Read the registration number and store it in students[i].reg_no. 

➢ Read the name and store it in students[i].name. 

➢ Read the age and store it in students[i]. age. 

Step 6. Display Student Records 

➢ Print "Student Records:" ➢ For i from 0 to n-1 do: 

➢ Print "Student i+1" 

➢ Print "Registration Number: students[i].reg_no" 

➢ Print "Name: students[i].name" 

 ➢ Print "Age: students[i]. age" 

Step 7. End  


O/P:



b. Modify the above program to calculate grade for each subject for all students in 1 st semester.

i. Problem Solving Logic 

a) Define the Structure: ➢ Extend the struct Student to include fields for marks and grades arrays. 

b) Get the Number of Students: ➢ Prompt the user to input the number of students. Store this number in a variable.

 c) Create an Array of Structures: ➢ Based on the number of students, create an array to hold the student structures.

 d) Input Loop: ➢ Use a for loop to input the details for each student.

 ➢ Inside the loop, prompt the user to enter the registration number, name, age, and marks for each subject. 

➢ Calculate and store the grades based on the marks. 

e) Output Loop: ➢ Use another for loop to iterate through the array of student structures. 

➢ Inside the loop, print the registration number, name, age, marks, and grades for each subject.

Algorithm Step 1. Start 

Step 2. Define Structure 

➢ Define a structure Student with fields:

 ➢ int reg_no ➢ char name[50] 

➢ int age

 ➢ int marks[3] 

➢ char grades[3] 

Step 3. Grade Calculation Function

 ➢ Define a function calculateGrade(int marks) that returns a char:

 ➢ If marks >= 90 return 'A' 

➢ Else if marks >= 80 return 'B'

 ➢ Else if marks >= 70 return 'C'

 ➢ Else if marks >= 60 return 'D' 

➢ Else return 'F' 

Step 4. Get Number of Students 

➢ Print "Enter the number of students: " 

➢ Read n using scanf("%d", &n) 

Step 5. Declare Array of Structures 

➢ Declare struct Student students[n] 

 Step 6. Input Student Details

 ➢ For i from 0 to n-1 do:

 ➢ Print "Enter details for student i+1:" 

➢ Print "Registration Number: " 

➢ Read students[i].reg_no using scanf("%d", &students[i].reg_no) 

➢ Print "Name: " 

➢ Read students[i].name using scanf("%s", students[i].name) 

➢ Print "Age: " ➢ Read students[i].age using scanf("%d", &students[i].age)

 ➢ For j from 0 to 2 do: 

➢ Print "Marks for Subject j+1: " 

➢ Read students[i].marks[j] using scanf("%d", &students[i].marks[j]) 

➢ Calculate grade using students[i].grades[j] = calculateGrade(students[i].marks[j]) 

Step 7. Display Student Records ➢ Print "\nStudent Records:\n" 

➢ For i from 0 to n-1 do: 

➢ Print "\nStudent i+1" 

➢ Print "Registration Number: students[i].reg_no"

 ➢ Print "Name: students[i].name" 

➢ Print "Age: students[i].age"

 ➢ For j from 0 to 2 do:

 ➢ Print "Subject j+1: Marks = students[i].marks[j], Grade = students[i].grades[j]"

STEP 8: END

.                      

O/P






Comments