Basic C Programs for Practice

With the help of the C programming language, we can solve various types of mathematical problems.In this tutorial, we are going to write some programs in C to solve mathematical problems like Fibonacci Series, Prime Numbers, Palindrome and Factorial of a number, Reverse of a number, Matrix multiplication and decimal to binary.

1. Fibonacci Series in C

In the Fibonacci series, the next number is the sum of the previous two numbers. And the first two numbers of the fibonacci series are 0 and 1.

Example:- 0,1,1,2,3,5,8,13 etc.

In two ways, you can implement fibonacci series:-

  • Writing the code without recursion
  • Writing the code with recursion

Algorithm:-

START
Step 1 → Take integer variable n1, n2, n3
Step 2 → Set n1 = 0, n2 = 0
Step 3 → DISPLAY n1, n2
Step 4 → n3 = n1 + n2
Step 5 → DISPLAY n3
Step 6 → Set n1 = n2, n2 = n3
Step 7 → REPEAT from 4 – 6, for n times
STOP

Fibonacci Series Without recursion in C:-

You can implement fibonacci series without recursion in C. Below is the code to perform fibonacci series without recursion:-

#include<stdio.h>    
int main()    
{    
int first=0,second=1,result,a,num=7;
printf("TechVidvan Tutorial: Fibonacci Series without recursion!\n");
printf("%d %d",first,second);   
for(a=2;a<num;++a)    
{    
result=first+second;    
printf(" %d",result);    
first=second;    
second=result;    
}  
return 0;  
}

Output:-

TechVidvan Tutorial: Fibonacci Series without recursion!
0 1 1 2 3 5 8

Fibonacci Series with recursion in C:-

With the help of recursion, you can implement the fibonacci series in C. Below is the code to perform fibonacci series with recursion:-

#include<stdio.h>    
void fib(int num){    
static int first=0,second=1,result;    
if(num>0){    
result=first+second;    
first=second;  
second=result;  
printf("%d ",result);    
fib(num-1);    
}    
}    
int main(){    
int num=7;   
printf("TechVidvan Tutorial: Fibonacci Series with Recursion in C!\n\n");
printf("Fibonacci Series: ");    
printf("%d %d ",0,1);    
fib(num-2);    
return 0;  
}

Output:-

TechVidvan Tutorial: Fibonacci Series with Recursion in C!

Fibonacci Series: 0 1 1 2 3 5 8

2. Find Prime Numbers in C Program:-

When a number is just divisible by 1 or itself and is greater than 1 is called a prime number.

NOTE:- Prime numbers are divisible by 1 or itself.

Example:- 2,3,5,7,11,13 etc are prime numbers.

C Program to Check if a number is prime or not

#include<stdio.h>  
int main(){    
int num,a,res=0,count=0;   
printf("TechVidvan Tutorial: Check if a number is prime or not!\n\n");
printf("Check this number: \n");    
scanf("%d",&num);    
res=num/2;    
for(a=2;a<=res;a++)    
{    
if(num%a==0)    
{    
printf("The entered number '%d' is not prime!",num);    
count=1;    
break;	 
}    
}    
if(count==0)    
printf("The entered number '%d' is prime!",num);	 
return 0;  
}	

Output:-

TechVidvan Tutorial: Check if a number is prime or not!

Check this number:
23
The entered number ’23’ is prime!

3. Find if the number is Palindrome using C Programming

Suppose, a number is reversed. If the reversed number is the same as the original number then this number is called a palindrome number.

Algorithm:-

  • First, take an input number from the user.
  • Then, hold the number in a temporary variable.
  • After that, reverse the given number.
  • Then, do a comparison between the reversed number and the temporary number.
  • If both numbers are the same then display that it is a palindrome number.
  • If not then display that it is not a palindrome number.

Example:- Palindrome Number

#include<stdio.h>  
int main()    
{    
int num,reversed,sum=0,garbage;
printf("TechVidvan Tutorial: Palindrome number!\n");
printf("Please enter your number: ");    
scanf("%d",&num);    
garbage=num;    
while(num>0)    
{    
reversed=num%10;    
sum=(sum*10)+reversed;    
num=num/10;    
}    
if(garbage==sum)    
printf("The entered number '%d' is palindrome!",garbage);    
else    
printf("The entered number '%d' is not palindrome!",garbage);   
return 0;  
}

Output:-

TechVidvan Tutorial: Palindrome number!
Please enter your number: 131
The entered number ‘131’ is palindrome!

