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.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.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:4i. 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/PStep 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
Post a Comment