Vous êtes sur la page 1sur 5

Assignment 03

Q.1 What is a prototype of the function? Q. 2 Is it true that a function may have several declarations, but only one definition? Yes/no Answer: yes ex: int abc (int) declare twice in a program Q 3.Write a square() function and use it to print the squares of the numbers 1-10. Q. 4 Why does the following program report a redeclaration error of function display() main() { display(); } void display() { printf(\nhello); } Answer: here display() is called before it is defined. In such cases the compiler assume that the function display() is declared as int display(); Q.5 There is a error in the flowing code. Add a statement in it to remove it. main() { int a; a=f(10,3.14); printf(%d,a); } f(int aa, float bb) { return((float)aa+bb); } Answer: Add the following function prototype in main() float f(int, float); Q. 6 Decide true or false with explanation. (a) A function cannot be defined inside another function. <true/false> Answer: true (b) In a function two return statement should never occur. <true/false> Answer: false

Assignment 03
Q.7. Point out the error, if any, in the while loop. main() { int i=1; while (i<=5) { printf(%d,i); if(i>2) goto here; } } fun() { here: printf(you are in function now); } answer: goto cannot take control to a different fuction.

Q.8 Define the code is correct or not: (a) void area (int a, int b) { a= a*b; return(a); } (b) main() { int i=0; i=func(); } func( ) { return(10); } Solution:-(a) incorrect, function should return a value (b) correct, Recursive call of main function (c) correct (d) incorrect, function name should not be a reserve key word (c) main() { main(); } (d) main() { for(); } void for () { printf(hello); }

Assignment 03

Q. 9. Give the output of following functions (a) { int a=4,b=3; void swap_1(int,int); swap_1(a,b); printf("a=%d, b=%d",a,b); } void swap_1(int a, int b) void main() (b) int i , j; main() { void swap_1(int,int) i=5; j=6; swap_1(i,j); printf(i= %d, j=%d,i,j); } void swap_1(int a, int b) { i=b; j=a; }

{ int k=a; a=b; b=a; } (c) (d) main() main() { { void fun(); int abc(int); for (j=1;j<=3;j++) int i=abc(10); { printf("%d\n",--i); fun(); } } } void fun () int abc(int i) { { static int i=10; return(i++); i=i+10; } printf("%d",i); } Solutions: (a) a=4,b=3 (scope of variables ) (b) i=6, j=5 (Global declaration of i& j ) (c) 20 30 40 ( static variable initialized only ones (d) ) 9 (In the function I return with post increment) Q. 10 What do you mean my recursion? Make a small program to use recursion. Q.11 Do the following programs in recursion as well as iteration.

sum of n numbers

Assignment 03

gcd of two numbers

lcm of two numbers Solution:lcm using recursion: #include<stdio.h> int lcm(int,int); int main(){ inta,b,l; printf("Enter any two positive integers "); scanf("%d%d",&a,&b); if(a>b) l = lcm(a,b); else l = lcm(b,a); printf("LCM of two integers is %d",l); return 0; } int lcm(inta,int b){ staticint temp = 1; if(temp % b == 0 && temp % a == 0) return temp; temp++; lcm(a,b); return temp; } lcm without recursion: #include<stdio.h> #include<stdio.h> int lcm(int,int); int main(){ inta,b,l; printf("Enter any two positive integers "); scanf("%d%d",&a,&b); if(a>b) l = lcm(a,b); else l = lcm(b,a); printf("LCM of two integers is %d",l); return 0; } int lcm(inta,int b){ int temp = a; while(1){ if(temp % b == 0 && temp % a == 0) break; temp++; } return temp; }

Q.11 Make a calculator that performs following task: (a) All mathematical task ones. (b) All mathematical task until user press e or E (c) Calculator can perform no. of input number until user enter Like a+b+c+d orc*d*e*f Q. 12 write a program to find out root of a equation using Newton-Raphson method which is derived as: xi+l = xi (f(xi)/f(xi)) here xi represent previous root value (for initial it will be given or to be assume), and xi+i

Assignment 03
represent new root calculated by Newton Raphson method. f(xi) : given equation f(xi) : first derivative of the f(xi). task: find out root for the equation: x2+x+3=0 with initial guess xi=1, till error between two consecutive roots fall less than .001 error=(new value- previous)/new

Vous aimerez peut-être aussi