babalram ass. of c

Upload: balram-jha

Post on 04-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Babalram Ass. of c

    1/6

    NAME-BALRAM JHA

    SECTION-K6005

    Reg. no.-11003822ROLL NO.-A04

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

    #include

    void main()

    {int x=0,n=0,i=1,j=1;

    clrscr();

    while(n

  • 7/31/2019 Babalram Ass. of c

    2/6

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

    11 2 1

    1 2 3 2 11 2 3 4 3 2 1

    1 2 3 4 5 4 3 2 1

    Ans.

    #includevoid main(){int x,y,z;clrscr();for(x=1;x

  • 7/31/2019 Babalram Ass. of c

    3/6

    Example using formatted function:

    #include

    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

    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 tofind factorial of any number.

  • 7/31/2019 Babalram Ass. of c

    4/6

    Ans.

    Call by value method: It copies the values of actual parameters into the formal parameters, i.e ,thefunction creates its own copy of argument values and then uses them.

    #include

    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 theactual parameters in the calling function.

    #includestdio.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)

  • 7/31/2019 Babalram Ass. of c

    5/6

    {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.

    # includevoid main( )

    {int terms,i;

    printf(enter the no of terms);scanf(%d, &terms );for (i=0;i

  • 7/31/2019 Babalram Ass. of c

    6/6

    int item,i,flag,loc;clrscr();

    printf("item=");scanf("%d",&item);for(i=0;i