4. Find Factorial of a number through C Program

Factorial of a number means the product of all positive descending integers. Factorial is denoted by ‘!’.
Example:-

5! = 5*4*3*2*1 = 120
4! = 4*3*2*1 = 24

There are 2 ways to write the program for finding the factorial of a number.

  • Finding factorial using loop
  • Finding factorial using recursion

Find factorial of a number using loop:-

In C, you make use of loops to find the factorial of a number.

#include<stdio.h>  
int main()    
{    
 int a,factorial=1,num;
 printf("TechVidvan Tutorial: Find the factorial of a number using a loop!\n");
 printf("Please enter the number: ");    
  scanf("%d",&num);    
  for(a=1;a<=num;a++){    
  	factorial=factorial*a;    
  }    
  printf("The factorial value of %d is: %d",num,factorial);    
return 0;  
}

Output:-

TechVidvan Tutorial: Find the factorial of a number using a loop!
Please enter the number: 5
The factorial value of 5 is: 120

Find factorial of a number using recursion:-

In C, you also calculate the factorial of a number using recursion.

#include<stdio.h>  
int fact(int num)  
{  
  if (num == 0)  
  return 1;  
  else  
  return(num * fact(num-1));  
}  
   
int main()  
{  
  int num;  
  int factorial;
  printf("TechVidvan Tutorial: Finding the factorial of a number using recursion!\n");
  printf("Please enter a number: ");  
  scanf("%d", &num);   
   
  factorial = fact(num);
  printf("The factorial value of %d is %d\n", num, factorial);  
  return 0;  
}

Output:-

TechVidvan Tutorial: Finding the factorial of a number using recursion!
Please enter a number: 5
The factorial value of 5 is 120

5. Reverse a number using C:-

In C, there are many ways to reverse a number. You can reverse a number using loops and recursion.

Algorithm:-

You have to use modulus(%) to reverse a number.

  • First, you have to initialize a reverse number to 0.
  • Then, multiply the reverse with value 10.
  • After that, divide the given number by 10 and find the modulus.
  • Then, add the modulus and the reverse number.
  • Display the result of addition.
  • Divide a given number by 10.
  • Then, repeat the steps from second to sixth until you get the output.

Reverse a number using while loop:-

Following is the code to reverse a number using a while loop.

#include<stdio.h>
#include<conio.h>
int main()
{
int num, reverse = 0;
printf("TechVidvan Tutorial: Reverse a number using the while loop!\n");
printf("Enter a number to reverse: ");
scanf("%d", &num);
while (num != 0)
{
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}
printf("The reverse value of the entered number is: %d", reverse);
return 0;
}

Output:-

TechVidvan Tutorial: Reverse a number using the while loop!
Enter a number to reverse: 22114456
The reverse value of the entered number is: 65441122

Reverse a number using recursion:-

Following is the program to reverse a number using recursion:-

#include<stdio.h>
#include<conio.h>
int reverse_number(int);
int main()
{
int num, reverse = 0;
printf("TechVidvan Tutorial: Reverse a number using recursion!\n\n");
printf("Please enter a number to reverse:");
scanf("%d", &num);
reverse = reverse_number(num);
printf("The reverse value of the number is: %d", reverse);
return 0;
}
reverse_number(int num)
{
static reverse = 0;
if (num == 0)
return 0;
reverse = reverse * 10;
reverse = reverse + num % 10;
reverse_number(num/10);
return reverse;
}

Output:-

TechVidvan Tutorial: Reverse a number using recursion!

Please enter a number to reverse:236498
The reverse value of the number is: 894632

Reverse a number using for loop:-

In this, we first initialised and declared variables. Then, we put the condition in the program. If the condition is true then the code within the for block will get executed. And if it is false then it will exit out of the for block. After that the increment statement will get executed. Then, the condition will be checked. If it is true then the for block will execute again.

#include<stdio.h>
#include<conio.h>
int main()
{
int num, remain, reverse=0, a;
printf("TechVidvan Tutorial: Reverse a number using the for loop!\n\n");
printf("Enter a number to reverse: ");
scanf("%d", &num);
for(a = num; a >0; )
{
remain = a % 10;
reverse = remain + reverse * 10;
a = a/ 10;
}
printf("The reverse value of the entered number is: %d", reverse);
return 0;
}

Output:-

TechVidvan Tutorial: Reverse a number using the for loop!

Enter a number to reverse: 3664921
The reverse value of the entered number is: 1294663

