A24 homework

download A24 homework

of 14

Transcript of A24 homework

  • 8/7/2019 A24 homework

    1/14

    ASSIGNMENT: 2

    CAP101

    FOUNDATION

    OF

    COMPUTER

    PROGRAMMING

    SUBMITTED BY:

    SUBMITTED

    Mrs. Richa Malhotra

    SAKSHI BANSAL

    E3004

    A24

  • 8/7/2019 A24 homework

    2/14

    PART: A

    Q:-1. Write a function itob(n, s,b) that converts integer n into

    base b character representation into string s.E.g. Itob (n, s

    16) format n as a hexadecimal integer ins.

    Ans:-

    #include

    #include

    #include

    Void itob (int n, int s, int b)

    {

    Int i; s=0;

    for ( i=0; n! =0; i++)

    {

    s=s+ ((pow (10, i))*(n %b));

    n=n/b;

    }

    Switch (b){

    Case 2:

    Printf ("\n the binary num ber is: %d", s);

    Break;

    Case 8:

    Printf ("\n the o ctal number is: %d", s);

    break;

    Case 16:

    Printf ("\n the hexad ecimal number is: %d", s);

    break;

    }

    }

    Void main ()

    {

    int n, i ,s ,b;

    clrscr ();

  • 8/7/2019 A24 homework

    3/14

    printf ("enter the value for number:");

    scanf ("%d", &n);

    printf ("enter the value which do you w ant to convert the no.");

    scanf ("%d", &b);

    itob (n, s, b);

    getch ();

    }

    Q:-2. Write a program to f ind the sum of reciprocals of f irst n

    natural nos. using recursion.

    Ans:-

    #include

    #include

    void main ()

    {

    float reciprocal (float n);

    f loat no, rec;

    clrscr ();

    printf("Enter the reciprocals: ");

    scanf("%f",&no);

    rec=reciprocal(no);

    printf("\n sum of reciproc als is %f", no, rec);

    getch();

    }

    float reciprocal(float n)

    { static float sum =0;

    if(n==1){ return 1;

    }

    sum=sum+(1/ n);

    reciprocal(n-1);

    return sum;

    }

    Q:-3. Explain with example how a recursive function works?

  • 8/7/2019 A24 homework

    4/14

    Ans:-RECURSIVE FUNCTION:-

    When a function cal ls i tself then it is cal led recursive function. . Some

    problems are m ost easi ly solved by recursion, usual ly those in which you

    act on data and then act in the same way on the result. All those

    problems which can be solved by the looping control statements can beimplemented using recursion.

    NECESSARY CONDITIONS:-

    For the prope r functioning recursion shou ld be satisfied the fol lowing two

    conditions:

    There mu st exist at least one point which calls same function again.

    There must exist at least one another point which stops the further

    calling.

    When these conditions wil l be fol lowed in the function then this function

    wil l eventually end and produce an answ er.

    When these conditions wi ll not be fo llowed then wi ll be unexpected or

    the function wil l never end and p roduce a runtime fai lure.

    EXAMPLE :-

    For example w rite a program to print the factorial of a given numb er:

    #include

    #include

    void m ain();

    {

    int no,fact;

    int factorial(int n);

    clrscr();

    printf(Enter the number to f ind the factorial:);

    scanf(%d,&no);

    fact = factorial(no);

    printf(The factorial of the num ber is = %d,fact);

    getch();

  • 8/7/2019 A24 homework

    5/14

    }

    int factorial(int n)

    { int f=1;

    if(no==1)

    {

    return 1;

    }

    f=n*factorial(n-1);

    return f;

    }

    In the statement the function is called as follows:

    f=n*factorial(4)// f=24*5

    f=n*factorial(3)// f=6*4

    f=n*factorial(2)// f=2*3

    f=n*factorial(1)//f=1*2

    So, in the last the value of f=120, is returned to the Main() function andprinted on the screen.

    PART B

    Q:-4. Write a program to insert an element in an unsorted list

    (on the specified location).

    Ans:-

    #include

    #include

    void main()

    {

    int i, list[20], t, no, loc;

  • 8/7/2019 A24 homework

    6/14

    printf("enter the limit for the list:");

    scanf("%d",& t;

    if(t20)

    {

    Printf ("\n it should be

  • 8/7/2019 A24 homework

    7/14

    list[i]=no;

    printf("\nThe list after insertion:\n");

    for(i=1; i

  • 8/7/2019 A24 homework

    8/14

    }

    printf("\nthe matrix is :\n");

    for(i=1; i

  • 8/7/2019 A24 homework

    9/14

    for(j=1; j

  • 8/7/2019 A24 homework

    10/14

    for(j=1; j

  • 8/7/2019 A24 homework

    11/14

    printf("Enter the number of rows and colomns of 2nd matrix:");

    scanf("%d%d", &r2, &c2);

    printf("\nEnter the elements of the matrix 1:");

    enter_matrix(mat1,r1,c1);

    printf("\nEnter the elements of matrix 2:");

    enter_matrix(mat2,r2,c2);

    printf("\n The matrices are :\n");

    printf("\n the first matrix=");

    print_matrix(mat1,r1,c1);

    printf("\n the second matrix is=");

    print_matrix(mat2,r2,c2);

    printf("\n\n MENU :\n\n");

    printf ( "\n 1. Addition.");

    printf ("\n 2. Subtraction.");

    printf ("\n3. Multiplication");

    printf("\n0. Exit\n\n");

    do

    {

    printf("\n enters the no. From menu:\n");

    scanf("%d",&no);

    switch(no)

    {

    case 1:

  • 8/7/2019 A24 homework

    12/14

    if( r1!=r2 || c1!=c2 )

    {

    printf("\nsum is not possible ");

    break;

    }

    printf("\n The sum of matrices is :\n");

    for(i=1; i

  • 8/7/2019 A24 homework

    13/14

    for(i=1; i

  • 8/7/2019 A24 homework

    14/14

    for(k=1; k