0% found this document useful (0 votes)
12 views7 pages

Function

The document contains multiple C programs demonstrating various concepts such as checking for palindromes, calculating factorials using recursion, generating Fibonacci series, checking for Armstrong numbers, and examples of call by value and call by reference for swapping variables. Each program includes the code, expected output, and explanations of the functionality. The examples illustrate fundamental programming techniques in C.

Uploaded by

pragashree19
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)
12 views7 pages

Function

The document contains multiple C programs demonstrating various concepts such as checking for palindromes, calculating factorials using recursion, generating Fibonacci series, checking for Armstrong numbers, and examples of call by value and call by reference for swapping variables. Each program includes the code, expected output, and explanations of the functionality. The examples illustrate fundamental programming techniques in C.

Uploaded by

pragashree19
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
You are on page 1/ 7

FUNCTION - C PROGRAM

C Program to check if a number is a palindrome by reversing the number


#include <stdio.h>
int reverseNum(int N)​ ​ // Function to store the reversed number​
{
int rev = 0, M;
while (N > 0)
{
M = N % 10;
rev = rev * 10 + M;
N /= 10;
}
return rev;
}
int main()
{
int N;
printf(“ Enter the number to check for palindrome”);
scanf(“%d”, & N);
reverseNum(N);
if (N== rev)
{
printf("The number is Palindrome\n");
}
else {
printf("The number is Not an Palindrome\n");
}
return 0;
}
OUTPUT
Enter the number to check for palindrome
121
The number is palindrome

*** Palindrome
Input 121
Reverse 121
The number 121 remains the same when its digits are reversed. *******

Factorial of a Number Using Recursion


#include <stdio.h>
int factorial( int n)
{
if (n == 1) ​ ​ ​ ​ //1! =1
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
int main()
{
int result, num;
Printf(“Enter the number”);
Scanf(“%d”,&num);
result=factorial(num);
printf("Factorial of %d is %d", num, result);
return 0;
}
OUTPUT
Enter the Number
5
Factorial of 5 is 120

Fibonacci Series using recursion in C


#include<stdio.h>
void printFibonacci(int n)
{
int n1=0,n2=1,n3;
if(n>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2); //n-2 because 2 numbers are already printed
return 0;
}
OUTPUT
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

C Program to Check for Armstrong Number using function

#include<stdio.h>
int amstrong(n)​ ​ ​ ​ ​ // Function declare
{
while(n>0)
{
m=n%10;
sum=sum+m*m*m;
n=n/10;
}
printf("Sum is=%d",sum);
}
int main()
{
printf("Enter a number:");
scanf("%d",&n);
amstrong(n);​ ​ ​ ​ // Function call
if(n==sum)
printf(“ the number is amstrong”);
else
printf(“the number is not amstrong”);
}

OUTPUT
Enter a number
153
the number is amstrong

Enter a number
123
the number is not amstrong

Call by Value Example: Swapping the values of the two


variables
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping %d %d",a,b);
swap(a,b);
printf("After swapping %d%d",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
}

Output
Before swapping 10 20
After swapping 10 20

Call by reference Example: Swapping the values of the two


variables
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping %d%d\n",a,b);
swap(&a,&b);
printf("After swapping %d%d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
}
Output
Before swapping 10 20
After swapping 20 10

You might also like