1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PROGRAMMiNG EXERCiSES

Discussion in 'Meet and Greet' started by Luisa Mia Peralta, Sep 25, 2011.

  1. Luisa Mia Peralta

    Luisa Mia Peralta New Member

    Joined:
    Sep 25, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    guys please help me answer this :)

    1) N factorial can be defined as the product of all integer from 1 to N and it is denoted by the symbol N!.0! (zero factorial) is defined a 1. write a program that will input N and would call the function factorial that will return N factorial. (determine first if N is a non-negative integer)

    2) an integer is considered prime if its only factors are 1 and itself. 1 can be considered a prime integer because its factors are 1 and only 1. Write a program that will input a non-negative and would call the function prime that returns 0 for true and 1 for false. (precondition: test if the entered value is non-negative)

    thanks in advance for your answers !! :DD
     
  2. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i,N,f=1;
    clrscr();
    printf("Enter any number");
    scanf("%d",&N);
    if(N>0)
    {
    for(i=1;i<=n;i++)
    {
    f=f*i;
    }
    }
    else
    break;
    printf("Factorial of given number is %d",f);
    getch();
    }
     
  3. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    #include<stdio.h>
    #include<conio.h>
    int prime(int);
    void main()
    {
    int n,x;
    clrscr();
    printf("Enter any number");
    scanf("%d",&n);
    x=prime(n);
    printf("%d",x);
    getch();
    }
    int prime(int n)
    {
    int i,c=0;
    if(n>0)
    {
    for(i=1;i<=n;i++)
    {
    if(n%i==0)
    c++;
    }
    }
    else
    {
    printf("Number can be found prime or not only for positive numbers");
    }
    if(c==2)
    return 0;
    else
    return 1;
    }
     
  4. Luisa Mia Peralta

    Luisa Mia Peralta New Member

    Joined:
    Sep 25, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    thanks a lot gpk kishore ! :D
     
  5. umesh kushwaha

    umesh kushwaha New Member

    Joined:
    Sep 29, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    :eek:perator::eek:perator:1)
    Code:
    #include<stdio.h>
    void main()
    {long int fact=1;
    int i=0,k=0,q=0,l,m,p,r,num,cnum,numh,n,numsh,temp;
    int a[300]={0},b[3],c[300]={0},d[3];
    d[1]=0;
    b[2]=0;
    clrscr();
    printf("printf the number within100\n");
    scanf("%d",&num);
    numh=num;
    if(num==0)
    printf("facctorial is one\n");
    else if(num<0)
    printf("factorial is undefined for negative number");
    else if(num<=10)
    {
    for(n=2;n<=num;n++)
    fact=fact*n;
    printf("factorial of %d is=%ld",num,fact);}
    for(n=2;n<=10 && num>=10;n++)
    {fact=fact*n;
    }
    while(fact>0 && num>10)
    { a[i++]=fact%10;
      fact=fact/10;
    }
    
    for(n=11;n<=num && num>10;n++)
    {
      numsh=n;
      while(numsh>0)
         {  b[k++]=numsh%10;
    	numsh=numsh/10;
          }
          c[0]=a[0]*b[0];
          c[1]=b[0]*a[1] +b[1]*a[0];
       for(l=2;l<=i;l++)
          {
    	c[l]= d[1]+ (b[0]*a[l]) + (b[1]*a[l-1])+(b[2]*a[l-2]);
    	cnum=c[l];
    	if(cnum<10)
    	d[1]=0;
    	if((cnum>9 ) && (l!=i))
    	 {
    	  for(q=0;q<2;q++)
    	  {
    	    d[q]=cnum%10;
    	    cnum=cnum/10;
    	   }
    	     c[l]=d[0];
    	     q=0;
    	  }
    	} if (c[l-1]>9)
    	{r=l-1;
    	cnum=c[l-1];
    	 while(cnum>0)
    	 {c[r++]=cnum%10;
    	 cnum=cnum/10;}
    	 l=r;}
    
        for(m=0;m<l;m++)
           {
    	 temp=c[m];
    	 a[m]=temp;
    	}i=l;
    	k=0;
    	d[1]=0;
      }
    
    
    if(num>10)
    {
    printf("factorial of %d is: ",numh) ;
    for(p=m-1;p>=0 && numh>10;p--)
    printf("%d",a[p]) ;}
    getch();
    }
     

Share This Page