FYBSC(CS)_Slip 5

  Q 1) A 

i.  Write a ‘C’ program to check whether the given year is leap year or not.

#include <stdio.h>
int main()
{
   int year;
   printf("Enter a year: ");
   scanf("%d", &year);
   if (year % 400 == 0)
   {
      printf("%d is a leap year.", year);
   }
   // not a leap year if visible by 100 but not divisible by 400
   else if (year % 100 == 0)
   {
      printf("%d is not a leap year.", year);
   }
   // leap year if not divisible by 100 but divisible by 4
   else if (year % 4 == 0) {
      printf("%d is a leap year.", year);
   }
   // all other years are not leap year
   else {
      printf("%d is not a leap year.", year);
   }
   return 0;
}

Program Output:

Enter a year: 2016
2016 is a leap year.

ii Write a C program to display all numbers between two given numbers.

#include<stdio.h >  
int main()  
{  
    int num1, num2;  
    printf("Enter 2 positive numbers\n");  
    scanf("%d%d", &num1, &num2);  
 
    printf("Natural numbers between %d and %d are:\n", num1, num2);  
    while(num1 <= num2)  
    {  
        printf("%d  ", num1);  
        num1++;  
    }  
    printf("\n");  
    return 0;  

Program Output:

Enter 2 positive numbers
5
16
Natural numbers between 5 and 16 are:
5  6  7  8  9  10  11  12  13  14  15  16

OR

Q 1) Write ‘C’ program to subtract two matrices of order mXn

#include<stdio.h>
int main()
{
    int MAT1[5][5];  
    int MAT2[5][5];  
    int SUB[5][5];  
    int row, col,m,n;
    printf("\nEnter The Dimensions mXn\n");
    scanf("%d%d",&m,&n);
    printf("Enter elements in matrix MAT1 : \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            scanf("%d", &MAT1[row][col]);
        }
    }
    printf("\nEnter elements in matrix MAT2 : \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            scanf("%d", &MAT2[row][col]);
        }
    }
    printf("\nGiven matrix MAT1 is: \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            printf("%d\t", MAT1[row][col]);
        }
        printf("\n");
    }
    printf("\n\nGiven matrix MAT2 is: \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            printf("%d\t", MAT2[row][col]);
        }
        printf("\n");
    }
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            SUB[row][col] = MAT1[row][col] - MAT2[row][col];
        }
    }
    printf("\nDifference of two matrices MAT1-MAT2 = \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            printf("%d\t", SUB[row][col]);
        }
        printf("\n");
    }
    return 0;
}

Program Output:

Enter The Dimensions mXn
3
2
Enter elements in matrix MAT1 :
3
7
8
6
9
5
Enter elements in matrix MAT2 :
7
4
3
5
3
1
Given matrix MAT1 is:
3       7
8       6
9       5

Given matrix MAT2 is:
7       4
3       5
3       1
Difference of two matrices MAT1-MAT2 =
-4     3
5      1
6      4

0 Comments:

Post a Comment