Babalram Ass. of C
Babalram Ass. of C
-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(); }
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(),
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)
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(); }