# PROGRAMMiNG EXERCiSES

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

1. ### Luisa Mia PeraltaNew Member

Joined:
Sep 25, 2011
Messages:
7
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 !! D

2. ### gpk kishoreNew Member

Joined:
Jun 30, 2011
Messages:
82
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 kishoreNew Member

Joined:
Jun 30, 2011
Messages:
82
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 PeraltaNew Member

Joined:
Sep 25, 2011
Messages:
7
0
Trophy Points:
0
thanks a lot gpk kishore ! 5. ### umesh kushwahaNew Member

Joined:
Sep 29, 2011
Messages:
2
0
Trophy Points:
0 perator: 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={0},b,c={0},d;
d=0;
b=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=a*b;
c=b*a +b*a;
for(l=2;l<=i;l++)
{
c[l]= d+ (b*a[l]) + (b*a[l-1])+(b*a[l-2]);
cnum=c[l];
if(cnum<10)
d=0;
if((cnum>9 ) && (l!=i))
{
for(q=0;q<2;q++)
{
d[q]=cnum%10;
cnum=cnum/10;
}
c[l]=d;
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=0;
}

if(num>10)
{
printf("factorial of %d is: ",numh) ;
for(p=m-1;p>=0 && numh>10;p--)
printf("%d",a[p]) ;}
getch();
}```