C program 2021 Makeup

Uncategorized

Q.no 1 . Explain the different types of data types with examples.

In C programming, data types are declarations for variables. This determines the type and size of data associated with variables.
Type of data types:

  1. Primary data types:These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.
  2. Derived data types:Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointers. These are discussed in details later.

For more details follow notes section of site.

Q.no 2

    // C program to find the sum of two matrices of order 2*2
    #include <stdio.h>
    int main()
    {
      float a[2][2], b[2][2], result[2][2];
      // Taking input using nested for loop
      printf("Enter elements of 1st matrix\n");
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
        {
          printf("Enter a%d%d: ", i + 1, j + 1);
          scanf("%f", &a[i][j]);
        }
      // Taking input using nested for loop
      printf("Enter elements of 2nd matrix\n");
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
        {
          printf("Enter b%d%d: ", i + 1, j + 1);
          scanf("%f", &b[i][j]);
        }
      // adding corresponding elements of two arrays
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
        {
          result[i][j] = a[i][j] + b[i][j];
        }
      // Displaying the sum
      printf("\nSum Of Matrix:");
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
        {
          printf("%.1f\t", result[i][j]);
          if (j == 1)
            printf("\n");
        }
      return 0;
    }

Q.no 3

Dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

Q.4

The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.
C strcmp() Prototype
The function prototype of strcmp() is:
int strcmp (const char* str1, const char* str2);

Example: C strcmp() function
#include <stdio.h>
#include <string.h>
int main() {
  char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
  int result;
  // comparing strings str1 and str2
  result = strcmp(str1, str2);
  printf("strcmp(str1, str2) = %d\n", result);
  // comparing strings str1 and str3
  result = strcmp(str1, str3);
  printf("strcmp(str1, str3) = %d\n", result);
  return 0;
}

 strcpy()

The strcpy() function in C programming to copy strings (with the help of an example).

The function prototype of strcpy() is:
char* strcpy(char* destination, const char* source);
Example
#include <stdio.h>
#include <string.h>
int main() {
  char str1[20] = "C programming";
  char str2[20];
  // copying str1 to str2
  strcpy(str2, str1);
  puts(str2); // C programming
  return 0;
}

Write a program to count a number of vowels in a given string.

#include <stdio.h>
int main()
{
  int c = 0, count = 0;
  char s[1000];
  printf("Input a string\n");
  gets(s);
  while (s[c] != '\0') {
    if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' || s[c] == 'I' || s[c] =='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U')
      count++;
    c++;
  }
  printf("Number of vowels in the string: %d", count);
  return 0;
}

Q.5 Describe the types of  control statement.

Control statements enable us to specify the flow of program control; ie, the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another.
There are four types of control statements in C:

  1. Decision making statements
  2. Selection statements
  3. Iteration statements
  4. Jump statements

For more details follow notes section of site.
 

Q.7

S.No Array Structure
1 Array is collection of homogeneous data. Structure is the collection of heterogeneous data.
2 Array data are access using index. Structure elements are access using . operator.
3 Array allocates static memory. Structures allocate dynamic memory.
4 Array element access takes less time than structures. Structure elements takes more time than Array.

 
 





Q.8

Write a c program for a  menu driven program which has following option:

a. Factorial of a number

b. Odd or Even

c. Exit

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
 int choice, num, i;
 unsigned long int fact;
 while(1) {
   printf("a.Factorial : \n");
    printf("b.Odd/Even : \n");
   printf("4.Exit\n");
   printf("\nYour choice :? ");
   scanf("%d",&amp;choice);
   switch(choice) {
      case 1:
            printf("\nEnter Number :");
            scanf("%d",&amp;num);
            fact = 1;
            for(i=1;i<=num;i++)
              fact = fact * i;
              printf("\n Factorial value = %lu\n",fact);
              break; // Takes control out of switch
      case 2:
            printf("\nEnter Number:");
            scanf("%d",&amp;num);
            if(num %2==0)
              printf("\n Even Number.\n");
            else
              printf("\nOdd Number.\n");
              break; //Takes control out of switch
      case 3:
            exit(0); //Terminates program execution
   }
 }
  getch();
}

 

Leave a Reply

Your email address will not be published.