DOC-20241208-WA0009.

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

C-Programming Lab Manual

1. Write a program to print the memory allocation required for all the datatype in
C Language.
2.
1 /* Program to print the memory allocation required for all the data types in C Language.*/
2 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> ------------------ */
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main()
7 {
8 // Printing the size of various data types in bytes
9 printf("\nSize of int is: %d Bytes", sizeof(int));
10 printf("\nSize of float is: %d Bytes", sizeof(float));
11 printf("\nSize of short int is: %d Bytes", sizeof(short int));
12
13 // Printing the size of signed and unsigned int types
14 printf("\nSize of signed int is: %d Bytes", sizeof(signed int));
15 printf("\nSize of unsigned int is: %d Bytes", sizeof(unsigned int));
16
17 // Printing the size of long and long long int types
18 printf("\nSize of long int is: %d Bytes", sizeof(long int));
19 printf("\nSize of long long int is: %d Bytes", sizeof(long long int));
20 printf("\nSize of double is: %d Bytes", sizeof(double));
21 printf("\nSize of long double is: %d Bytes", sizeof(long double));
22
23 // Printing the size of signed and unsigned short/long int types
24 printf("\nSize of signed short int is: %d Bytes", sizeof(signed short int));
25 printf("\nSize of signed long int is: %d Bytes", sizeof(signed long int));
26 printf("\nSize of unsigned short int is: %d Bytes", sizeof(unsigned short int));
27 printf("\nSize of unsigned long int is: %d Bytes", sizeof(unsigned long int));
28 printf("\nSize of unsigned long long int is: %d Bytes", sizeof(unsigned long long int));
29
30 // Printing the size of char and signed/unsigned char types
31 printf("\nSize of char is: %d Bytes", sizeof(char));
32 printf("\nSize of signed char is: %d Bytes", sizeof(signed char));
33 printf("\nSize of unsigned char is: %d Bytes\n\n", sizeof(unsigned char));
34
35 return 0;
36 }
37

Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam


1
1 /*menu based program to take of input of two values followed input of choice and accordingly
2 perform arithmetic operations like Addition, Subtraction, Multiplication, Modulus, Division,
3 Power( Using Switch Statement)*/
4 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> -------------------- */
5 #include <stdio.h>
6 #include <math.h> // For the power function
7 int main()
8 {
9 int choice;
10 double num1, num2;
11
12 // Display the menu
13 printf("Menu:\n");
14 printf("1. Addition\n");
15 printf("2. Subtraction\n");
16 printf("3. Multiplication\n");
17 printf("4. Modulus\n");
18 printf("5. Division\n");
19 printf("6. Power\n");
20 printf("Please Enter your choice (1-6): ");
21 scanf("%d", &choice);
22 // Input two values
23 printf("Enter the first value: ");
24 scanf("%lf", &num1);
25 printf("Enter the second value: ");
26 scanf("%lf", &num2);
27
28 // Perform the operation based on user's choice
29 switch (choice)
30 {
31 case 1: // Addition
32 printf("Result: %.2lf\n", num1 + num2);
33 break;
34 case 2: // Subtraction
35 printf("Result: %.2lf\n", num1 - num2);
36 break;
37 case 3: // Multiplication
38 printf("Result: %.2lf\n", num1 * num2);
39 break;
40 case 4: // Modulus
41 if ((int)num2 == 0)
42 {
43 printf("Error: Division by zero.\n");
44 }
45 else
46 {
47 printf("Result: %d\n", (int)num1 % (int)num2);
48 }
49 break;
50 case 5: // Division
51 if (num2 == 0)
52 {
53 printf("Error: Division by zero.\n");
54 }
55 else
56 {
57 printf("Result: %.2lf\n", num1 / num2);
58 }
59 break;
60 case 6: // Power
61 printf("Result: %.2lf\n", pow(num1, num2));
62 break;
63 default:
64 printf("Invalid choice.\n");
65 break;
66 }
67
68 return 0;
69 }
70

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam

2
Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam

3
1 /*Program to find out the whether the given number is a perfect square or not*/
2 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> -------------------- */
3 #include <stdio.h>
4 #include <math.h> // For the sqrt function
5 int main()
6 {
7 int num;
8 double sqrt_result;
9 // Input the number
10 printf("Enter a number: ");
11 scanf("%d", &num);
12 // Check if the number is negative
13 if (num <=0)
14 {
15 printf("Negative numbers cannot be perfect squares.\n");
16 return 1;
17 }
18 // Compute the square root of the number
19 sqrt_result = sqrt(num);
20 // Check if the square of the rounded square root equals the original number
21 if (sqrt_result == (int)sqrt_result)
22 {
23 printf("%d is a perfect square.\n", num);
24 }
25 else
26 {
27 printf("%d is not a perfect square.\n", num);
28 }
29 return 0;
30 }
31
32

Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam


4
1 /*Program to find the factorial of a given number*/
2 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> ------------------ */
3 #include<stdio.h>
4 int main()
5 {
6 // Declare variables
7 int i, number, factorial = 1;
8 // Prompt the user to enter a number
9 printf("Enter a number: ");
10 // Read the user input and store it in the variable 'number'
11 scanf("%d", &number);
12
13 // Loop to calculate the factorial of the number
14 for(i = 1; i <= number; i++)
15 {
16 // Multiply the current value of 'factorial' by 'i' in each iteration
17 factorial = factorial * i;18
}
19
20 // Output the factorial result
21 printf("Factorial of %d is: %d", number, factorial);
22 // Indicate that the program ended successfully
23 return 0;24
}
25

Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam

5
1 /*Program to find whether a given number is Palindrome or not.*/
2 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> ------------------ */
3 #include<stdio.h>
4
5 int main()
6 {
7 // Declare variables:
8 int n, r, sum = 0, temp;
9 // Prompt the user to enter a number
10 printf("Please Enter the Number:");
11 // Read the user input and store it in the variable 'n'
12 scanf("%d", &n);
13 // Store the original number in 'temp' for comparison later
14 temp = n;
15
16 // Loop to reverse the digits of the number
17 while(n > 0)
18 {
19 r = n % 10; // Extract the last digit of the number
20 sum = (sum * 10) + r; // Build the reversed number
21 n = n / 10; // Remove the last digit from the number22
}
23
24 // Check if the original number and the reversed number are the same
25 if(temp == sum)
26 printf("The number is a palindrome\n"); // If true, the number is a palindrome
27 else
28 printf("The number is not a palindrome\n"); // If false, the number is not a palindrome
29
30 return 0; // Indicate that the program ended successfully
31 }
32
33

Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam

6
1 /*Program to Print Fibonacci Series up-to given 'n' number of terms*/
2 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> ------------------ */
3 #include <stdio.h>
4 int main()
5 {
6 int num, a = -1, b = 1, c;
7 // Prompt the user to enter a number
8 printf("Enter a number: ");
9 scanf("%d", &num);
10 // Loop to generate and print the Fibonacci series
11 for (int i = 0; i <=num; i++)
12 {
13 c = a + b; // Calculate the next Fibonacci number
14 printf("%d, ", c); // Print the current Fibonacci number
15 a = b; // Update 'a' to the previous 'b'
16 b = c; // Update 'b' to the new Fibonacci number
17 }
18
19 return 0;
20 }
21

Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam

7
1 /*Program to print the first 'n' prime numbers up-to 'n' values */
2 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> ------------------ */
3 #include<stdio.h>
4 int main()
5 {
6 int num, i, count, n; // Declare variables:
7
8 printf("Enter max range: "); // Prompt the user to enter the maximum range
9 scanf("%d", &n); // Read the user input and store it in the variable 'n'
10
11 // Outer loop to iterate through each number from 1 to 'n'
12 for(num = 1; num <= n; num++)
13 {
14 count = 0; // Reset 'count' to 0 for each new number
15
16 // Inner loop to check if 'num' has any divisors other than 1 and itself
17 for(i = 2; i <= num / 2; i++)
18 {
19 if(num % i == 0) // Check if 'num' is divisible by 'i'
20 {
21 count++; // Increment 'count' if a divisor is found
22 break; // Exit the loop early since 'num' is not a prime number23
}
24 }
25
26 // Check if 'num' is a prime number (i.e., it has no divisors other than 1 and itself)
27 if(count == 0 && num != 1)
28 printf("%d ", num); // Print the prime number29 }
30
31 return 0; // Indicate that the program ended successfully
32 }
33

Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam

8
1 /*Program to Print Pascal Triangle for given 'n' Values*/
2 /*----------<Author: K.Dileep Kumar|Asst.Prof|Dept. of CSE|RGUKT-Srikakulam> -------------------- */
3 #include <stdio.h>
4 int main()
5 {
6 int rows, coef = 1, space, i, j; // Declare variables
7 printf("Enter the number of rows: "); // Prompt the user to enter the number of rows
8 scanf("%d", &rows); // Read the user input and store it in the variable 'rows'
9 // Outer loop to iterate over each row
10 for (i = 0; i < rows; i++)
11 {
12 // Inner loop to print spaces for alignment
13 for (space = 1; space <= rows - i; space++)
14 printf(" "); // Print two spaces for each iteration
15 // Inner loop to calculate and print the binomial coefficients for each row
16 for (j = 0; j <= i; j++)
17 {
18 // Calculate the binomial coefficient using the formula: coef = coef * (i - j + 1)
/ j
19 if (j == 0 || i == 0) // The first and last coefficients in each row are always 1
20 coef = 1;
21 else
22 coef = coef * (i - j + 1) / j;
23 printf("%4d", coef); // Print the coefficient with a width of 4 characters for
proper alignment
24 }
25 printf("\n"); // Move to the next line after printing all coefficients in the current
row
26 }
27 return 0; // Indicate that the program ended successfully
28 }
29

Output:

Author: K.Dileep Kumar |Asst. Professor |Dept. of CSE | RGUKT-Srikakulam

You might also like