# 7 Basic C Programs that will help you to rise from Noob to Pro

If the roots of a tree are not strong, it would not be able to support the tree. Similarly, if you are not aware of some of the basic C Programs and you wish to be master C Programming, then you’re simply building castles in the air.

There is no use of learning a programming language unless you develop a proper logic to solve problems. Simply learning the syntax of language would not serve the purpose.

Here, we will discuss some of the most common or basic C Programs, that will help you to code better.

• Fibonacci series in C
• Prime numbers in C
• Palindrome in C
• Factorial in C
• Number reversal in C
• Matrix multiplication in C
• Decimal to binary conversion in C

We will explain each code step by step for a clear understanding of how it works.

## Basic C Programs

A good programmer has great command over the concepts of a language as well as the skills required to develop algorithms to solve complex problems that come through rigorous practice.

We are going to discuss mentioned Basic C Programs to reach out to them and explore its structure & syntax through an example. We will cover each and every topic related to them and will let you know about the small things related to them.

After reading them, I am sure that your concepts for these basic C programs would be crystal clear & it would not make any trouble in the future to you. Join DataFlair on Telegram!!

### 1. Fibonacci Series in C

Let us begin our discussion by trying to understand what is the Fibonacci series?

In mathematics, a Fibonacci series refers to the sequence in which the next element is equal to the sum of its two preceding elements.

Therefore, the first two numbers in the sequence need to be specified.

Generally, the first term is 0 and the second is 1.

The Fibonacci series goes on like this up till n numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584…. N

Before discussing the example, revise the Basic Structure of C Program

#### Example of Fibonacci Series in C

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int number_of_terms, iteration;
int term1 = 0, term2 = 1, next_term;

printf("Enter the number of terms till you want to display the numbers in the Fibonacci series: ");
scanf("%d", &number_of_terms);

printf("The Fibonacci series up till %d terms is: ",number_of_terms);

for (iteration = 1; iteration <= number_of_terms; iteration++)
{
printf("%d ", term1);
next_term = term1 + term2; // Next term is the sum of previous terms
term1 = term2;
term2 = next_term;
}
return 0;
}
```

Code- Output- Elucidation

Declaration: We declare the variable ‘number_of_terms’ to find the terms till which the series is to be displayed, the variable ‘iteration’ to traverse the for loop. Then, we initialize the first and second term as 0 and 1 respectively are their values are fixed.

Input: We take the input of the number of terms to be entered by the user till which he wishes to display the Fibonacci series.

Logic: We initialize the variable ‘iteration’ to 1. We make the loop run till the number of terms in the series and print the first term and second term.

• Now, our task is to find the sum of both the terms, that is, 0 and 1 to obtain the next term.
• When the loop runs for the second time, we store the result obtained, that is, 2, to the second term and the original second term to the original first term.
• The same process is repeated until the variable ‘iteration’ becomes equal to the number of terms.

Do you know how to declare Variables in C? Here is a code in C to display the Fibonacci series up till a given number:

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int term1 = 0, term2 = 1, next_term = 0, limit;

printf("Enter a number till which you want to display the Fibonacci series: ");
scanf("%d", &limit);

printf("The Fibonacci series till %d is: %d, %d, ",limit, term1, term2); // term1 = 0 and term2 = 1

next_term = term1 + term2;
while(next_term <= limit)
{
printf("%d ",next_term);
term1 = term2;
term2 = next_term;
next_term = term1 + term2;
}
return 0;
}```

Code- Output- Elucidation

Declaration: We declare a variable limit to find the number till which the Fibonacci series is to be displayed. We initialize the variables ‘term1’ and ‘term2’ to 0 and 1 respectively as their values are constant. We initialize next_term to 0 for the traversal of the while loop.

Input: We take the input of the limit by the user till which he wishes to display the Fibonacci series.

Logic: We store the sum of the first two terms, that is, 0 and 1 in the variable ‘next_term’.

• The while loop runs till the next term would be equal to the limit entered by the user.
• At the first iteration, the value of the second term is stored in the first term. Then, the value of the next term is stored in the second term. Finally, we add the newly obtained terms to the next_term.
• This process keeps on repeating itself till all the numbers are displayed less than the limit entered by the user.

### 2. Prime Numbers in C

Before we begin our discussion on prime numbers, it is important to understand conditional statements and operators in C.

