Paper solution of second sessional exam

// Q-1 (C-1) What will be the output of following programs
#include<stdio.h>
int main()
{
    int a[5]={5,1,15,20,25};
    int i,j,m;
    clrscr();

    i=++a[1];    // i=2
    j=a[1]++;    // j=2
    m=a[i++];    // m[i]=m[2]=15 then i++ = i+1 = 2+1 = 3 so, i=3
    printf("\n\n%d, %d, %d",i,j,m);
    getch();
    return 0;
}

/*
    o/p: 3, 2, 15
*/
Click Here to Download

// Q-1 (c-[2])

void main()
{
    int i,j;
    clrscr();
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d",i|j);    // binary or calculation
        printf("\n");
    }
    getch();
}
/*                      i  or  j      i  or  j      i  or   j
  o/p:   0 1 2    00 + 00    00 + 01   00 + 02
          1 1 3     01 + 00    01 + 01   01 + 02
          2 3 2     02 + 00    02 + 01   02 + 02

*/
Click Here to Download

// Q-2 (d) Write a program to print following pattern  using while structure
    *
  * *
 * * *
* * * *
 * * *
  * *
   *

#include<stdio.h>
#include<conio.h>

void main()
{
    int no,i=1,j,sp;
    clrscr();

    printf("Enter number of rows : ");
    scanf("%d",&no);
    while(i<=no)
    {
    sp=no-i;
    while(sp>=1)
    {
        printf(" ");
        sp--;
    }

    j=1;
    while(j<=i)
    {
        printf("* ");
        j++;
    }
    printf("\n");
    i++;
    }

    i=1;
    while(i<=no)
    {
    sp=i;
    while(sp>=1)
    {
        printf(" ");
        sp--;
    }

    j=1;
    while(j<=no-i)
    {
        printf("* ");
        j++;
    }
    printf("\n");
    i++;
    }
  getch();
}
Click Here to Download

// Q-3 (a) Write a program to read a given array of real numbers, print it and

determine the lowest value and its position from given array.

#include<stdio.h>
#include<conio.h>

void main()
{

    int i,min_index,no;
    float num[50],min;
    clrscr();

    printf("\n\n\n\t Program for finding Minimum number from an

array");

    printf("\n\n\n\t\t\t Enter the size of array : ");
    scanf("%d",&no);

    for(i=0;i<no;i++)
    {
        printf("\n\n\t\t\t Enter number for num[%d] = ",i);
        scanf("%f",&num[i]);
    }

    for(i=0;i<no;i++)
    {
        printf("\n\t\t\t num[%d] = %f",i,num[i]);   
    }

    min=num[0];

    for(i=0;i<no;i++)
    {
        if(num[i]<=min)
        {
            min=num[i];
            min_index=i;
        }
    }

    printf("\n\n\t\t\t Minimum number is %f at index %d",min,min_index);

    getch();
}
Click Here to Download

// Q-3 (b) Write a program to evalute the given series -1! +2! -3! +4! -5!...+n!

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,fact=1,sumO=0,sumE=0,sum,no;
    clrscr();
    printf("Enter the No : ");
    scanf("%d",&no);

    for(i=1;i<=no;i++)
    {
        fact = fact * i;
        if(i%2==0)
        {
            printf("+%d ",fact);
            sumE = sumE + fact;

        }
        else
        {
            printf("-%d ",fact);
            sumO = sumO - fact;
        }
    }
    sum = sumO+sumE;
    printf("\n\nSum of the series = %d",sum);

    getch();
}
Click Here to Download

// Q-3 (a) Write a program to read a given array of real numbers, print it and count the number of odd values and even value from the given array.

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[30],i,no,countO=0,countE=0,countZ=0;
    clrscr();

    printf("Enter the size of array : ");
    scanf("%d",&no);

    // This loop is used for input
    for(i=0;i<no;i++)
    {
        printf("Enter the value for a[%d] = ",i);
        scanf("%d",&a[i]);
    }

    // This loop is used for print the array and logic of the odd and even value count
    printf("\n\n Your enterd array is : ");
    for(i=0;i<no;i++)
    {
        printf("\n a[%d] = %d",i,a[i]);
        if(a[i]==0)
            countZ=countZ++;

        else if(a[i]%2==0)
            countE = countE++;

        else if(a[i]%2!=0)
            countO = countO++;
    }
    printf("\n\n No of odd values = %d",countO);
    printf("\n\n No of even values = %d",countE);
    printf("\n\n No of zero = %d",countZ);
    getch();

}
Click Here to Download

/* Q-3 (b) write a program to read two matrices and print them. Determine the product of these two matrices and print it  */

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10][10],b[10][10],mul[10][10],row,col,i,j,k;
    clrscr();

    printf("Enter number of rows for matrix : ");
    scanf("%d",&row);

    printf("Enter number of columns for matrix : ");
    scanf("%d",&col);
    if(row==col)
    {

    printf("Enter values for First matrix \n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("Enter value for a[%d][%d] = ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
    printf("\n\nEnter values for Second matrix \n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("Enter value for b[%d][%d] = ",i,j);
            scanf("%d",&b[i][j]);
        }
    }

    printf("Your First matrix is : \n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }

    printf("\n\nYour second matrix is : \n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d ",b[i][j]);
        }
        printf("\n");
    }

    printf("Multiplication of the Matrices:\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            mul[i][j]=0;

            for(k=0;k<row;k++)
                mul[i][j]+=a[i][k]*b[k][j];
            printf("%d\t",mul[i][j]);
        }
        printf("\n");
    }
   }
   else
   {
    printf("Sorry....\nMatrix multiplication can't be possible..\nyou must need same number of rows and columns");
   }
   getch();
}
Click Here to Download

1 comment: