C Language Practical Programs For Beginners

Important practical programs for C Language

C language practical programs for beginners.

1. C Program To Find a Factorial of a number

 #include<stdio.h>

int main()    

{    

int i,fact=1,number;    

printf(“Enter a number: “);    

scanf(“%d”,&number);    

for(i=1;i<=number;i++)

{    

fact=fact*i;    

}

printf(“Factorial of %d is: %d”,number,fact);    

return 0;  

Output:

Enter a number: 5

Factorial of 5 is: 120

2. C Program To Find a Factorial of a number Using RecursionMethod

#include<stdio.h>

long factorial(int n)  

{  

if (n == 0)  

return 1;  

else

return(n * factorial(n-1));  

}  

void main()  

{  

int number;  

long fact;  

printf(“Enter a number: “);  

scanf(“%d”, &number);   

fact = factorial(number);  

 printf(“Factorial of %d is %ldn”, number, fact);  

return 0;  

}  

Output:

Enter a number: 6

Factorial of 5 is: 720

3. C Program To Find a Even and Odd of a number

#include <stdio.h>

int main()

{

int number;

printf(“Enter an integer: “);

scanf(“%d”, &number);

 // will return true if the number is perfectly divisible by 2

if(number % 2 == 0)

printf(“%d is even.”, number);

else

printf(“%d is odd.”, number);

return 0;

}

Output: 

Enter an integer: 7

7 is odd.

4. C Program To Find a Even and Odd of a number Using Conditional Operator

#include <stdio.h>

int main()

{

int number;

printf(“Enter an integer: “);

scanf(“%d”, &number);

(number % 2 == 0) ? printf(“%d is even.”, number) : printf(“%d is odd.”, number);

return 0;

}

5. C Program To Print Fibonacci Series in C without Recursion

#include<stdio.h>  

int main()    

{    

int n1=0,n2=1,n3,i,number;    

printf(“Enter the number of elements:”);    

scanf(“%d”,&number);    

printf(“n%d %d”,n1,n2); //printing 0 and 1  

for(i=2;i<number;++i)  //loop will start from 2 because 0 and 1 are already printed

 {    

                        n3=n1+n2;    

                        printf(” %d”,n3);    

                        n1=n2;    

                        n2=n3;    

 }  

return 0;  

 }    

Output:

Enter the number of elements:15

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

6. C Program To Print Fibonacci Series in C with Using Recursion

#include<stdio.h>  

void printFibonacci(int n)

{    

staticint 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

 

7. C Program To Check Prime Number of a given Number.

#include <stdio.h>

int main()

{

int n, i, flag = 0;

printf(“Enter a positive integer: “);

scanf(“%d”, &n);

for(i = 2; i <= n/2; ++i)

{

// condition for nonprime number

if(n%i == 0)

{

flag = 1;

break;

}

}

if (n == 1)

{

printf(“1 is neither a prime nor a composite number.”);

}

else

{

if (flag == 0)

printf(“%d is a prime number.”, n);

else

printf(“%d is not a prime number.”, n);

}

return 0;

}

Output

Enter a positive integer: 29

29 is a prime number.

8. C Program To Check Whether a Number is Palindrome or Not

#include <stdio.h>

int main()

{

int n, reversedInteger = 0, remainder, originalInteger;

printf(“Enter an integer: “);

scanf(“%d”, &n);

originalInteger = n;

// reversed integer is stored in variable

while( n!=0 )

{

remainder = n%10;

reversedInteger = reversedInteger*10 + remainder;

n /= 10;

}

// palindrome if orignalInteger and reversedInteger are equal

if (originalInteger == reversedInteger)

printf(“%d is a palindrome.”, originalInteger);

else

printf(“%d is not a palindrome.”, originalInteger);

return 0;

}

Output

Enter an integer: 1001

1001 is a palindrome.

Leave a Comment

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