A prime number is nothing but a natural number (1, 2, 3, 4…..) with its only factors as 1 and the number itself with the exclusion of 1.

#### Example of Prime Numbers in C

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int number, iteration;
int flag = 0; // Initialize

printf("Enter a positive number to check if it is prime or not ");
scanf("%d", &number);

for(iteration = 2; iteration <= number/2; iteration++)
{
if(number%iteration == 0) // Condition for a non-prime number
{
flag = 1; // To indicate that the number is non-prime
break;
}
}

if (number == 1)
{
printf("1 is not a prime number.\n");
}
else
{
if (flag == 0)
printf("%d is a prime number.\n", number);
else
printf("%d is not a prime number.\n", number);
}

return 0;
}

```

Code- Output- Elucidation

Declaration: We declare the variable ‘number’ to check if it is prime or not and ‘iteration’ to traverse the for loop. We initialize the variable ‘flag’ to 0 used for indication of the prime or non-prime number

Input: We take the input of the number entered by the user to check if it is prime or not.

Logic: Our main aim is to check if there is any factor of the number other than 1 or the number itself in the range (1, number). If there exists a number such that it divides the number, then the number is non-prime.

• After checking the condition of a non-prime number, we can use the break statement to exit out of that condition. The else condition would print the number that is not non-prime. Therefore, we get a prime number.

Explanation Through Dry Run:

Consider the value of the variable number = 19.

Since iteration starts from 2 till the integral division of 19/2 = 9, therefore:

Iteration would take the values: 2, 3, 4, 5, 6, 7, 8 and 9.

Checking the condition if number % iteration is divisible with each other, that is,

19 % 2 ≠ 0
19 % 3 ≠ 0
19 % 4 ≠ 0
19 % 5 ≠ 0
19 % 6 ≠ 0
19 % 7 ≠ 0
19 % 8 ≠ 0
19 % 9 ≠ 0

Since these conditions are not satisfied, we conclude the number 19 is not non-prime which means that it is a prime number.

In order to come out of the if-condition, we use the break statement. Therefore, the flow of control is transferred to the else condition that indicates that the number 19 is prime.

Here is a program in C that helps you find a prime number in a given range:

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int lower_limit, upper_limit, iteration;
int flag; // A variable flag to indicate if the number is prime or not
printf("Enter the range:\n");
printf("Lower limit: ");
scanf("%d",&lower_limit);
printf("Upper limit: ");
scanf("%d",&upper_limit);

printf("The prime numbers in the range (%d,%d) are:\n", lower_limit, upper_limit);

while (lower_limit < upper_limit)
{
flag = 0;
for(iteration = 2; iteration <= lower_limit/2; iteration++)
{
if(lower_limit % iteration == 0)
{
flag = 1; // Condition for non-prime number
break;
}
}
if (flag == 0)
{
printf("%d ", lower_limit);
}
lower_limit++;
}
return 0;
}
```

Code- Output- Elucidation

Declaration: We declare the variables ‘lower_limit’ and ‘upper_limit ’to specify the range between which we are supposed to find all the prime numbers.

Input: We take the input of the lower limit and upper limit from the user to display the prime numbers within that range.

Logic: Our main aim is to check if there is any factor of the numbers other than 1 or the number itself in the range (lower_limit, upper_limit). If there exists a number such that it divides the number, then the number is non-prime.

• The variable ‘flag’ indicates if the number is prime or not.
• After checking the condition of a non-prime number, we can use the break statement to exit out of that condition. The else condition would print the number that is not non-prime. Therefore, we get a prime number.
• After incrementing the value of the lower limit, the loop continues to run till the lower limit continues to be less than the upper limit.

### 3. Palindrome in C

Before we begin our discussion on Palindromes, it is important to understand iteration, conditional statements, operators, and strings in C.

A palindrome in C is nothing but either a number, word or phrase that is read in a similar fashion forward as well as backward.

For instance,

• The number 181 is a palindrome.
• The word “hannah” is a palindrome.
• The phrase “madam or nurses run” is a palindrome.

We will discuss 2 Basic C programs based on Palindromes:

1. To check if the given number is a palindrome or not.
2. To check if the given word is a palindrome or not.

#### Example of Palindrome Series in C

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int number, number_reverse = 0, remainder, original_number;

printf("Enter an integer: ");
scanf("%d", &number);

