Computer Programming And Utilization(CPU) - Dec - 2011

Question 2

(b) Write a program in C to generate Fibonacci series like following : 1, 1, 2, 3, 5, 8, 13 …Generate 20 such numbers.


#include<stdio.h>
#include<conio.h>
main()
{
 int i,a=1,b=0,sum;
 clrscr();
 for(i=0;i<20;i++)
 {
  sum = a + b;
  a = b;
  b = sum;
  printf("%d ",sum);   
 } 
 getch();
}
    

Question 3

(b) Write a program to display following pattern using nested for loops.
* * * * *
* * * *
* * *
* *
*



#include<stdio.h>
#include<conio.h>
main()
{
 int i,j;
 clrscr();
 for(i=5;i>0;i--)
 {
  for(j=i;j>0;j--)
  {
   printf("* ");
  }
  printf("\n");
 }
 getch();
}
    

Question 4

(b) Explain how string is defined in C. Write user defined functions for the following :
(i) strlen() : to find length of string.
(ii) strcat() : to concate two strings.



#include<stdio.h>
#include<conio.h>
char* strCat(char *,char *);
main()
{
 char *s1,*s2,*s3;
 int len;
 clrscr();
 s1 = "Jaydip";
 s2 = "Panchal";
 len = strLen(s1);
 s3 = strCat(s1,s2);
 printf("\nLength of String : %d",len);
 printf("\nConcatation of Two String : %s",s3);
 getch();
}
int strLen(char *s)
{
 int i;
 for(i=0;s[i] != '\0';i++);
 return i;
}
char* strCat(char *s1, char *s2)
{
 int i,j;
 for(i=0;s1[i] != '\0';i++);
 for(j=0;s2[j] != '\0';j++,i++)
  s1[i] = s2[j];
 return s1;
}
    

Question 5

(a) Write a C program to Add two 3 X 3 Matrix.


#include<stdio.h>
#include<conio.h>
main()
{
 int matrix1[3][3] = {{15,25,23},{45,25,35},{15,24,12}},matrix2[3][3] = {{85,36,12},{36,32,14},{78,12,74}};
 int i,j;
 clrscr();
 printf("\n\n First Matrix : \n\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d ",matrix1[i][j]);
  }
  printf("\n");
 }
 printf("\n\n Second Matrix : \n\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d ",matrix2[i][j]);
  }
  printf("\n");
 }
 printf("\n\n Sum of Two Matrix : \n\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d ",matrix1[i][j]+matrix2[i][j]);
  }
  printf("\n");
 }
 getch();
}
    

Question 6

(a) Write a C program to read 10 numbers from user and find Sum, Maximum and Average of them.


#include<stdio.h>
#include<conio.h>
main()
{
 int number[10],sum=0,maximum = 0,i;
 float average;
 clrscr();
 for(i=1;i<=10;i++)
 {
  printf("Enter Number %d : ",i);
  scanf("%d",&number[i]);
 }
 printf("\n\nSum of all Numbers : ");
 for(i=1;i<=10;i++)
 {
  sum += number[i];
 }
 printf("%d",sum);
 printf("\n\nAverage of all Numbers : ");
 average = sum/10;
 printf("%f",average);
 printf("\n\nMaximum Number is : ");
 for(i=1;i<=10;i++)
 {
  if(maximum < number[i]) 
   maximum = number[i];
 }
 printf("%d",maximum);
 getch();
}
    

No comments:

Post a Comment