Reverse a number using do-while loop:-

It is much like the while loop. But it gets executed at least one time in the do-while loop.
The condition is set to at the end of the loop. And the statements inside the do-while loop gets executed before checking if the condition is true or false.

#include<stdio.h>
#include<conio.h>
int main()`
{
int num, reverse = 0;
printf("TechVidvan Tutorial: Reverse a number using a do-while loop!\n\n");
printf("Enter a number to reverse: ");
scanf("%d", &num);
do
{
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}while(num != 0);
printf("The reverse value of the number is: %d", reverse);
return 0;
}

Output:-

TechVidvan Tutorial: Reverse a number using a do-while loop!

Enter a number to reverse: 23645869
The reverse value of the number is: 96854632

6. Matrix Multiplication using C Language

In C, we can add, subtract, multiply one, two or three dimensional matrices. To do these operations, you have to take input from the user for row number, column number and matrix elements. After that, you can perform matrix multiplication.

Following is an example of multiplying of 2*2 and 3*3 matrices:-

#include<stdio.h>    
#include<stdlib.h>  
int main(){  
int m1[10][10],m2[10][10],multiple[10][10],row,column,a,b,c;    
printf("TechVidvan Tutorial: Multiplying matrices in C!\n\n");
printf("Please enter the number of rows: ");    
scanf("%d",&row);    
printf("Please enter the number of columns: ");    
scanf("%d",&column);    
printf("Please enter the elements of first matrix:\n");    
for(a=0;a<row;a++)    
{    
for(b=0;b<column;b++)    
{    
scanf("%d",&m1[a][b]);    
}    
}    
printf("Please enter the elements of second matrix:\n");    
for(a=0;a<row;a++)    
{    
for(b=0;b<column;b++)    
{    
scanf("%d",&m2[a][b]);    
}    
}    
    
printf("Multiplying the two matrices:\n");    
for(a=0;a<row;a++)    
{    
for(b=0;b<column;b++)    
{    
multiple[a][b]=0;    
for(c=0;c<column;c++)    
{    
multiple[a][b]+=m1[a][b]*m2[c][b];    
}    
}    
}    
 
for(a=0;a<row;a++)    
{    
for(b=0;b<column;b++)    
{    
printf("%d\t",multiple[a][b]);    
}    
printf("\n");    
}    
return 0;  
}

Output:-

TechVidvan Tutorial: Multiplying matrices in C!

Please enter the number of rows: 2
Please enter the number of columns: 2
Please enter the elements of first matrix:
144
1
2
35
Please enter the elements of second matrix:
12
35
4
5
Multiplying the two matrices:
2304 40
32 1400

7. Convert decimal to binary using C Programming

In C, you can convert a decimal value to a binary value or vice-versa.

Decimal Number:-

It is specially known as a base 10 number. It ranges from 0 to 9.

Example:- 23, 6,78,90 etc.

Decimals are also defined as a combination of digits.

Binary Number:-

It is either 1 or 0. It is a base 2 number. Combination of 0 and 1’s such as 001,110,1110 etc.
Following is the table of binary values for the decimal numbers.

Decimal Binary
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010

Algorithm for converting decimal to binary:-

  • First, you have to divide the number by 2 using modulus(%) and then store the remainder in an array.
  • Then, you have to again divide the number by 2 using division operator(/).
  • Then, Repeat the above steps until the number is greater than 0.

Example:- Converting decimal to binary

#include<stdio.h>    
#include<stdlib.h>  
int main()
{  
int arr[10],num,a;    
printf("TechVidvan Tutorial: Converting decimal to binary in C!\n\n");
printf("Please enter the number which you want to convert: ");    
scanf("%d",&num);  
printf("Binary value of the number %d is: ",num);  
for(a=0;num>0;a++)    
{    
arr[a]=num%2;    
num=num/2;    
}    
 
for(a=a-1;a>=0;a--)    
{    
printf("%d",arr[a]);    
}    
return 0;  
}

Output:-

TechVidvan Tutorial: Converting decimal to binary in C!

Please enter the number which you want to convert: 12
Binary value of the number 12 is: 1100

Summary

In this tutorial, we perform some mathematical operations using loops, recursion etc. We wrote a program to convert a decimal number to a binary number and performed multiplication on matrices. We wrote a program to find the factorial of a number in C. Then we also wrote programs to reverse a number, find the palindrome of a number, check if the number is prime or not. We discussed how we can implement the fibonacci series in C.