C Language Pic By Abhijeet Wani

1.) Data Types and Variables:

a) Concept-based:

i) Explain the difference between signed and unsigned integers. 

-> In C, signed integers can represent both positive and negative numbers, whereas unsigned integers can only represent non-negative numbers (zero and positive numbers). The sign bit determines the interpretation of the binary representation of a signed integer.

ii) What is the size of the char data type in C? How is it different from int?

-> The size of the char data type in C is 1 byte. It can store a single character or a small integer value. On the other hand, the size of the int data type varies depending on the system architecture, typically 4 bytes on most systems. It can store larger integer values.

b) Write a program:

 i) Write a program to swap two numbers using a temporary variable.

-> #include <stdio.h>

   int main ()

  {

   int a, b, temp;

   printf("Enter two numbers: ");

   scanf("%d %d", &a, &b);

   printf("See Before Swapping Two Numbers: a = %d, b = %d\n", a, b);

   temp = a;

   a = b;

   b = temp;

   printf("Look After Swapping Two Numbers: a = %d, b = %d\n", a, b);

   return 0;

   }

ii) Write a program to find whether a number is prime or not

-> #include <stdio.h>

     int main()

    {

     int num, i, isPrime = 1;

     printf("Enter a positive integer: ");

     scanf("%d", &num);

     if (num <= 1)

    {

      isPrime = 0;

      }

     else 

     {

      for (i = 2; i <= num/2; i++)

     {

       if (num % i == 0)

      {

         isPrime = 0;

         break;

        }

     }

 }

 if (isPrime == 1)

{

  printf("%d is a prime number.\n", num);

  }

 else

 {

  printf("%d is not a prime number.\n", num);

  }

  return 0;

  } 


2.) Constants and Literals:

a) Concept-based:

i) Explain the difference between a constant and a variable.

-> A constant is a value that remains unchanged during the execution of a program, whereas a variable is a named memory location that can hold different values during the execution of a program.

ii) What are the different types of constants in C? Provide examples.

-> In C, there are four types of constants: - Integer constants: Example - 10, -5, 0. - Floating-point constants: Example - 3.14, -0.5, 2.0. - Character constants: Example - 'C', 'c', '@'. - String constants: Example - "Hello", "AbniksBlogs", "123".

b) Write a program:

i) Write a program to calculate the circumference of a circle given its radius.

->#include <stdio.h>

    #define PI 3.1415

    int main()

    {

      float radius, circumference;

      printf("Just Enter Radius Of Circle: ");

      scanf("%f", &radius);

      circumference = 2 * PI * radius;

      printf("Circumference: %.2f\n", circumference);

      return 0;

    }

ii) Write a program to calculate the sum of two numbers using constant variables.

-> #include <stdio.h>

     #define NUM1 10

     #define NUM2 20

     int main()

     {

      int sum;

      sum = NUM1 + NUM2;

      printf("Sum: %d\n", sum);

      return 0;

      }


3.) Operators:

a) Concept-based:

i) Explain the concept of arithmetic operators in C with suitable examples.

-> In C, arithmetic operators are used to carry out mathematical operations. Examples of arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators work with numeric operands.

ii) Distinguish between the pre-increment and post-increment operators.

-> The pre-increment operator (++x) increments the value of 'x' before its use in an expression, while the post-increment operator (x++) increments the value of 'x' after its use in an expression.

 For example:

  int x = 5;

  int y = ++x; // y = 6, x = 6 (pre-increment)

  int z = x++; // z = 6, x = 7 (post-increment)

 

b) Write a program:

i) Write a program to check whether a number is even or odd using the modulus operator.

-> #include <stdio.h>

     int main()

     {

      int num;

      printf("Enter an integer: ");

      scanf("%d", &num);

      if (num % 2 == 0)

      {

          printf("%d is an even number.\n", num);

       }

      else {

                  printf("%d is an odd number.\n", num);

               }

      return 0;

     }

 

ii) Write a program to calculate the area of a circle using the value of π.

->  #include <stdio.h>

      #define PI 3.1415

       int main()

      {

        float radius, area;

        printf("Please enter radius of the circle: ");

        scanf("%f", &radius);

        area = PI * radius * radius;

        printf("Area: %.2f\n", area);

        return 0;

      }


4.) Input and Output Operations:

a) Concept-based:

i) Discuss the difference between printf() and scanf() functions in C.

-> printf() is used to display output to the console or other output devices, whereas scanf() is used to read input from the user. printf() uses format specifiers to format and display the output, while scanf() uses format specifiers to read and assign input values to variables.

ii) How can you read a string input from the user in C? Provide an example.

-> To read a string input from the user, you can use the %s format specifier in scanf().

    For example:

    #include <stdio.h>

    int main()

    {

      char name[50];

      printf("Enter your name: ");

      scanf("%s", name);

      printf("Hello, %s!\n", name);

      return 0;

     }

b) Write a program:

i) Write a program to read two numbers from the user and calculate their sum.

-> #include <stdio.h>

     int main()

    {

      int num1, num2, sum;

      printf("Enter the first number: ");

      scanf("%d", &num1);

      printf("Enter the second number: ");

      scanf("%d", &num2);

      sum = num1 + num2;

      printf("Sum: %d\n", sum);

      return 0;

    }

ii) Write a program to concatenate two strings entered by the user.

-> #include <stdio.h>

     #include <string.h>

     int main()

    {

      char str1[100], str2[100];

      printf("Enter the first string: ");

      scanf("%s", str1);

      printf("Enter the second string: ");

      scanf("%s", str2);

      strcat(str1, str2);

      printf("Concatenated string: %s\n", str1);

      return 0;

    }


5.) Control Flow Statements:

a) Concept-based:

i) Explain the concept of if-else statements in C with suitable examples.

-> The if-else statement is used to do various actions based on various conditions. If the condition inside the if statement is true, the code inside the if block is executed. Otherwise, if the condition is false, the code inside the else block is executed.

For example:

int num = 10;

 if (num > 0)

{

      printf("Number is positive.\n");

  }

else

{

      printf("Number is non-positive.\n");

  }

 

ii) Discuss the difference between while loop and do-while loop in C.

-> The while loop and do-while loop are both used for repetitive execution of a block of code. The main difference is that the while loop tests the condition at the beginning, and if the condition is false, the code inside the loop is never executed. On the other hand, the do-while loop tests the condition at the end, so the code inside the loop is executed at least once, even if the condition is false.

b) Write a program:

i) Write a program to print the Fibonacci series up to a given number using a while loop.

-> #include <stdio.h>

     int main()

    {

      int num, prev = 0, next = 1, temp;

      printf("Enter a number: ");

      scanf("%d", &num);

      printf("This is Fibonacci Series: %d, %d, ", prev, next);

      while (next <= num)

     {

          temp = prev + next;

          printf("%d, ", temp);

          prev = next;

          next = temp;

      }

        printf("\n");

        return 0;

   }

ii) Write a program to find the factorial of a given number using a recursive function.

-> #include <stdio.h>

     int factorial(int num)

    {

     if (num == 0 || num == 1)

    {

     return 1;

     }

     else

    {

      return num * factorial(num - 1);

      }

   }

  int main()

{

   int num;

   printf("Enter a number: ");

   scanf("%d", &num);

   printf("Factorial: %d\n", factorial(num));

   return 0;

   }

  

Post a Comment

If you have any questions do let me know