0% found this document useful (0 votes)
56 views6 pages

Babalram Ass. of C

The document contains 6 questions and their answers related to C programming. 1) A C program to print all prime numbers between two user specified limits using a while loop. 2) A C program to print a pattern of increasing and decreasing numbers using nested for loops. 3) The difference between formatted and unformatted I/O in C, explained with examples of each. Formatted I/O uses conversion specifiers while unformatted works only with characters. 4) Explanation of call by value and call by reference in C with examples to find the factorial of a number using each method. Call by value copies arguments while call by reference passes a reference. 5) A recursive and iterative

Uploaded by

Balram Jha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

Babalram Ass. of C

The document contains 6 questions and their answers related to C programming. 1) A C program to print all prime numbers between two user specified limits using a while loop. 2) A C program to print a pattern of increasing and decreasing numbers using nested for loops. 3) The difference between formatted and unformatted I/O in C, explained with examples of each. Formatted I/O uses conversion specifiers while unformatted works only with characters. 4) Explanation of call by value and call by reference in C with examples to find the factorial of a number using each method. Call by value copies arguments while call by reference passes a reference. 5) A recursive and iterative

Uploaded by

Balram Jha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME-BALRAM JHA SECTION-K6005 Reg. no.-11003822 ROLL NO.

-A04
Q. 1 WAP to print all prime numbers between two specified limits i.e., starting as well as terminating limit should be specified by the user.

#include<stdio.h> void main() {int x=0,n=0,i=1,j=1; clrscr(); while(n<25){ J=1; X=0; while(j<=i) {if(i%j==0) x++; j++;} if(x==2) {printf(%d,i); n++;} i++;} getch(); }

Q. 2 Write C program to print the following pattern:


1 121 12321 1234321 123454321

Ans. #include<stdio.h> void main() { int x,y,z; clrscr(); for(x=1;x<6;x++) { for(y=1;y<6-x;y++) { printf( ); } for(z=1;z<=x;z++) { printf(%2d,z); }for(y=1,z=x-y;y<x,z>0;y++,z--) { printf(%2d,z); } printf(\n); } getch(); } Q. 3 What is difference between Unformatted & Formatted I/O. Elaborate using different

examples? Formatted I/O:The formatted input output functions read and write all types of data values. They require conversion symbol to identify the data type, The formatted function return the values after execution, The return value is equal to the number of variables successfully read/written, For formated function we use input as scanf() and output as printf(),

Example using formatted function: #include<stdio.h> void main()


{ Int a; Printf(hello to the world of c language) Scanf(%d,a); } Unformatted input output:The unformatted i/o functions only work with the character data type, They do not require require conversion symbol for identification of data types, There is no need to conver the data, The return values always be same, They contain getch() , getche(), getchar(), gets(), as input and putch(), putchar(), puts(), as output. Examples containing unformatted function are:#include<stdio.h> void main() {char ch[20]; Int c=0; Clrscr(); While((ch[c]=getchar())!=\n) C++; Ch[c]=\0; Printf(\n%s,ch); } Q. 4 Explain the concept of Call by value & Call by reference. Explain both concepts using example to find factorial of any number.

Ans. Call by value method: It copies the values of actual parameters into the formal parameters, i.e ,the function creates its own copy of argument values and then uses them. #include<stdio.h> int fact(int x); int x; void main() printf(enter the value whose factorial is to be calculated); scanf(%d,&x); factorial=fact(x); printf(the factorial=%d,factorial); getch(); } int fact(int x) { int fact=1; While(x>1) { fact=f*x; x=x-1; } Return(f); } Call by refrence : It passess a value to the function being called ,a reference to the original variable is passed.when a function is called by reference ,then the formal parameters become references to the actual parameters in the calling function. #include<>stdio.h> int n; int fact(int *a); void main() { int factorial; printf(enter the value whose factorial is to be calculated); scanf(%d,&a); factorial=fact(&a); printf(the factorial=%d,factorial); getch(); } int fact(int *a)

{ int fact=1; While(*a>=1) { fact=fact*(*a); a=*a-1; } Return(fact); }

Q. 5 Write a recursive function & simple function to print Fibonacci Series. Ans. # include<stdio.h> void main( ) { int terms,i; printf(enter the no of terms); scanf(%d, &terms ); for (i=0;i<terms; i++) printf(%d,fib( i)); printf(\n); } int fib ( int n) { if(n==0 | | n==1) else return(fib (n-1)+fib(n-2)); }

Q. 6 Write a program to delete an element from specified location in 1-d array. Ans. #include<stdio.h> void main(){ int x[5]={20,20,40,40,50};

int item,i,flag,loc; clrscr(); printf("item="); scanf("%d",&item); for(i=0;i<5;i++) { if(item==x[i]) { x[i]=0; printf("num= %d",x[i]); flag=1; } } if(flag==1) printf("not found"); }else{ printf(found); } Loc=i; printf(%d,loc); for(i=loc;i<=4;i++) { item[i]=item[i+1]; } item[i]=0; printf(%d,item[i]); getch(); }

You might also like