Вы находитесь на странице: 1из 3

Question I (a) Write a simple program to find the size of different basic data types in C. Program: #include<stdio.h> #include<conio.

h> void main() { clrscr(); printf(size of integer= %d,sizeof(int)); printf(\nsize of float= %d,sizeof(float)); printf(\nsize of char= %d,sizeof(char)); printf(\nsize of double= %d,sizeof(double)); printf(\nsize of long= %d,sizeof(long)); getch(); } Output: (b) Write a program in C for showing working of different logical operator in C. Your program should guide users with proper message/menu on the console. Program: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,c,d,m,t; float x,y,j.k; printf(Enter 6 interger numbers: ); scanf(%d\n%d\n%d\n%d\n%d\n%d\n,&a,&b,&c,&d,&m,&t); printf(Enter 4 decimal numbers: ); scanf(%f\n%f\n%f\n%f\n,&x,&y,&j,&k); if((a>b && c<d) || (x>y && j<k)||(m!=t)) printf(its demo for logical operator); else printf(logical error is there in program); getch(); } Output: (c) Write a function to find the area of a triangle whose length of three sides is given. Program: #include<stdio.h> #include<conio.h> #include<math.h> float getArea(float a, float b, float c) { float s=(a+b+c)/2; float area= sqrt(s*(s-a)*(s-b)*(s-c)); return area; } void main() {

clrscr(); float a,b,c; printf(Enter the three sides of Triangle: ); scanf(%f\n%f\n%f,&a,&b,&c); printf(Area of Triangle is %f,getArea(a,b,c)); getch(); } Output: Question 2: (a) Write a C program to print the following triangle: * *** ***** ******* Program: Output: (b) 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. Make necessary assumptions. Program: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[25],i; printf(Enter the marks of 25 students:\n); for(i=1;i<=25;i++) { scanf(%d,&a[i]); } for(i=1;i<=25;i++) { if(a[i]>50) printf(%d marks are greater than 50%\n,a[i]); } getch(); } Output: Question 3: (a) 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. If you are calling by reference it means that compiler will not create a local copy of the variable which you are referencing to. It will get access to the memory where the variable saved. When you are doing any operations with a referenced variable you can change the value of the variable. Call by value is where a copy of an object is placed in the parameter stack. The called function can access and manipulate that copy at will, but it cannot change the original copy because it has no way of knowing where that original copy is located. Call by Reference, on the other hand, is

where the address of an object is placed in the parameter stack. Using extra syntax, the * or the ->, the called function can access and manipulate the original copy at will. Program: #include<stdio.h> #include<conio.h> void swap(int *num1, int *num2) { int temp; temp=*num1; *num1=*num2; *num2=temp; } void main() { clrscr(); int x,y; printf(Enter the number: ); scanf(%d,&x); printf(Enter the number: ); scanf(%d,&y); printf(x= %d y= %d \n,x,y); swap(&x,&y); printf(x= %d y= %d,x,y); getch(); } Output: (b) Write a C program for addition of two 33 matrices. #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[3][3],b[3][3],c[3][3],i,j; printf(Enter the elements for first 3x3 Matrix:\n); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf(%d\n,&a[i][j]); } } printf(Enter the elements for second 3x3 Matrix:\n); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf(%d\n,&b[i][j]); } } printf(Sum of 3x3 Matrix:\n); for(i=0;i<3;i++)

{ for(j=0;j<3;j++) { c=[i][j]=a[i][j]+b[i][j]; printf(%d\n,c[i][j]); } } getch(); } Output: (c) Write a C program for finding GCD of two given numbers. Program: #include<stdio.h> #include<conio.h> int gcd(int m, int n) { while(m!=n) { if(m>n) m=m-n; else n=n-m; } return(m); } void main() { clrscr(); int m,n,gcd(int,int); printf(Enter any 2 values:\n); scanf(%d\n%d,&m,&n); printf(GCD= %d,gcd(m,n)): getch(); } Output: Question 4: (a) Write C programme for followings: i) Counting the number of words in a given string ii) Concatenating two given strings i) #include<stdio.h> #include<conio.h> void main() { clrscr(); char str[1000]; int i=0; printf(Enter the string: ); scanf(%s,str);

while(str[i]!=\0) { i++; } printf(There are %d words.i); getch(); } ii) #include<stdio.h> #include<conio.h> void main() { clrscr(); char a[1000],b[1000]; int i=0,j=0,k; printf(Enter the string: ); scanf(%s,a); printf(Enter the string: ); scanf(%s,b); while(a[i]!=\0) { i++; } while(b[j]!=\0) { j++; } for(k=0;k<j;k++) { a[i+k]=b[k]; } printf(%s,a); getch(); } (b) What is a pointer? Explain pointer arithmetic with example. Also explain use of malloc function in C programming with an example A pointer in C is the address of variable of a function. The advantages of a pointer over using normal variables are: If you pass the pointer to a variable to a function, then that function can modify the value of the variable directly. Pointer variables can also be used in arithmetic expressions. The following operations can be carried out on pointer: 1. Pointers can be incremented or decremented to point to different locations like ptr1=ptr2+3; ptr++; --ptr; 2. If ptr1 and ptr2 are properly declared and initialized pointers, the following operations are valid. res=res+*ptr1; *ptr1=*ptr2+5; prod=*ptr1**ptr2;

quo=*ptr1/*ptr2; 3. Expressions like ptr1==ptr2,ptr1<ptr2 and ptr2!=ptr1 are permissible provided the pointers ptr1 and ptr2 refer to same and related variables. These comparisons are common in handling arrays. A block of memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type of void. This means that we can assign it to any type of pointer. It takes the following form: ptr=(cast-type *) malloc(byte-size); ptr is a pointer of type cast-type. The malloc returns a pointer to an area of memory with size byte-size Example: x=(int *) malloc(100 *sizeof(int)); Question 5: (a) Explain recursion. Also write a C program for Tower of Hanoi problem with a example of 4 disks . The C language allows programmer to write functions that calls themselves and this is called Recursion. #include<stdio.h> #include<conio.h> void TOH(int,char,char,char); void main() { int n; clrscr(); printf(Enter the number of disk: ); scanf(%d,&n); printf(Tower of hannoi problem for %d disk: \n,n); TOH(n,A,B,C); getch(); } void TOH(int n, char A, char B, char C) { if(n<=0) printf(\n Wrong input \n); else if(n==1) printf(\n Move disk from peg %c to peg %c,A,C); else { TOH(n-1,A,C,B); TOH(1,A,B,C); TOH(n-1,B,A,C); } } (a) Write a C program using structure to find students grades in a class. Make the necessary assumptions.

Вам также может понравиться