FYBSC(CS)_Slip 3

 Q 1) A 

i. Write a ‘C’ program to accept two integers from the user and interchange them. Display the interchanged numbers.

#include<stdio.h>
int main()
{
   int x, y, temp;

   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
   temp = x;
   x    = y;
   y    = temp;
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
   return 0;
}

Program Output

Enter the value of x and y
4
9
Before Swapping
x = 4
y = 9
After Swapping
x = 9
y = 4

ii Write ‘C’ program to accept a single digit and display it in words. For example, Input = 9 Output = Nine. 

#include <stdio.h>
void main()
{
   int digit;
   printf("Input Digit(0-9) : ");
   scanf("%d",&digit);
   switch(digit)
   {
    case 0:
           printf("Zero\n");
           break;
    case 1:
           printf("one\n");
           break;
    case 2:
           printf("Two\n");
           break;
    case 3:
           printf("Three\n");
           break;
    case 4:
           printf("Four\n");
           break;
    case 5:
           printf("Five\n");
           break;
    case 6:
           printf("Six\n");
           break;
    case 7:
           printf("Seven\n");
           break;
    case 8:
           printf("Eight\n");
           break;
    case 9:
           printf("Nine\n");
           break;
    default:
           printf("invalid digit. \nPlease try again ....\n");
           break;
    }
}

Program Output:

Input Digit(0-9) : 9
Nine

OR

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

#include <stdio.h>
void main()
{
    int first[10][10],second[10][10],sum[10][10],i,j,m,n;
    printf("\n\nAddition of two Matrices :\n");
    printf("------------------------------\n");  
    printf("Enter the number of rows and columns of matrix\n");
    scanf("%d%d", &m, &n);
    /* Stored values into the array*/
    printf("Input elements in the first matrix :\n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("element - [%d],[%d] : ",i,j);
            scanf("%d",&first[i][j]);
        }
    }   
    printf("Input elements in the second matrix :\n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("element - [%d],[%d] : ",i,j);
            scanf("%d",&second[i][j]);
        }
    }    
    printf("\nThe First matrix is :\n");
    for(i=0;i<m;i++)
    {
      printf("\n");
      for(j=0;j<n;j++)
           printf("%d\t",first[i][j]);
    }
    printf("\nThe Second matrix is :\n");
    for(i=0;i<m;i++)
    {
      printf("\n");
      for(j=0;j<n;j++)
          printf("%d\t",second[i][j]);
    }
    /* calculate the sum of the matrix */  
   for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            sum[i][j]=first[i][j]+second[i][j];
   printf("\nThe Addition of two matrix is : \n");
   for(i=0;i<m;i++)
   {
       printf("\n");
       for(j=0;j<n;j++)
           printf("%d\t",sum[i][j]);
   }
   printf("\n\n");
}

Program Output:

Addition of two Matrices :
------------------------------
Enter the number of rows and columns of matrix
3
2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
element - [2],[0] : 5
element - [2],[1] : 6
Input elements in the second matrix :
element - [0],[0] : 3
element - [0],[1] : 4
element - [1],[0] : 5
element - [1],[1] : 6
element - [2],[0] : 7
element - [2],[1] : 8

The First matrix is :
1       2
3       4
5       6
The Second matrix is :
3       4
5       6
7       8
The Addition of two matrix is :
4       6
8       10
12      14

0 Comments:

Post a Comment