Showing posts with label IGNAU mca assignment. Show all posts
Showing posts with label IGNAU mca assignment. Show all posts

Monday, September 12, 2011

Addition of two 3x3 matrix

Q. Write a C program for addition of two 3x3 matrix.

Ans.


#include<stdio.h>
#include<conio.h>
int main()
{
 int mata[3][3],matb[3][3],matc[3][3];
 int r,c,k;
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter first matrix : ");
    scanf("%d",&mata[r][c]);
  }
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter second matrix : ");
    scanf("%d",&matb[r][c]);
  } 
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    matc[r][c]=0;
    for(k=0; k<3;k++)
       matc[r][c]=mata[r][c] + matb[r][c];
  }
 }
 printf("New addition matrix : \n"); 
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
     printf(" %d",matc[r][c]);
  printf("\n");
 }
 getch();
 return 0;
}

Output of above program :

Enter first matrix :
  1 2 3
  4 5 6
  7 8 9
Enter second matrix :
  2 1 7
  4 6 3
  8 1 1

New addition matrix :
  3  3  10
  8  11 7
  15 9  10

GCD or HCF of two numbers

Q. Write a C program for finding GCD of two given numbers.

Ans.

 /*c program for finding GCD/HCF of two numbers*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int n1,n2,rem;
  printf("Enter first number : ");
  scanf("%d",&n1);
  printf("Enter second number : ");
  scanf("%d",&n2);
  if(n2>n1)
  {
    n1=n1+n2;
    n2=n1-n2;
    n1=n1-n2;
  }
  do{
      rem=n1%n2;
      n1=n2;
      n2=rem;
    }while(rem!=0);
  printf("\nGCD of two number is %d",n1);
  getch();
  return 0;
 }

/************** OUTPUT ***************
Enter first number : 65
Enter second number : 45


GCD of two number is 5
**********************************/

call by reference swap program

Q. what is calling by reference? How it is different from call by value? Write a C function to swap two given numbers using call by reference mechanism.

Ans.

When an argument is passed by reference, the caller actually allows the called function to modify the original variable's value.

  • Sends the address of a variable to the called function.
  • Use the address operator(&) in the parameter of the called function.
  • Anytime we refer to the parameter, therefore we actually referring to the original variable.
  • If the data is manipulated and changed in the called function, the original data in the function are changed.
/*swap function to interchange values by call by reference*/
#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
 int n1,n2;
 printf("Enter first number (n1) : ");
 scanf("%d",&n1);
 printf("Enter second number (n2) : "); 
 scanf("%d",&n2);
 printf("\nBefore swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 swaping(&n1,&n2);
 printf("\nAfter swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 getch();
 return 0;
}
void swaping(int *x, int *y)
{
  int z;
  z=*x;
  *x=*y;
  *y=z;
}

/***************** OUTPUT **************/
call by reference swap C program
Swap using call by reference

Example of Array

Q. Write a C program to read the internal test marks of 25 students in a class and show the number of students who have scored more than 50% in the test.

Ans.
/*We assume that maximum marks in internal test is 500. */
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int main()
{
 int arr[SIZE];
 int i,j,max=0,min=0;
 for(i=1; i<=SIZE; i++)
 {
   printf("Enter %d student marks : ",i);
   scanf("%d",&arr[i]);
 }
 for(i=1; i<=SIZE; i++)
 {
  if(arr[i]/5 > 50)
  {
    max++;
    printf("\nMarks which scored more than 50%% is %d",arr[i]);
  }
  else
  {
    min++;
    printf("\nMarks which scored less than 50%% is %d",arr[i]);
  }
 } 
 printf("\n");
 printf("\nTotal number of students who scored more  than 50%% = %d",max);
 printf("\nTotal number of students who scored less than 50%% = %d",min);
 getch();
 return 0;
}


      Output of the above program :
Enter 1 student marks : 450
Enter 2 student marks : 152
Enter 3 student marks : 266
Enter 4 student marks : 65
Enter 5 student marks : 123

Marks which scored more than 50% is 450
Marks which scored less than 50% is 152
Marks which scored more than 50% is 266
Marks which scored less than 50% is 65
Marks which scored less than 50% is 123

Total number of students who scored more than 50% =  2 
Total number of students who scored less than 50% =  3 

Sunday, September 4, 2011

Star (*) Praymid

Q. Write a C program to print the following triangle:







*








* * *






* * * * *




* * * * * * *


* * * * * * * * *
* * * * * * * * * * *




Ans.

 /*c program for star pyramid*/
 #include<stdio.h>
 #include<conio.h>

 int main()
 {
  int num=6,r,c,sp;

  for(r=1; num>=r; r++)
  {
   for(sp=num-r; sp>=1; sp--)
       printf(" ");
   for(c=r; c>=1; c--)
        printf("*");
   for(c=r; c>1; c--)
        printf("*");
   printf("\n");
  }

  getch();
  return 0; 
 }


/***************OUTPUT***************








*








***






*****




*******


*********
***********



***************************************/