
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































1. Explain the difference between a compiler and an interpreter in the context of C programming. Solution: Differences between a compiler and an interpreter in the context of C programming are as follows: Compiler Interpreter Translates the entire source code at once Translates the source code line by line Generates intermediate object code/executable Executes code directly without generating an intermediate file Detects errors after the entire code is compiled Stops at the first encountered error and halts execution Needs more memory during compilation Requires less memory during execution Generates machine-specific code Requires an interpreter on each platform Entire C source code must be compiled before execution Can directly execute C interactively line by line Binary executable produced is platform/architecture specific Interpreter can run on any platform with interpreter installed Used for creating executables to distribute software Used for interactive evaluation and debugging Example: GCC (GNU Compiler Collection), Clang Example: Python, Ruby, JavaScript interpreters 2. Describe the concept of data types and their significance in C programming. Solution: Data types are fundamental to writing code in the C programming language. They allow a C program to interpret stored data appropriately by defining the storage format, permissible values and operations that can be performed on the data. Carefully selecting suitable
data types for the data being represented allows the compiler to allocate storage efficiently and helps identify problems early.
Solution: A pointer in C programming is a special variable that stores the address of another variable or data object. Therefore, pointers are used to store the memory address of another value rather than storing the actual data value itself. Characteristics of Pointers:
1. Write a C program to enter two numbers and perform all arithmetic operations. Solution: #include <stdio.h> int main() { double num1, num2; printf("Enter the first number: "); scanf("%lf", &num1); printf("Enter the second number: "); scanf("%lf", &num2); printf("\nArithmetic Operations:\n"); printf("Sum: %.2f\n", num1 + num2); printf("Difference: %.2f\n", num1 - num2); printf("Product: %.2f\n", num1 * num2); if (num2 != 0) { printf("Quotient: %.2f\n", num1 / num2); } else { printf("Cannot divide by zero.\n"); } if (num2 != 0) { printf("Remainder: %.2f\n", fmod(num1, num2)); } else { printf("Cannot calculate remainder when the second number is zero.\n"); } return 0; }
Output: Enter the first number: 76. Enter the second number: 45. Arithmetic Operations: Sum: 122. Difference: 31. Product: 3512. Quotient: 1. Remainder: 31.
2. Write a C program to enter radius of a circle and find its diameter, circumference and area. Solution: #include <stdio.h> #include <math.h> int main() { double radius, diameter, circumference, area; printf("Enter the radius of the circle: "); scanf("%lf", &radius); diameter = 2 * radius; circumference = 2 * M_PI * radius; area = M_PI * pow(radius, 2); printf("\nResults:\n"); printf("Diameter: %.2f\n", diameter); printf("Circumference: %.2f\n", circumference); printf("Area: %.2f\n", area);
printf("\n%d days is equal to:\n", days); printf("Years: %d\n", years); printf("Weeks: %d\n", weeks); printf("Days: %d\n", remaining_days); return 0; }
5. C program to find area of an equilateral triangle. Solution: #include <stdio.h> #include <math.h> int main() { float side, area; printf("Enter the side length of the equilateral triangle: "); scanf("%f", &side); Area = (sqrt(3) / 4) * side * side area = (sqrt(3) / 4) * side * side; printf("\nThe area of the equilateral triangle with side %.2f units is %.2f square units.\n", side, area); return 0; } 6. C program to calculate total average and percentage of five subjects. Solution: #include <stdio.h> int main() { float subject1, subject2, subject3, subject4, subject5; float total_marks, average, percentage; printf("Enter marks of Subject 1: "); scanf("%f", &subject1); printf("Enter marks of Subject 2: "); scanf("%f", &subject2); printf("Enter marks of Subject 3: "); scanf("%f", &subject3); printf("Enter marks of Subject 4: "); scanf("%f", &subject4);
printf("Enter marks of Subject 5: "); scanf("%f", &subject5); total_marks = subject1 + subject2 + subject3 + subject4 + subject5; average = total_marks / 5; percentage = (total_marks / (5 * 100)) * 100; printf("\nTotal Marks: %.2f\n", total_marks); printf("Average Marks: %.2f\n", average); printf("Percentage: %.2f%%\n", percentage); return 0; }
Switch
1. C program to create calculator using switch case and functions. - #include <stdio.h> float add(float num1, float num2); float subtract(float num1, float num2); float multiply(float num1, float num2); float divide(float num1, float num2); int main() { int choice; float num1, num2, result; printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); printf("Select operation:\n"); printf("1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n"); printf("4. Division\n"); scanf("%d", &choice); switch (choice) { case 1: result = add(num1, num2); printf("Result: %.2f\n", result); break; case 2: result = subtract(num1, num2); printf("Result: %.2f\n", result); break; case 3: result = multiply(num1, num2); printf("Result: %.2f\n", result); break; case 4: if (num2 == 0) { printf("Error! Division by zero is not allowed.\n"); } else { result = divide(num1, num2); printf("Result: %.2f\n", result);
break; default: printf("Invalid choice! Please enter a valid operation.\n"); } return 0;} float add(float num1, float num2) {return num1 + num2;} float subtract(float num1, float num2) { return num1 - num2;} float multiply(float num1, float num2) { return num1 * num2;} float divide(float num1, float num2) { return num1 / num2; }
2. C program to print number of days in a month using switch case. - #include <stdio.h> int main() { int month; printf("Enter month number (1-12): "); scanf("%d", &month); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("Number of days in month %d: 31 days\n", month); break; case 4: case 6: case 9: case 11: printf("Number of days in month %d: 30 days\n", month); break; case 2: printf("Number of days in month 2: 28 or 29 days (leap year or non-leap year)\n"); break; default: printf("Invalid month number! Please enter a number between 1 and 12.\n"); } return 0; }
printf("%d is zero.\n", number); break; default: printf("Invalid input!\n"); break; } return 0; }
5. C program to find all roots of a quadratic equation using switch case. - #include <stdio.h> #include <math.h> int main() { float a, b, c; float discriminant, root1, root2, realPart, imaginaryPart; printf("Enter coefficients a, b, and c of the quadratic equation (ax^2 + bx + c = 0): "); scanf("%f %f %f", &a, &b, &c); // Calculate discriminant discriminant = b * b - 4 * a * c; switch (discriminant > 0? 1 : (discriminant == 0? 0 : - 1)) { case 1: // Real and distinct roots root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and distinct.\n"); printf("Root 1 = %.2f and Root 2 = %.2f\n", root1, root2); break; case 0: // Real and equal roots root1 = root2 = - b / (2 * a); printf("Roots are real and equal.\n"); printf("Root 1 = Root 2 = %.2f\n", root1); break; case - 1: // Complex roots realPart = - b / (2 * a); imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and imaginary.\n"); printf("Root 1 = %.2f + %.2fi and Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart, realPart, imaginaryPart); break; default: printf("Invalid coefficients! Please enter valid values.\n"); break; } return 0; }
for (col = 1; col <= stars; col++) { printf("*"); } printf("\n"); spaces++; stars - = 2; } return 0; }
3. C program to print hollow pyramid - #include <stdio.h> int main() { int rows, spaces, stars, row, col; printf("Enter the number of rows for the hollow pyramid: "); scanf("%d", &rows); spaces = rows - 1; stars = 1; for (row = 1; row <= rows; row++) { for (col = 1; col <= spaces; col++) { printf(" "); } for (col = 1; col <= stars; col++) { if (row == rows || col == 1 || col == stars) { printf("*"); } else { printf(" "); } } printf("\n"); spaces--; stars += 2; } return 0; } 4. C program to print square or rectangle star pattern
#include <stdio.h> int main() { int rows, cols, row, col; printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); for (row = 1; row <= rows; row++) { for (col = 1; col <= cols; col++) { if (row == 1 || row == rows || col == 1 || col == cols) { printf("* "); } else { printf(" "); } } printf("\n"); } return 0; }
5. C program to print hollow square or rectangle star pattern. - #include <stdio.h> int main() { int rows, cols, row, col; printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); for (row = 1; row <= rows; row++) { for (col = 1; col <= cols; col++) { if (row == 1 || row == rows || col == 1 || col == cols) { printf("* "); } else { printf(" "); } } printf("\n");