original_number = number; // Storing the value of the number into a variable original_number

/* Reversing the number */
while( number!=0 )
{
remainder = number % 10;
number_reverse = number_reverse * 10 + remainder;
number /= 10;
}
// To check if the original number and the reversed number are equal or not
if (original_number == number_reverse)
printf("%d is a palindrome.\n", original_number);
else
printf("%d is not a palindrome.\n", original_number);
return 0;
}```

Code- Output- Elucidation

Declaration: We declare the variable ‘number’ to store the value entered by the user to check if it is a palindrome or not, the variable ‘remainder’ to find the remainder when the integral division of that number is performed with 10, and the variable ‘original_number’ to store the value of the number entered by the user temporarily. We initialize the variable ‘number_reverse to 0 for loop traversal.

Input: We take the input of the number to check if it is a palindrome or not from the user in the variable ‘number’.

Logic: The while loop runs till the number entered by the user becomes 0

• A set of conditions is given to extract the reversed number from the number entered by the user.
• If the value of the variable ‘number_reverse’ matches to that of the original number, then the given number enter by the user is a palindrome.

### Explanation through Dry Run:

Consider the value of the number = 181

The while loop will run for 3 times, given as:

1. First iteration, number = 181

remainder = 181 % 10 = 1
number_reverse = ( 0 * 10 ) + 1 = 1
number = 181 / 10 = 18

2. Second iteration, number = 18

remainder = 18 % 10 = 8
number_reverse = ( 1 * 10 ) + 8 = 18
number = 18 / 10 = 1

3. Third iteration, number = 1

remainder = 1 % 10 = 1
number_reverse = ( 18 * 10 ) + 1 = 181
number = 1 / 10 = 0

The loop will no longer run as the base condition no longer continues to be satisfied as the number reached its minimum value = 0

Here is a program in C that checks if the given word entered by the user as a string if a palindrome or not:

```#include <stdio.h>
#include <string.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

char string1, string2, temp;
int i,j;
printf("Enter the first string to check if it is a palindrome\n");
fgets(string1,30,stdin);
strcpy(string2, string1);  // Copying the first string to the second one
// Reversing the string

i = 0;
j = strlen(string2)-1;
while (i < j)
{
temp = string2[i];
string2[i] = string2[j];
string2[j] = temp;
i++;
j--;
}
puts(string2);
if (strcmp(string1, string2) == 0)  // Comparing input string with the reverse string
{
printf("The string %s is a palindrome\n",string1);
}
else
{
printf("The string %s isn't a palindrome\n",string1);
}
return 0;
}```

Code- Output- ### 4. Factorial Program in C

Before we begin our discussion on how to find the factorial of a number, it is important to be thorough with Iteration, Data types, Conditional statement, and Operators in C.

Let us try to understand what is a factorial:

The factorial of a number n is given by: 1 x 2 x 3 x 4 ……………….. x n.

For instance, the factorial of 5 given given by: 1 x 2 x 3 x 4 x 5 = 120.

Example of Factorial in C

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int number, iteration;
long factorial = 1; // initializing factorial to 1

printf("Enter a positive integer: ");
scanf("%d",&number);

if (number < 0) // If the user enters a negative number
printf("The factorial of a negative number does not exist\n");
else
{
for(iteration = 1; iteration <= number; iteration++)
{
factorial = factorial*iteration;
}
printf("The factorial of %d is %lu\n", number, factorial);
}
return 0;
}
```

Code- Output- Elucidation

We can understand the working of this program with the help of a dry run:

Consider number = 8

For i = 1

factorial = 1 * 1 = 1

For i = 2

factorial = 1* 2 = 2

For i = 3

factorial = 2 * 3 = 6

For i = 4

factorial = 6 * 4 = 24

For i = 5

factorial = 24 * 5 = 120

For i = 6

factorial = 120 * 6 = 720

For i = 7
factorial = 720 * 7 = 5040

For i = 8

factorial = 5040 * 8 = 40320

Hence, we obtain the value of 8! = 40320

Key takeaway: You can even find the factorial of a number using recursion in C.

### 5. Number Reversal in C

Before we begin our discussion on number reversal, it is important to understand Operators in C and Iteration in C.

The purpose of this program is to reverse a number.

For instance,

The reversal of the number 67193 is 39176

