KEMBAR78
C Programming L Lab Record 2024 11 29 | PDF
0% found this document useful (0 votes)
7 views78 pages

C Programming L Lab Record 2024 11 29

The document outlines a C Programming Laboratory course for a student named Laksh Chikara, detailing various experiments conducted between November 13 and November 29, 2024. Each experiment includes an aim, input/output formats, and sample test cases for programs covering topics such as arithmetic operations, gross salary calculation, and quadratic equations. The document serves as a record of the student's programming exercises and learning outcomes.

Uploaded by

lakshchikara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
7 views78 pages

C Programming L Lab Record 2024 11 29

The document outlines a C Programming Laboratory course for a student named Laksh Chikara, detailing various experiments conducted between November 13 and November 29, 2024. Each experiment includes an aim, input/output formats, and sample test cases for programs covering topics such as arithmetic operations, gross salary calculation, and quadratic equations. The document serves as a record of the student's programming exercises and learning outcomes.

Uploaded by

lakshchikara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 78
: C Programming Laboratory - 4.5CA151C01 Student Name Laksh Chikara Roll No. Section Session/Batch Sree) Faculty Name >*MANAV RACHNA vidlyanatorilysha! ‘Manav Rachna international Institute of Research and Studies Deemed tob ty under saction Soft S.No. Experiment Date Aim of Experiment Signature/Date Grade Wednesday, 13th Nov 2024 Arithmetic operations Friday, 29th Nov 2024 Calculate Gross Salary Wednesday, 13th Nov 2024 Sum and Reverse of a Three Digit Number Friday, 29th Nov 2024 Swap 2 Numbers Thursday, 28th Nov 2024 Multiplication Table Thursday, 28th Nov 2024 Greatest of Three Numbers Thursday, 28th Nov 2024 Leap Year or Not Thursday, 28th Nov 2024 Even or Odd Thursday, 28th Nov 2024 quadratic equation 10 " Thursday, 28th Nov 2024 Thursday, 28th Nov 2024 Weekday number using Switch-case Using #define in C for String Manipulation 12 Thursday, 28th Nov 2024 First n Natural Numbers and their Sum 3 Thursday, Small Factorials. 28th Nov 2024 4 Thursday, ‘Fibonacci series 28th Nov 2024 15 Thursday, A Pyramid of Stars 28th Nov 2024 16 Thursday, _—_—Palindrome or Not 28th Nov 2024 W7 Thursday, Maximum Number in an Array 28th Nov 2024 18 Thursday, Search an Element in an Array 28th Nov 2024 19 Thursday, Addition of Two Matrices 28th Nov 2024 20 Thursday, Subtraction of Two Matrices 28th Nov 2024 aa Thursday, Multiplication of two Matrices 28th Nov 2024 22 Friday, 29th Matrix Transpose Nov 2024 23 Friday, 28th —_Concatenate two strings Nov 2024 without using built-in functions 2a Prime and Armstrong 25 Friday, 28th Binary to Decimal and Nov 2024 Decimal to Binary 26 Friday, 29th_—_Usage of Pointer Nov 2024 27 Friday, 29th Reverse an Array In Place Nov 2024 28 Friday, 28th Student Details using Nov 2024 Structure 29 Friday, 29th Add Two Distances (in inch- Nov 2024 feet) Using Structures 30 Friday, 28th Add two Complex Numbers Nov 2024 Experiment - 1 Experiment - 1: Arithmetic operations Name: Laksh Chikara UUID: Date of performance: 13th Nov 2024 Ain Write a C program to perform arithmetic operations like +,-,*,/,% on two input variables. Input Format: + The first line of input is an integer representing the value for first number + The second line of input is an integer representing the value of second number Output Format: + The program prints the results of addition, subtraction, multiplication, division, and modulus each on a Note: For ivision and Modulo operation, the value of num2 must be greater than 0 ‘Add new line char \n at the end of output. Tasks to be done: ram: arithmeticoperations.¢ include int main() { int num, muna; scanf( num); seanf("%d", &num2); if (muna > @) { printf("ed\n", umd + numa); // Addition printé(“%d\n", nud - nun); // Subtraction printf("xd\n", num * nun2); // Multiplication printf("Xd\n", numl / num2); // Division printf("Xd\n", numl % num2); — // Modulus } else printf("Error: Division and modulus operations require the second number to be greater ‘than @.\n"); 7 return 0; Test case - 1 Test case - 2 User Output 1000 2 1002 998 2000, 500 @ Resul Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 2 Experiment - 2: Calculate Gross Salary Name: Laksh Chikara uuID: Date of performance: 29th Nov 2024 Ait Write the € program tolnput the basic salary of an employee and calculate his/her gross salary based on the following conditions: + If Basic Salary <= 10000, then HRA = 20% and DA = 80% + If Basic Salary <= 20000, then HRA = 25% and DA = 90% + IfBasic Salary > 20000, then HRA = 30% and DA = 959% Gross Salary is the total of Basic Salary, HRA and DA. Note: Output the grass salary up to two decimal points only. Tasks to be done: Observations: Program: #include int main() { float basic_salary, HRA, DA, gross_salary; print#(“Enter basic salary: seanf("%F", Bbasic_salary)} Af (basic_salary <= 10008) { HRA = 0.26 * basic_salary; DA = 0.80 * basic_salary; } else if (basic_salary <= 20000) { HRA = 0.25 * basic_salary; DA = @.99 * ba } else { HRA = 0.30 * basic_salary; DA = @.95 * basic_salarys » ic_salary; gross_salary = basic_salary + HRA + DA; print#("Gross salary: %.2F\n", gross_salary); return 0; } Output: Test case - 1 User Output Enter basic salan 35000 Gross salary: 78750.00 Test case - 2 User Output Enter basic salary: 90000 Gross salary: 202500.00 Resul Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 3 Experiment - 3: Sum and Reverse of a Three Digit Number Name: Laksh Chikara uuID: Date of performance: 13th Nov 2024 Ait Write a C program to find sum and reverse of three digit number. Input Format: A single line of input contains a three-digit integer Output Format: The first line contains an integer representing the sum of the digits of the given integer The second line contains an integer representing the reverse of the given integer If the input is not a three-digit non negative number, print "Invalid input” on a single line. Constraints: 100 <= num 999 Tasks to be done: Observation: ram: include int main() { ‘int num, sum @, reverse = 0, digit; // Read the input number scanf("%d", &num) ; // Check if the input is a valid three-digit nunber if (num < 10 || num > 999) { print#("Invalid input\n"); } else { // Calculate the sum of the digits int temp = num; // Copy of num to extract digits while (temp = 2) { digit = temp % 10; // Get the last digit sum += digit; // Add the digit to sum ‘temp /= 18; // Remove the last digit // Calculate the reverse of the number temp = num; // Reset temp to original num while (temp != @) { digit = temp % 10; // Get the last digit reverse = reverse * 10+ digit; // Build the reversed number ‘temp /= 10; // Remove the last digit t // Output the sum and reverse printé("Rd\n", sum); print#("%d\n", reverse); 7 return 0; t Output: Test case - 1 User Output 59. Invalid input Test case - 2 User Output 100 1 1 Test case - 3 User Output 251 2 152 Result: Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 4 Experiment - 4: Swap 2 Numbers Name: Laksh Chikara UUID: Date of performance: 29th Nov 2024 Ain Write a C program to swap two numbers without using a third variable. Input Format: The first line contains an integer representing the first number The second line contains an integer representing the second number Note: You just need to implement the logic to swap the two numbers without using third variable, printing the result has already been given. Tasks to be done: Observations: swapwithoutvar.c include int main() { int num, numa; scanf("%d", &numt); seanf("%d", &num2); hum = num + num2s rnum2 = num - rum2s um = numa = nun2s printf("After swapping, first number is: %d\n", num); printf(“Aafter swapping, second number is: Xd", num2); return 0; Output: Test case - 1 User Output 12 18 ‘After swapping, first number is: 18 After swapping, second number is: 12 Test case - 2 User Output 90 56. ‘After swapping, first number is: 56 After swapping, second number is: 9@ Result: Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 5 Experiment - 5: Multiplication Table Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Write a C program to print the multiplication table of a given number. Input Format: A single line of input contains an integer representing the number to which the multiplication table is to be printed Output Format: The multiplication table of the given number up to 10 values Tasks to be done: Observations: multiplicationtable.c include int main() ( int num; scanf("%d", &nun) for (int 1 = 1; i <= 15 464) { print ("Xd x Xd = %d\n", num, i, num * i); } return 0; y Output: Test case - 1 User Output 2 2xi-2 2x2-4 2x3-6 2x4-8 2x5 = 10 2x6=12 2x7- 14 2x8 = 16 2x9 = 18 2x 10 = 20 Test case - 2 User Output 5 Sxi-5 Sx2- 10 5x3 = 15 Sx4= 20 5x5 = 25 5x6 = 30 Sx7 = 35 5x8 = 40 5x9 = 45 Sx 10 = 50 Result: Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 6 Experiment - 6: Greatest of Three Numbers Name: Laksh Chikara uuID: Date of performance: 28th Nov 2024 Ait Write a C program to display the greatest of three numbers. Input Format The program prompts the user to enter three integers in three lines. Output Format The program prints the greatest of the three integers. Tasks to be done: Observation: ram: greatestofThree.c // Type Content here... #include int main() { int num, num2, num3; // Input three integers seanf(“%d", num); scanf("%d", &num2); scanf("Kd", &num3); // Determine the greatest nunber int greatest = num; // Assume first number is the greatest if (num2 > greatest) { greatest = nun2; t if (ound > greatest) { greatest = nun3; y // Print the greatest number printf("Xd\n", greatest); return 95 y Output: Test case - 1 User Output 2 36 45 35 Test case - 2 User Output 1 25 46. on Resul Thus the above program is executed successfully and the output has been verified Learning Outcom Experiment - 7 Experiment - 7: Leap Year or Not Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain Write a C program to find whether the entered year is a leap year or not, Input Format: + Input consists of a four digit integer representing the year Output Format: + Print “Leap year” if the given is a leap year, otherwise print "Not a leap year" Tasks to be done: Observation: // Type Content here... #include int main() ( Ant years 1/ Input the year seanf("%d", 8year); // Check if the year is a leap year Af ((year % 4 == @ 8 year % 100 I= @) || (year % 400 == @)) { print#(*Leap year\n"); } else { print#(‘Not a leap year\n"); > return 85 } Output: Test case - 1 User Output 2016 Leap year Test case - 2 User Output 2011 Not_a leap year Result: Thus the above program is executed successfully and the output has been verified Learning Outcom Experiment - 8 Experiment - 8: Even or Odd Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain Write aC program to check whether given number is even or odd. Input Format: Input is a single line of input contains a positive integer Output Format: Print “Even' if the number is even, otherwise print "Odd" Tasks to be done: Observation: Program: // Type Content here... #include int main() { int nuns // Input the nunber seanf("%d", nun); // Check if the nunber is even or odd if (num % 2 == 0) { printf ("Even\n")5 } else print ("odd\n"); » return 8; y Output: Test case - 1 User Output 45 odd Test case - 2 User Output 54 Even Thus the above program is executed successfully and the output has been verified Learning Outcom Experiment - 9 Experiment - 9: quadratic equation Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain Write aC program that takes three coefficients a,b, and c of a quadratic equation ax*+bx+c = 0 as input and computes al possible roots Note: Print the output up to 2 decimals Example Input: Enter coefficient a: 4 Enter coefficient b: 7 Enter coefficient c:2 Example output: Root 1: -0.36 Root 2: -1.39 Tasks to be done: Program: include include int main() ( float a, b, ¢, discriminant, root1, root2; // Input coefficients printf(“Enter coefficient a: "); scant("%E", Ba); print f("Enter coefficient b scanf("KF", 8b); print#("Enter coefficient ¢ scanf("KF", &c); // Calculate the discriminant discriminant = b*b-4*a*c3 // Check if the discriminant is non- negative (real roots exist) Af (discriminant > 0) { root = (-b + sqrt(discriminant)) / (2 * a); root? = (-b - sqrt(discriminant)) / (2 * a); print#(*Root 1: %.2f\n", root); print#(*Root 2: %.2f\n", root2); } // Tf discriminant is ®, the ‘equation has one real root else if (discriminant ef root = “b/s (Q*a); printf(*Root 1: %.2f\n", root); t 1/1 discriminant is negative, the roots are complex (not real) else { printf(*No real roots\n"); y return 0; y Output: Test case - 1 ee User Output aa Enter coefficient a: 4 Enter coefficient 6: Z Enter coefficient ¢ 2 Root 1: -0.36 Root 2: -1.39 Test case - 2 User Output Enter coefficient a 3 Enter coefficient 6: 6 Enter coefficient < 2 Root 1; 0.29 Root 2: 2.29 Result Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 10 Experiment - 10: Weekday number using Switch-case Name: Laksh Chikara uuiD: Date of performance: 28th Nov 2024 Ai Write a program to read the weekday number from the standard input and print the weekday name using switch-case. Assume that the weekdays are provided with the below numbers: + Sunday = 0 + Monday = 1 + Tuesday — 2 + Wednesday — 3 + Thursday = 4 + Friday = 5 + Saturday = 6 Input Format: + A single line of input contains a positive integer representing the weekday number Output Format: + The corresponding day of the week based on the above given criteria + If the given number is not a valid weekday number, print “Invalid weekday number” Tasks to be done: Observations Program: 11 Type Content here... include int main() ( int weekday; scanf("%d", &weekday); switch (weekday) { case 0: print#(*Sunday\n"); break; case 1: print#(*Monday\n"); break; case 2: print#("Tuesday\n"); break; case 3: print? break; Wednesday\n") 5 case 4: print#(*Thursday\n") ; break; case 5: print#(“Friday\n"); break; case 6: print#("Saturday\n"); break; default: print#("Invalid weekday nunber\n"); break; } return 0; } Output: Test case - 1 User Output 6 Saturday Test case - 2 User Output 9 Invalid weekday number Resul Thus the above program is executed successfully and the output has been verified Learning Outcom Experiment - 11 Experiment - 11: Using #define in C for String Manipulation Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain Write aC program that uses a macro to define a constant string and demonstrates string manipulation operations. Implement the program with the following features: + Define a macro SAMPLE_STRING with a user-defined string literal "Hello, World!” + Print the defined sample string + Calculate and print the length of the sample string, + Print the sample string in reverse order. Tasks to be done: Observation: ram: StringManipulation include include define SANPLE STRING “Hello, World! int main() ( print#("%s\ ') SAMPLE_STRING) ; Ant length = strlen(SAMPLE_STRING); for (int i = length - 4; i >= 0; print#("Xc", SAMPLE_STRING[1]); } printé("\ YE % return 0; y Output: Test case - 1 User Output Hello, world! dlroW_,oleH Result: Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 12 Experiment - 12: First n Natural Numbers and their Sum Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain White a C program to display frst m natural numbers and their sur. Input Format: A single line of input contains an integer representing the value of Output Format: The first line contains n space separated elements representing the first n natural numbers: The second line contains the sum of the first r natural numbers Note: For every output, the program must navigate to next line using Tasks to be done: Observation: ram: rstNNaturals.c include int main() { int n, sum = 05 // Input the number scanf("%d", &n); // Print the first n natural numbers for (int i= 5 4 <= nj dee) { printf(*\n"); print#("%d\n", sum); return 0; } Output: Test case - 1 User Output Test case - 2 User Output 16 123456789 10 11 12 13 14 15 16 136. Resul Thus the above program is executed successfully and the output has been verified Experiment - 13 Experiment - 13: Small Factorials Name: Laksh Chikara uuID: Date of performance: 28th Nov 2024 Ait Write a C program to calculate the factorial of positive integers without using recursion. Input Format: Single line of input contains an integer N representing the value to find the factorial Output Format: Print the factorial result of N Tasks to be done: Observation: Program: include int main() { int N, factorial = 1; seanf("%d", 8N); for (int 1 = 15 4 <= Ns i++) ( factorial } // Print the factorial print#("Zd\n", factorial); return @; } Output: Test case - 1 User Output Test case - 2 User Output Test case - 3 User Output 4 24 Test case - 4 User Output 5 120 Test case - 5 User Output Resul Thus the above program is executed successfully and the output has been verified Learning Outcom Experiment - 14 Experiment - 14: Fibonacci series Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain White a C program that prints the first \” numbers ofthe Fibonacci series. Input Format: A single line contains an integer representing the value of NV Output Format: First WV numbers of the Fibonacci series are separated by space Tasks to be done: Observation: ram: fibonacciSeries.c include int main() { int 5 seanf("%d", &n); int a=@, b= 45 for (int i = 0; i < nj ie) { if (i == 8) { print#("xd", 2)5 } else if (i == 4) { print#(" Xd", b); } else { int next = a +b; printf(" Xd", next); a=; b= next; » } prantf(* \n"); return @; } Output: Test case - 1 User Output 5 @ii2s Test case - 2 User Output 8 oi1235813 Resul Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 15 Experiment - 15: A Pyramid of Stars Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain Write aC program to print stars sequence in pyramid form. Input Format: A single line of input contains an integer representing the total number of rows of the pyramid Output Format: Output the pyramid of stars with n rows. Tasks to be done: Observation: ram: pyramidofstars.c 11 Type Content here... include int main() ( int nj scanf("%d", &n); for (int 4 = 13 4 < wae) { for (int j= 43 J ce n= 45 JH) ( printé } for (int k = 45 k c= (2*4- 1)s ket) £ printe("); y print#("\n")s y return @; Test case - 1 User Output 5 Test case - 2 User Output 4 Result: Thus the above program is executed successfully and the output has been verified ing Outcom: Experiment - 16 Experiment - 16: Palindrome or Not Name: Laksh Chikara uuiD: Date of performance: 28th Nov 2024 Ai Write a C program to check whether the given number is palindrome or not. Input Format: A single line of input contains an integer. Output Format: If the given number is palindrome, then print “{numbert is palindrome”, otherwise, print "{number) is not palindrome”. Tasks to be done: Observations: 11 Type Content here... include int main() ( int num, originalNum, reversediium = @, remainder; scanf("d", &num) originalNum = num; while (num = 0) { remainder = num % 10; reversedNum = reversedNum * 18 + remainders num /= } 8; if (originalNum == reversedNum) { print#("%d is palindrome\n", originalNum) ; y else ( printf("Xé is not palindrome\n", originalNum); } return 0; y Output: Test case - 1 User Output 321 121 1s palindrone Test case - 2 User Output 134 134 is not palindrome Result: Thus the above program is executed successfully and the output has been verified Learning Outcom Experiment - 17 Experiment - 17: Maximum Number in an Array Name: Laksh Chikara uuID: Date of performance: 28th Nov 2024 Ait Write a C program to find the maximum number in an array. Input Format: The first line contains an integer n representing the total number of elements in the array The next n lines contain an integer representing the elements of the array Output Format: The maximum element of the array in the format "Largest element: (number)” Tasks to be done: Observations: Program: include int main() { int 5 pranté 7 scanf("%d", &n); int arr(n); for (int 1 = 5 1 < m5 det) { scanf("Kd", Garr[4])5 y int max = are[@]; for (int 1 = 15 4 max) ¢ max = arr[i]; } } print#("Largest element: %d\n", max); Test case - 1 User Output ‘argest element: 6 Test case - 2 User Output 9 45, 12 10 3 Largest element: 45 Result: Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 18 Experiment - 18: Search an Element in an Array Name: Laksh Chikara uuID: Date of performance: 28th Nov 2024 Ait Write a C program to search for an element in an array. Return the position of the user-given element if it is found in the array otherwise display that it is not found. Input Format: The first line contains an integer n representing the size of the array The second line contains n space separated integers representing the elements of the array The third line contains an integer s, representing the element to be searched Output Format: If the search element is found, print “(search element) found at position {position}", otherwise print ™ {search element) not found" Tasks to be done: Observation: Program: searchEle.c include int main() { int n, searchElenent, found print#("Enter size: seanf("%d", Bn); int arr[n]3 print#(“Enter elements: "); for (int 1 = @; 1 int main() { int rows, cols; scanf("%d 2d", Brows, &cols); Ant matrixd[rows][cols], matrix2[rows][cols], sum[rows][cols]; for (int 1 = @; 1 < rows; i+) { for (int j = @; 4 < cols; j++) { scanf("Sd", @matrixt[1][4])5 } } for (int i = 0; i < rows; itt) { for (int J = @; J < colss J+) { scanf("%d", amatrax2(i][3})5 } } for (int 1 = @; 1 < rows; i+) { for (int j = 5 5 < cols; j++) { sum[SJ[J] = matrixt[4][J] + matrix2[i}[J]s } } for (int 1 = @; 1 < rows; i++) { for (int j = @; 4 < cols; j++) { printe("%d ", sum[S][J])3 y prant#(*\n"); t return @; y Output: Test case - 1 User Output 22 1234 5678 68 10 12 Test case - 2 User Output 12 12 98 10 10 Resul Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 20 Experiment - 20: Subtraction of Two Matrices Name: Laksh Chikara uuiD: Date of performance: 28th Nov 2024 Ai Write a C program to perform the subtraction of two matrices. Input Format: First line contains two space separated integers, row & col, representing the number of rows and columns of each matrix Next row lines contain col number of space separated integers representing the elements of mattix 1 Next row lines contain col number of space separated integers representing the elements of matrix 2 Output Format: row number of lines with col number of space separated elements representing the elements of resultant matrix Note: Subtraction of two matrices can only be done when the dimensions of both matrices are same, so we are taking the same dimensions for both matrices. Tasks to be dons Observation: include int main() { int row, col; scanf("Xd 2d", &row, &col); Ant matrixd[row][col], matrix2[row][col], result[row] [col]; print#("Matrix 1:\n"); for (int 1 = @; 4 < row i++) { for (int j = @; 4 < col; j++) { scanf("Sd", &matrixt[1][4])5 } } print#("Matrix 2:\n"); for (int 4 = 0; i < row; itt) { for (int J = @; J < cols j4) { scanf("%Kd", amatrax2(i][3})5 } } for (int i= @; 4 < rows is+) { for (int j = @} j < col; je+) { result(i][i] = matrixi[i][J] - matrix2(i](3]; y } for (int 4 = 0; i < row; itt) { for (int j= @; 4 < cols j++) { print#("d ", result(i][4])5 } print#("\n"); Test case - 1 User Output 22 Matrix 1: 12 34 Matrix 2: 56 78 -4 4 -4 4 Test case - 2 User Output 34 Matrix 1. 1234 9876 5464 Matrix 2 niki wnt 14 @i23 e765 4353 su Thus the above program is executed successfully and the output has been verified Learning Outcome: Experiment - 21 Experiment - 21: Multiplication of two Matrices Name: Laksh Chikara UUID: Date of performance: 28th Nov 2024 Ain Write a program to perform matrix multiplication If Multiplication cannot be done for the given matrices, then print "Not Possible” Input Format: + First line of input contains two space separated integers, no of rows (r1) and columns (ct) of matrix 1 + Next 11 lines contains c1 no of elements of matrix 1, separated by space + Next line of input contains two space separated integers, no of rows (r2) and columns (c2) of matrix 2 + Next 12 lines contains c2 no of elements of matrix 2, separated by space Output Format: + Print the result after multiplying the two matrices, if multiplication is not possible, print "Not Possible* Constraints: eT eer <= 10 +0 <= matrix fi], matrix2fi] <= 100 Tasks to be done: ram: include int main() { int rt, ch, 72, 25 scanf("%d 2d", &rt, Bel); int matrixa[rt][ct]; for (int 4 = @; i < rly iss) { for (int 5 F< cds G4) ( seanf("Kd", @matrsxd[4][3])s } } scanf("Sd %d", &r2, &c2); Ant matrix2[r2][e2]; for (int i= 0; i < 125 ite) ( for (int J = @; J < ed J+) { scanf("%d", @matrax2(i][3})5 } } Af (ct != 2) print#("Not Possible\n"); yelse { int result{ra][e2]; for (int i= 0) 4 < ris d+) { for (int j= 0; 4 < ed; J++) ( result(i]{j } y for (int Ac rds ate) { for (int 5 ees HH) ( for (int k = @; k < cy kes) { result[i][J] += matrixi[i][k] * matrix2[kI[i]; for (int = 0; 4 < raj iss) ( for (int j= 0; J < €25 J++) { printe("mé ", result[i}[3})s } printf("\n") 5 } } return 03 } Output: Test case - 1 User Output 22 12 34 22 12 34 716 15 22 Test case - 2 User Output 33 123 456 789 23 123 456 Not Possible Test case - 3 User Output 22 11.22 33.44 22 1122 33.44 847 1210 1815 2662, Test case - 4 User Output 23 123 456 32 12 34 56 22 28 49 64 Test case - 5 User Output uW 5 22 36 912 Not Possible Resul Thus the above program is executed successfully and the output has been verified Learning Outcome: Experiment - 22 Experiment - 22: Matrix Transpose Name: Laksh Chikara uuiD: Date of performance: 29th Nov 2024 Ai Write a C program to generate the transpose of a matrix. Input Format: + The first line of input should contain two integers separated by a space. These integers represent the ‘number of rows (n) and columns (m) of the matrix. + The next n lines each contain m integers separated by spaces. These integers represent the elements of the matrix. Output format: + The program prints the original matrix in a grid format where each row is on a new line and each clement is separated by a space. + The program prints the transpose of the matrix. The transpose is formed by swapping rows with columns, so each column of the original matrix becomes a row in the transposed matrix. Tasks to be done: ram: matrixTranspose.c include void printMatrix(int ros, int cols, int matrix{rows)[cols]) { for (int 4 = 0} 4 < rows; i++) for (int j= 0; J < cols; J++) ( pinte("sd ", nateix[i](J))s } printe(*\n } } int main() { int n, m5 scanf("%d 8d", &n, &m); int matrix(n][m], transpose[m][n]; for (int 4 = 0; 1 < nj i+) { for (int j= @; J int main() ( char stri{1ee], str2[1e9], result[200]; int i= 0, j= 0; scanf("%s", str); seanf("%s", str2); while (stri[i] I= "\9") result[i] = stra[i]; Lees } while (str2[j] != "\e"){ result[i] = str2[J]; ies } result(i] = "\@"s print#("Xs\n" , result); return @; } Output: Test case - 1 User Output Programming Languages ProgranmingLanguages Test case - 2 User Output Morning ‘SunShine Morningsunshine Resul Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 25 Experiment - 25: Binary to Decimal and Decimal to Binary Name: Laksh Chikara uuID: Date of performance: 29th Nov 2024 Ait Write a C program that includes two functions: one to convert a binary number to a decimal number and another to convert a decimal number to a binary number. The program should prompt the user to choose the type of conversion and input the respective number. Then, it should use the appropriate function to perform the conversion and display the result. Input Format: The first line should contain an integer 1 or 2 indicating the type of conversion: +1 for converting binary to decimal + 2 for converting decimal to binary The second line should contain the binary or decimal number to be converted based on the user's choice. Output Format: The output should be a single line displaying the result of the conversion, Tasks to be done: Program: include include // Function to convert binary to decimal Ant binaryToDecimal(int binary) { int decimal = @, base ) remainder; while (binary > @) { remainder = binary X 105 decimal += remainder * base; base *= 25 binary /= 10; return decimals // Function to convert decimal to binary int decimalToBinary(int decimal) { Ant binary = @, base 1, remainders while (decimal > 0) ¢ remainder = decimal % 23 binary += remainder * base; base *= 10; decimal /= 2; return binary; int main() ( int choice, number; // Prompt user for conversion choice scanf("%d", &choice); if (choice at // Binary to Decimal printf("binary number: "); scanf("Kd", &nunber); printf("Decimal: %4\n", binaryToDecimal(nunber)); } else if (choice == 2) { // Decimal to Binary printf("decimal number ys *, Bnumber); printf("Binary: %d\n", scanf( decimal ToBinary(nunber)); y else { If Invalid choice printf(*Invalid choice\n" return Output: Test case - 1 User Output 1 binary number: 3010 Decimal: 10 Test case - 2 User Output 2 decinal_nunber 15 Binary: 112 Test case - 3 User Output 3 Invalid choice Result: Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 26 Experiment - 26: Usage of Pointer Name: Laksh Chikara UUID: Date of performance: 29th Nov 2024 Ain Write aC program to demonstrate the use of pointers. The program should take an integer input from the User and then print both the value and the address of the entered integer. This should be done using direct variable access as well as through a pointer Input Format: + The program should prompt the user to enter an integer. Output Format: + Print the value of the integer directly and print the value of the integer using the pointer in separate lines. Note: + The code snippet given is partially filled in. Your task is to complete the missing parts to ensure the program works as intended Tasks to be dons Observation: include int main() ( Ant nuns int *ptr; // Declaration of pointer variable scanf("%d", & num); // Taking input from the user ptr = &num 5 // Assigning address of ‘num’ to ‘ptr* printf("Value of num: %d\n", num); printf(*Value of num using pointer: Rd\n", *ptr ); // Dereferencing pointer to access value return @; Output: Test case - 1 User Output 10 Value oF num: 10 Value of num using pointer: 10 Test case - 2 User Output 20 Value oF num: 20 Value of num using pointer: 20 Resul Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 27 Experiment - 27: Reverse an Array In Place Name: Laksh Chikara uuID: Date of performance: 29th Nov 2024 Ait Write a C function that reverses the elements of an array in place. The function should accept a pointer to the array and the size of the array and should modify the array directly. Input Format: The first input line contains an integer n, representing the number of elements in the array. The second line contains n space-separated integers representing the elements of the array. Output Format: Output the elements of the reversed array in a single line, space separated. Tasks to be done: Observation: Program: reverseInPlace.¢ include void reverseArray(int *arr, int size) { int temp, start = @, end = size - 1; while (start < end) { temp = arr[start]; arr[stant] = arefend]; arr{end] = temp; starter; end } } int main() { int ns JI Input the size of the array scanf("%d", &n); ant are(n); /{ Input the elenents of the array for (int i= 0; 4 include // Define the Student structure struct Student { int rol1Nos char nane[12]; Ant class int year; float totalMarks; b int main() ( struct Student student; student .rol No) ; scanf("%s", student-nane); scanf("Kd", &student.classs); scanf("%d", &student.year); scanf("KF", &student.totalMarks); printf("Student Details:\n"); printf(*Roll No: d\n", student .rolINo) ; printf("Name: %s\n", student.name) ; printf(*Class: %d\n", student.classs); printf("Year: d\n", student.year) ; printf("Total Marks: %.2f\n", student.totalMarks); return @; Output: Test case - 1 User Output 103 Aoemon 8 2022 35 ‘Student Details: Roll No: 103 Nane: Aoenon Class: 8 Year: 2022 Total Marks: 35.00 Test case - 2 User Output 27 Aeogon 9 2023 30 Student details: Roll No: 27 Nane: Aeogon Class: 9 Year: 2023 Total Marks: 30.00 Result Thus the above program is executed successfully and the output has been verified Learning Outcomes: Experiment - 29 Experiment - 29: Add Two Distances (in inch-feet) Using Structures Name: Laksh Chikara uuID: Date of performance: 29th Nov 2024 Ait Write a C Program to add two distances (in inch-feet) system using structures. Input Format: The first line contains two space separated elements representing the feet (integer) and inches (float) respectively of first distance. The second line contains two space separated elements representing the feet (integer) and inches (float) respectively of second distance. Output Format: Output the sum of the two distances in the format "X ft ¥ inch" where the X is the value of feet and Y is the value of inches after addition, Note: The value of Y in the output should be restricted to 1 decimal point only. Tasks to be done: Program: Distance.c include struct Distance { int feet; Float inches; b struct Distance addDistances(struct Distance di, struct Distance 42) { struct Distance result; result.feet = di.feet + d2.feets result.inches = di.inches + d2.inches; Af (result.inches >= 12.0) { result-feet += (int)(result.inches / 12); result-inches = fmod(result. inches, 12.0); } return results int main() { struct Distance di, d2, result; scanf("%d Bf", Bdi-feet, &d1.inches); &d2.feet, &d2.inches); result = addDistances(d1, 42); print#("%d f %.1f inch\n", result.feet, result.inches); return 0; y Output: Test case - 1 User Output 3424 2386 57 ¥ 11.8 inch Test case - 2 User Output 54 3.65 2123 75 # 5.9 inch Resul Thus the above program is executed successfully and the output has been verified Learning Outcom Experiment - 30 Experiment - 30: Add two Complex Numbers Name: Laksh Chikara uuID: Date of performance: 29th Nov 2024 Ait Write a C program to add two complex numbers by passing structure to a function. Input Format: The first line contains two space separated elements representing the real (float) and imaginary (float) values of first complex number respectively The second line contains two space separated elements representing the real (float) and imaginary (float) values of second complex number respectively Output Format: Output the addition result in the format "r + qi” where r refers to the real value and q refers to the imaginary value. Tasks to be done: Observations ram: AddComplexNumbers.¢ include typedef struct t Float real; float imag; // initialize required variables } complex; complex add(complex ni, complex n2) { complex result; result. real iL.real + n2.real; result. imag return results Leimag+ n2.imags } int main() { complex ni, n2, result; scanf("Xf Xf", &nl.real, &n1.imag); scanf("f Xf", &n2.real, &n2.imag); result = add(nt, 2); printf(%.1F + X.1Fi" return @; result-real, result-imag); Output: Test case - 1 User Output 1234 1352 2.5 + 8.31 Test case - 2 User Output 3.244 6.248 4 + 8.94 Result: Thus the above program is executed successfully and the output has been verified Learning Outcom

You might also like