Saturday, 27 June 2015

List of C Programs

1. Write a program to find factorial of the given number. 
Recursion: A function is called 'recursive ' if a statement within the body of a function calls the same function. It is also called 'circular definition '.Thus recursion is a process of defining something in terms of itself.

Program: To calculate the factorial value using recursion.

#include<stdio.h>
int fact(int n);
int main()
{
int x, i;
printf("En ter a value for x: \n");
scanf("%d" ,&x);
i = fact(x);
printf("\n Factorial of %d is %d", x, i);
return 0;
}
int fact(int n)
{
/* n=0 indicates a terminating condition */
if (n)
return (1);
}
else
{
/* function calling itself */
return (n * fact(n - 1));
/*n*fact(n -1) is a recursive expression */
}
}

OUTPUT:

Enter a value for x: 4
Factorial of 4 is 24

2.Write a program to check whether the given number is even or odd.

Program:

#include<stdio.h>
int main()
{
int a;
printf("En ter a: \n");
scanf("%d" ,&a);
/* logic */
if (a % 2 == 0){

printf("Th e given number is EVEN\n");
}
else{
printf("Th e given number is ODD\n");
}
return 0;
}

Output:
Enter a: 2
The given number is EVEN

3. Write a program to swap two numbers using a temporary variable.

#include<stdio.h>
int main()
{
int a, b, temp;
printf("En ter the value of a and b: \n");
scanf("%d %d",&a,&b);
printf("Be fore swapping a=%d, b=%d \n", a, b);
/*Swapping logic */
temp = a;
a = b;
b = temp;
printf("Af ter swapping a=%d, b=%d", a, b);
return 0;
}

Output:

Enter the values of a and b: 2 3
Before swapping a=2, b=3
After swapping a=3, b=2

4. Write a program to swap two numbers without using a temporary variable. 

#include<stdio.h>
int main()
{
int a, b;
printf("En ter values of a and b: \n");
scanf("%d %d",&a,&b);
printf("Be fore swapping a=%d, b=%d\n", a,b);
/*Swapping logic */
a = a + b;
b = a - b;
a = a - b;
printf("Af ter swapping a=%d b=%d\n", a, b);
return 0;

}



Output:
Enter values of a and b: 2 3
Before swapping a=2, b=3
The values after swapping are a=3 b=2

5. Write a program to swap two numbers using bitwise operators. 
Program: 

#include<stdio.h>
int main()
{
int i = 65;
int k = 120;
printf("\n value of i=%d k=%d before swapping", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\n value of i=%d k=%d after swapping", i, k);
return 0;

}

6. Write a program to find the greatest of three numbers.
 Program: 

#include<stdio.h>
int main()
{
int a, b, c;
printf("En ter a,b,c: \n");
scanf("%d %d %d",&a,&b,&c);
if (a>b&&a>c){
printf("a is Greater ");
}
else if (b>c)
{
printf("b is Greater ");
}
else {
printf("c is Greater ");
}
return 0;
}

Output:

Enter a,b,c: 3 5 8
c is Greater 

7. Write a program to find the greatest among ten numbers. 
Program: 

#include<stdio.h>
int main()
{
int a[10];
int i;
int greatest;
printf("En ter ten values:");
//Store 10 numbers in an array
for (i = 0; i<10; i++){
scanf("%d" ,&a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i<10; i++){
if (a[i]>greatest){
greatest = a[i];
}
}
printf("\n Greatest of ten numbers is %d", greatest);
return 0;
}

Output:
Enter ten values: 2 53 65 3 88 8 14 5 77 64 
Greatest of ten numbers is 88

8. Write a program to check whether the given number is a prime. 
Program:

#includestdio.h>
Void main()
{
int n, i, c = 0;
printf("En ter any number n: \n");
scanf("%d" ,&n);
/*logic*/
for (i = 1; i<=n/2;i++)
if (n % i == 0){
c++;
}
}
if (c == 2){
printf(“n is a Prime number");
}
else{
printf("n is not a Prime number");
}
getch();
}

Output: 
Enter any number n: 7
 n is Prime 

9. Write a program to check whether the given number is a palindromi c number.
 Program:

#include<stdio.h>
int main()
{
int n, n1, rev = 0, rem;
printf("En ter any number: \n");
scanf("%d" ,&n);
n1 = n;
/* logic */
while (n>0){
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if (n1 == rev){
printf("Gi ven number is a palindromi c number");
}
else{
printf("Gi ven number is not a palindromi c number");
}
return 0;
}

Output:

Enter any number: 121
Given number is a palindrome

10.Write a program to check whether the given string is a palindrome . 

Program:

#include<stdio.h>
#include<conio.h>
int main()
{
char string1[20 ];
int i, length;
int flag = 0;
printf("En ter a string: \n");
scanf("%s" , string1);
length = strlen(str ing1);
for(i=0;i<length ;i++){
if(string1 [i] != string1[le ngth-i-1]) 
{
flag = 1;
break;
}
}
if (flag){
printf("%s is not a palindrome \n", string1);
}
else{
printf("%s is a palindrome \n", string1);
}
return 0;
}

Output:

Enter a string: radar
"radar"is a palindrome

Remaining will be continued later.....

No comments:

Post a Comment