Question:

How do you program the following in c?

by  |  earlier

0 LIKES UnLike

1. how do you write a program in c to find the factorial of a number.

2. how do write in c to add matrixes using pointers

hi you guys hope am not disturbing you, just that i can't do this on my own thanks alot

 Tags:

   Report

4 ANSWERS


  1. 1.this is for the first answer

    #include<stdio.h>

    #include<conio.h>

    main()

    {

    int a=0,fac=1,i;

    printf("enter a number");

    scanf("%d",&a);

    if(a==0)

    printf("%d",fact);

    else

    for(i=1;i<=a;i++)

    {

    fact=fact*i;

    }

    printf("%d",fact);

    getch();

    }

    2.this is for the second answer

    #include <conio.h>

    #include<stdio.h>

    int main()

    {

    int mat1[10][10],*p1, mat2[10][10], *p2, sum[10][10], *s;

    int i,j, m,n;

    p1=mat1;

    p2=mat2;

    s=sum;

    printf("how many rows and coloumns?\n");

    scanf("%d%d",&m,&n);

    printf("enter matrix 1\n");

    for(i=0; i<m;i++)

    {

    for(j=0;j<n;j++)

    scanf("%d", p1+i*10+j);

    }

    printf("enter matrix 2\n");

    for(i=0; i<m;i++)

    {

    for(j=0;j<n;j++)

    scanf("%d", p2+i*10+j);

    }

    for(i=0; i<m;i++)

    {

    for(j=0;j<n;j++)

    *(s+i*10+j)=*(p1+i*10+j) + *(p2+i*10+j);

    printf("\n");

    }

    printf("Sum matrix\n");

    for(i=0; i<m;i++)

    {

    for(j=0;j<n;j++)

    printf("%d  ", *(s+i*10+j));

    printf("\n");

    }

    getch();

    }


  2. void main()

    {

    int a,i;

    long int fac=1;

    printf("enter number");

    scanf("%d",&a);

    for(i=a;i<=1;i--)

    {

    fac=fac*i;

    }

    printf("factorial of %d is %ld",a,fac);

    }

    long int bcoz it will lead problem with int when find factorial after 9

    good luck for another program

    i m not sure abt that

    sorry


  3. 1)

    int no,fact=1;    \* Declaring the variables

    cout<<"Enter no ";  \* displaying message for input

    cin>>no;  

    while(no>0)          \* using while loop

    {

    fact=fact*no;

    no=no-1;

    }

  4. For the factorial part ...

    try this ..

    int factorial( int number)

    {    if( number == 0)

           return 1;

         else

           return (number * factorial(number -1))

    }

    //  Use the above code for factorial and call using factorial(5) . It

    // should return 120 .

    About the adding matrices ,

    Please note that all matrices( or arrays) ultimately decomposes to pointers .

    Here is the logic , please try it yourself ..

    1. Check for order to be same (most important , else no addition)

    2. Run a loop inside a loop to traverse each matrix and add corresponding elements of two matrices and store them at a third matrix.

    dont worry .. point 2 will be a bit difficult at first , but then .. rome wasnt built in a day. DONT copy paste code form intenet , try it and i am full ysure you will get it. Once you understand it , you will definitely begin to  love C. ...

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions