Programming In C Practical 22

Question 1
Write program to reverse the number 1234 using function.
Reverse.cpp
C
				#include<stdio.h>
#include<conio.h>

int revfun(int num)
{
	int rem,rev=0;
	while(num>0)
	{
		rem=num%10;
		rev=rev*10+rem;
		num=num/10;
	}
	return(rev);
}

void main()
{
	clrscr();
	int n=1234,rev;
	rev=revfun(n);
	printf("\nReverse of given number is: %d",rev);
	
	getch();
}
			
Output
Question 2
Find factorial of a given number using function
Fact.cpp
C
				#include<stdio.h>
#include<conio.h>

int factfun(int num)
{
	int fact=1;
	while(num>0)
	{
		fact=fact*num;
		num--;
	}
	return(fact);
}

void main()
{
	clrscr();
	int n,ans;
	printf("\nEnter a number: ");
	scanf("%d",&n);
	ans=factfun(n);
	printf("\nFactorial of given number is: %d",ans);
	
	getch();
}
			
Output
Question 3
Create a function to find GCD of given number. Call this function in a program.
Gcd.cpp
C
				#include<stdio.h>
#include<conio.h>
#include<math.h>

int gcd(int x,int y)
{
	int i,small,result;
	small=(x<y)?x:y;
	for(i=1;i<=small;i++)
	{
		if(x%i==0&&y%i==0)
			result=i;
	}
	return(result);
}

void main()
{
	clrscr();
	int num1,num2,ans;
	printf("\nEnter a number1: ");
	scanf("%d",&num1);
	printf("\nEnter a number2: ");
	scanf("%d",&num2);
	ans=gcd(num1,num2);
	printf("\nGCD of given numbers is: %d",ans);

	getch();
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *