Thursday, 4 November 2021

C Program to find Palindrome using recursion

C Program to find Palindrome using recursion

 

Program:-

#include <stdio.h>

#include<conio.h>

#include <math.h>

int reverse(int num);

int isPalindrome(int num); 

int main()

{

int num;     

printf("Enter any number: ");

scanf("%d", &num);

if(isPalindrome(num) == 1)

{

printf("%d is palindrome number.\n", num);

}

Else

{

printf("%d is NOT palindrome number.\n", num);

}

return 0;

}

int isPalindrome(int num)

{

if(num == reverse(num))

{

return 1;

}     

 return 0;

}

int reverse(int num)

{

int digit;     

if(num==0)

return 0;

digit = (int)log10(num)     

return ((num%10 * pow(10, digit)) + reverse(num/10));

}



Output:

Enter any number: 1221
1221 is palindrome
number.

 

C Program to find Factorial using recursion

C Program to find Factorial using recursion


Program 

#include <stdio.h> 

#include<conio.h>

long int factorial(int n)

{  

if(n==1)    return 1;

return n*factorial(n-1);

}

int main()

{

int num;

long int fact=0;     

printf("Enter an integer number: ");

scanf("%d",&num);     

fact=factorial(num);

printf("Factorial of %d is = %ld",num,fact);

printf("\n");  

return 0;

}

 


Output:-

Enter an integer number: 5
Factorial of 5 is = 120

C Program for Swapping using call by value method

C Program for Swapping using call by value method


Program:-        
#include <stdio.h>
#include<conio.h>
void swap(int, int);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d %d", &x, &y);  
printf("Before Swapping x = %d y = %d\n", x, y);
swap(x, y);    
printf("After Swapping  x = %d  y = %d\n", x, y);
return 0;
}
void swap(int a,  int b)
{
int temp;   
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d  %d\n", a, b);
}
 

Output:-
Enter the value of x and y : 4  6
Before Swapping x = 4 y =6
After Swapping  x = 6 y = 4

C Program to find sum of N numbers

 C Program to find sum of N numbers

Program:-

#include<stdio.h>

#include<conio.h>

int sum(int);
int main()
{
int num,s;
printf("enter value of n:");
scanf("%d",&num);
s = sum(num);
printf("sum of %d numbers = %d\n", num, s);
}
int sum(int num)
{
int i,sum=0;
for(i=0;i<=num;i++)
{
sum = sum + i;
 }
return sum;
}
 

Output:

enter value of n: 5
sum of %d numbers =15
google-site-verification: google18ae80885fc3d876.html