Example of Number Reversal in C

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int number, number_reverse, remainder;
number_reverse = 0; // initializing number_reverse to 0

printf("Enter a number in the form of an integer: ");
scanf("%d", &number);

while(number != 0)
{
remainder = number%10;
number_reverse = number_reverse*10 + remainder;
number = number/10;
}
printf("The reversed number is: %d\n", number_reverse);
return 0;
}```

Code- Output- Elucidation

The same concept has been applied to the palindrome problem.

### 6. Matrix Manipulation in C

Before we begin our discussion on matrix multiplication, it is important to understand Arrays and Multidimensional arrays in C.

Matrix Multiplication is one of the popular basic of C programs, let us begin our discussion by trying to understand what a matrix is.

In mathematics, a matrix is referred to as a rectangular array of elements used to represent numbers, symbols, or expressions in the form of horizontal rows and vertical columns.

The general form of a m x n matrix is: This is an example of a 4 x 3 matrix: This is how you can visualize a matrix, say A[m][n] with m number of rows and n number of columns: It is important to note that not every matrix can be multiplied with another matrix.

Condition for multiplication of 2 matrices:

Two matrices can be multiplied only when the number of columns of the first matrix is equal to the number of rows in the second matrix.

How to multiply 2 matrices:

Each element of the resultant matrix is obtained by multiplying the elements of each row of the first matrix with its corresponding elements of each column of the second matrix and adding them together.

Example of Matrix Multiplication in C

```#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int row1, column1, row2, column2, sum = 0;
int iteration1, iteration2, iteration3;
int matrix1, matrix2, product;

printf("Enter the number of rows and columns of first matrix: ");
scanf("%d%d", &row1, &column1);

printf("Enter the elements of first matrix\n\n");

for (iteration1 = 0; iteration1 < row1; iteration1++)
{
for (iteration2 = 0; iteration2 < column1; iteration2++)
{
scanf("%d", &matrix1[iteration1][iteration2]);
}
}

printf("Enter the number of rows and columns of second matrix: ");
scanf("%d%d", &row2, &column2);

if (column1 != row2)
{
printf("The matrices can't be multiplied.\n\n");
}
else
{
printf("Enter the elements of second matrix\n\n");

for (iteration1 = 0; iteration1 < row1; iteration1++)
{
for (iteration2 = 0; iteration2 < column2; iteration2++)
{
scanf("%d", &matrix2[iteration1][iteration2]);
}
}
for (iteration1 = 0; iteration1 < row1; iteration1++)
{
for (iteration2 = 0; iteration2 < column2; iteration2++)
{
for (iteration3 = 0; iteration3 < row2; iteration3++)
{
sum = sum + matrix1[iteration1][iteration3]*matrix2[iteration3][iteration2];
}

product[iteration1][iteration2] = sum;
sum = 0;
}
}

printf("The product of the matrices is:\n\n");

for (iteration1 = 0; iteration1 < row1; iteration1++)
{
for (iteration2 = 0; iteration2 < column2; iteration2++)
printf("%d\t", product[iteration1][iteration2]);
printf("\n");
}
}
return 0;
}```

Output- ### 7. Convert Decimal to Binary Number in C

Before we begin our discussion on decimal to binary conversions, it is important to understand Operators in C.

Here is a code in C that illustrates how to convert a number in a decimal number system to binary number system:

```#include <stdio.h>
#include <math.h>
long decimal_to_binary(int decimal_number)
{
long binary_number = 0;
int remainder, iteration = 1;

while (decimal_number != 0)
{
remainder = decimal_number%2;
decimal_number = decimal_number/ 2;
binary_number = binary_number+ remainder*iteration;
iteration = iteration*10;
}
return binary_number;
}

int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int decimal_number;
printf("Enter a positive integer in decimal system: ");
scanf("%d", &decimal_number);
printf("The number %d in decimal system to binary is %ld\n", decimal_number, decimal_to_binary(decimal_number));
return 0;
}```

Code- Output- ## Summary

In this tutorial, we discussed the basic C programs that helped us to develop a deep understanding of the logical statements and algorithms involved while solving a problem. After discussing all these seven problems in detail, you are ready to master a couple of fundamental problems that you might encounter in the long run. Good luck on your programming journey!

Next step – Functions in C Language

If you have any queries regarding the working of any of these Basic of C programs, feel free to share it in the comment section below.