0% found this document useful (0 votes)
59 views4 pages

Factorial and Number Functions in C

C lab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views4 pages

Factorial and Number Functions in C

C lab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem Solving and Computer

Programming L ab 6
NAME: SANJEEV NAIK BANOTHU
ROLL NO: 221220052
1. Find the factorial of a number using recursive and non-recursive
functions.
SORCE CODE
#include<stdio.h>
#include<math.h>
float factorial(int n);
float fact(int n);
int main(){
int n;
printf("ENTER THE NUMBER:");
scanf("%d",&n);
printf("WITHOUT RECURSION %0.0f\n",fact(n));
printf("WITH RECURSION %0.0f",factorial(n));

float factorial(int n){


if(n==1){
return 1;
}
float final=factorial(n-1)*n;
return final;
}
float fact(int n){
int i=1,fact=1;
for(i=1;i<=n;i++){
fact*=i;
}
return fact;
}

OUTPUT
2. Find the following using functions for a given natural number
a. Reverse of a number.
SORCE CODE
#include<stdio.h>
void palindrome(int x){
int pal=0,rem;
while(x!=0){
rem=x%10;
pal=pal*10+rem;
x=x/10;
}
printf(" %d",pal);

};
int main(){
int x;
printf("enter the number:");
scanf("%d",&x);
printf("THE REVERSE OF THE NUMBER %d IS ",x);
palindrome(x);
return 0;
}

OUTPUT

b. Sum of Digits of a number.


SOURCE CODE
#include <stdio.h>
int digits_sum(int a){
int k=0;
for(a;a>0;a/=10){
k+=a%10;
}
return k;
};
int main() {
int a,x;
printf("ENTER THE NUMBER:");
scanf("%d",&a);
x=a;
printf("SUM OF DIGITS IN THE NUMBER %d is
%d",x,digits_sum(a));
}

OUTPUT

c. Palindrome or not.
SOURCE CODE

#include<stdio.h>
void palindrome(int x){
int pal=0,rem;
int n=x;
while(x!=0){
rem=x%10;
pal=pal*10+rem;
x=x/10;
}
pal==n?printf(" YES IT IS PALINDROME"):printf(" IT IS NOT A
PALINDROME");

};
int main(){
int x;
printf("enter the number:");
scanf("%d",&x);
printf("%d",x);
palindrome(x);
return 0;
}

OUTPUT
d. Prime or not.
SOURCE CODE
#include<stdio.h>
void checkforprime(int x);
int main(){
int x;
printf("ENTER THE NUMBER:");
scanf("%d",&x);
checkforprime(x);
return 0;
}
void checkforprime(int x){
int a=0;
for(int i=2;i<=9;i++){
if(x%i==0 && x!=i) a=1;
}
a==1? printf("%d is not a prime number ",x) : printf("%d is a
prime number ",x);

OUTPUT

You might also like