DOC-20241208-WA0009.
DOC-20241208-WA0009.
DOC-20241208-WA0009.
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:
2
Output:
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:
Output:
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:
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:
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:
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: