## Program to Find Maximum and Minimum in an Array of Number using Pointers

The C program find the maximum and minimum of an array of integers is written using Dev-C++ version 4.9.9.2 installed on a 64-bit windows 7 operating system.

This program is intended for intermediate level learners familiar with the concept of pointers.

### Problem Definition

We want to write a program to find the maximum and minimum of an array of numbers using pointers. The program accept 10 numbers from user and store them in an array of integers.

We then use two pointers to locate the maximum and the minimum of the array. The pointer traverse through all the values of the array and compares it with value of max which is 0 at the beginning.

If the number in array greater than max current value, then make the new number as new max value, otherwise keep looking for another max. The same logic applies to minimum of array.

### Flowchart

To understand the logic of the program see the flowchart given below.

### PROGRAM

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,temp,tempmin, max,min;
max = temp = tempmin = 0;
int arr[10];
min = arr[0];
printf("Enter number upto 10 numbern");
for(i=0;i<=10;i++)
{
scanf("%d",&arr[i]);
}
int *ptr1, *ptr2;
ptr1 = arr;
ptr2 = arr;
for(i=1;i<=10;i++)
{
if(max <= *ptr1)
{
temp = max;
max = *ptr1;
*ptr1 = temp;
}
ptr1++;
}
printf("MAX=%d\n",max);
// finding minimum
for(j=1;j<=10;j++)
{
if(min >= *ptr2)
{
tempmin = *ptr2;
*ptr2 = min;
min = tempmin;
}
ptr2++;
}
printf("MIN=%d\n",min);
system("PAUSE");
return 0;
}

## C Program to Write a String Copy Function

C programming language has many built-in string functions including the strcpy() function. The strcpy() function copy text from one string to another. To understand the working of string copy function, we write a program that copies text from one string to another.

This program is written using Dev-C++ compiler installed on a Windows 7 64-bit machine and it is intended for beginners of C programming.

### Problem Definition

The program reads a string and then copy that string to another string and display the results.

Each string is an array of characters with a null character at the end of the string. Make sure that the size of the string is same or greater than string 2 size, otherwise, there will be problem copying string 1 to string 2.

Start copying string character by character from string 1 to string 2, until we reach a null character.

Assign a null character to the string 2 when all characters of string 1 are copied successfully. This is because every string in C language must end with a ‘\0’.

For Example:-

Suppose the program reads “hello” to string 1, and then the starts copying string 1 to string 2 one character at a time until a null is encountered. See figure below.

### Program Code

/* C Program to Write Strcopy () Function */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char s1 [10];
char s2 [10];
int i;
void strcopy (char s2 [10], char s1 [10]);
/* Read the String 1 */
printf ("Enter String 1 :");
gets (s1);
/* Copy the String 1 to String 2 */
strcopy (s2, s1);
/* Print both the functions */
for (i=0; i<35; i++)
printf("_");printf("\n\n");
printf("String2=%s \n\nString1=%s\n",s2,s1);
for(i=0;i<35;i++)
printf("_");printf("\n\n");
system("PAUSE");
return 0;
}
void strcopy (char s2[10], char s1[10])
{
int i= 0;
while(s1[i] !='\0')
{
s2[i] = s1[i];
++i;
}
s2[i]='\0';
}

## Program to process marks of student and display grade.

AIM:

To write a program to compute marks of student and display Grade.

PROGRAM:

/* Program to process marks of students and display Grades */

#include <stdio.h>

#include <conio.h>

struct student{

char name[25];

int rollno;

int marks;

};

main()

{

struct student stud[15];

int i,n;

clrscr();

/* Get the number of students */

printf(“ENTER THE NUMBER OF STUDENTS:”);

scanf(“%d”,&n);

/* Get the value of Students marks*/

for(i=0;i<n;i++)

{

printf(“ENTER STUDENT INFORMATION:n”);

printf(“NAME:”);

scanf(“%s”,&stud[i].name);

printf(“ROLLNO:”);

scanf(“%d”,&stud[i].rollno);

printf(“MARKS( IN %):”);

scanf(“%d”,&stud[i].marks);

}

/* Print student information*/

for(i=0;i<n;i++)

{

if(stud[i].marks <= 50)

{

}

else if(stud[i].marks > 50 && stud[i].marks <= 55)

{

}

else if(stud[i].marks > 55 && stud[i].marks <= 60)

{

}

else if(stud[i].marks > 60 && stud[i].marks <= 75)

{

}

else if (stud[i].marks > 75 && stud[i].marks <= 90)

{

}

else if(stud[i].marks > 90)

{

}

}

printf(“_______________________________n”);

printf(“________________________________n”);

for(i=0;i<n;i++)

{

}

printf(“________________________________n”);

getch();

return 0;

}

## C Program to Compute Nth Factorial using Recursion

Factorial is a very important concept of mathematics and it’s used in counting numbers, probability and other areas of mathematics.

There are many ways to write a program for factorial, but for the sake of learning, we use two important way to program this problem.

The two ways to write factorial program are as follows.

1. Program to compute nth factorial without using recursion.
2. Program to compute nth factorial using recursion.
A recursion is an ability to a function or a procedure to call itself several times until the given problem is solved.

Since you are programming using recursion, we will discuss more of it. A function calls itself by providing parameter one less than the original value for all subsequent calls.
It means if Add(n) is the original function with parameter n, then it calls itself with Add(n-1) or it totally depends on the problem.

If you do not understand, do not worry – continue reading.

This program uses advanced concept and suitable for an intermediate level learner of C programming and it is written using Dev-C++ compiler version 4.9.9.2.
In the following sections, you will find problem definition, flowchart, program code for practice and verified output of the program to help you learn faster.

Problem Definition

In simple words, the factorial of a given number n is the product of all the numbers up to that number, including the number.

So factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120

You will be practicing the program for factorial with recursion. In the previous section, we introduced you to the concept of recursion.To elaborate on that see the example below.
If factorial is a function then,

 Figure: Recursion

Every time factorial function calls itself, it provides one less of the parameter to the function until the factorial (1) is reached.

Then it starts working from factorial (1) to the top – factorial (n) and prints the result.

Flowchart

 Flowchart: C Program to Compute Nth Factorial using Recursion

Program Code

/*Program to compute Nth factorial */

#include < stdio.h >
#include < stdlib.h >

int fact (int n)

{

unsigned int f;

if ((n == 0) || (n == 1))

return (n);

else

f = n * fact (n 1); /* Compute factorial by Recursive calls */

return (f);

}

main ()

{

int i, n;

printf (“Enter the Number :”);

scanf (“%d”, & n);

/* Printing results */

for (i = 0; i < 30; i++)
printf (“_”);
printf (“nn”);

printf (“Factorial of Number %d is %dn”, n, fact (n));

for (i = 0; i < 30; i++)
printf (“_”);
printf (“nn”);

system (“PAUSE”);
return 0;

}



Output

The output of the program is given below.

 Output: C Program to Compute Nth Factorial using Recursion

## C Program to Check if a Number is Prime or Not

The program to check prime number is written using Dev-C++ compiler installed on a Windows 7 64-bit system. You can use any other standard compiler to compile the program and it will still work.This program is intended for beginners learning C programming language. To help you understand the program, you will find following section helpful – problem definition, flowchart, program code and verified output.

### Problem Definition

Prime numbers are very important topic in mathematics. It is usually easy to find the small prime numbers, but very difficult to identify a prime number, when the numbers grow larger.We want to write a program to test whether a given number is a prime number or not.

What is a Prime number ?

Any positive number greater than 1 which is divisible only by itself and number one is called a Prime number, all other numbers are Composite numbers.

For example

63 = 3 x 21

The number 63 has two factors 2 and 21, therefore it is a composite number.

73 = 73 x 1

The factors of number 73 are 73 itself and 1, therefore, 73 is a prime number.

How do we process the given input number?

Here are the steps to process the input number in the program.

Step 1 – Get the number, N

Step 2 – Check if the number, N is divisible by 2,3, or 5

Step 3 – If the number, N is divisible, then the remainder = = 0

Step 4 – If number is divisible, then check = 0, check = 1

Step 5 – If check = = 1, number is a prime number

Step 6 – Print the output

Step 7 – End the program

### Program Code

/*Program to Check if the number is Prime or not */
#include <stdio.h>
#include <conio.h>
main()
{ int i,check,num; int a,b,c; /* Get the number */
printf("Enter a number:");
scanf("%d",&num);
/* check if the number is divisible by 2,3 or 5*/
for( i=1; i<=5;i++)
{
if(num % i == 0)
{
check = 0;
}
else
{
check = 1;
}
}
/*print the result */
if(check == 1)
{
for(i=0;i<35;i++)
printf("_");printf("\n\n");
printf("The Number %d is a Prime Number\n\n",num);
for(i=0;i<35;i++)
printf("_");printf("\n\n");
}
else
{
for(i=0;i<35;i++)
printf("_");printf("\n\n");
printf("The Number %d is not a Prime Number\n\n",num);
for(i=0;i<35;i++)
printf("_");printf("\n\n");
}
getch();
return 0;
}

### Output

The output of the above program is given below.

## C Program To Implement Linear Search and Binary Search Algorithm

This program is written on Turbo C++ version 3.0, but you can use any other standard C compiler to code and run this program.

### Problem Definition

To write a program to implement Linear Search and Binary Search algorithm.

### Program Code

/* Program to implement linear search and Binary search */
#include <stdio.h>
#include <conio.h>
main()
{
/* Declare variables - array_of_number,search_key,i,j,low,high*/
int array[100],search_key,i,j,n,low,high,location,choice;
void linear_search(int search_key,int array[100],int n);
void binary_search(int search_key,int array[100],int n);
clrscr();
/* read the elements of array */
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&n);
printf("ENTER THE ELEMENTS OF THE ARRAY:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&array[i]);
}
/* Get the Search Key element for Linear Search */
printf("ENTER THE SEARCH KEY:");
scanf("%d",&search_key);
/* Choice of Search Algorithm */
printf("___________________\n");
printf("1.LINEAR SEARCH\n");
printf("2.BINARY SEARCH\n");
printf("___________________\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
linear_search(search_key,array,n);
break;
case 2:
binary_search(search_key,array,n);
break;
default:
exit(0);
}
getch();
return 0;
}
/* LINEAR SEARCH */
void linear_search(int search_key,int array[100],int n)
{
/*Declare Variable */
int i,location;
for(i=1;i<=n;i++)
{
if(search_key == array[i])
{
location = i;
printf("______________________________________\n");
printf("The location of Search Key = %d is %d\n",search_key,location);
printf("______________________________________\n");
}
}
}
/* Binary Search to find Search Key */
void binary_search(int search_key,int array[100],int n)
{
int mid,i,low,high;
low = 1;
high = n;
mid = (low + high)/2;
i=1;
while(search_key != array[mid])
{
if(search_key <= array[mid])
{
low = 1;
high = mid+1;
mid = (low+high)/2;
}
else
{
low = mid+1;
high = n;
mid = (low+high)/2;
}
}
printf("__________________________________\n");
printf("location=%d\t",mid);
printf("Search_Key=%d Found!\n",search_key);  printf("__________________________________\n");
}

## C Program to Implement Bubble Sort Algorithm

This program is an implementation of Bubble sort algorithm. The program receives unsorted input numbers and sort the number in ascending or descending number as an output.

Linear Search and Binary Search

Insertion Sort

Maximum and Minimum Number in an Array

This program is written with Turbo C++ version 3 compiler, but you can use any other standard C compiler to code and run this program. It is intended for intermediate to advanced level learner of C language.

To help you understand this program see following sections – Problem definition, Flowchart of the program and a verified output.

### Problem Definition

In this program we read some numbers into an array. The numbers are unsorted and our goal is to sort the input numbers using Bubble Sort algorithm.

In depth analysis of the Bubble Sort algorithm is not in the scope of the article. We discuss the working of this algorithm and later implement the same using C program.

The working of this algorithm is easy to understand with an example. Suppose we are given following unsorted numbers to sort using bubble sort.

unsorted = 2 4 1 8 6
Pick the first number = 2

Compare with all the number one-by-one from right-to-left and when a[j] > a[j+1], swap the numbers.

2 < 4 (no swap)
2 > 1 (swap)
The array is now = 1 2 4 8 6

Pick the second number = 2 and it is smaller than all the other numbers.

Pick the third number = 4 and it is found to be smaller the rest of the numbers.

Pick the fourth number = 8,

8>6 (swap)
The array is now = 1 2 3 4 6 8

Pick the fifth number = 6 to sort and it is again smaller than last number 8. In this way the whole array is sorted.

### Program Code Bubble sort

/*PROGRAM FOR BUBBLE SORTING */
#include <stdio.h>
#include <conio.h>
#define N 100 main() {
int array[N],n,i,j,temp;
clrscr();
/* Read number of elements in array */
printf("ENTER NUMBER OF ELEMENTS:");
scanf("%d",&n);
/* Read the elements of array */
printf("ENTER %d ELEMENTS\n",n);
for(i=0;i<n;i++) {
scanf("%d",&array[i]);
}
/* Do Bubble Sort */
for(i=0;i<(n-1);i++) {
for(j=0;j<(n-i-1);j++) {
if(array[j]>array[j+1]) {
temp = array[j];
array[j]=array[j+1];
array[j+1] = temp;
}
}
}
/* Print the Sorted Array */
printf("_____________________________________________\n");
printf("Sorted list in Ascending Order:\n");
for(i=0;i<n;i++) {
printf("%d\t",array[i]);
}
printf("\n____________________________________________\n");
getch();
return 0;
}

### Output-Bubble Sort Algorithm

The output of the above program for bubble sort algorithm is given below. The first ask for number of elements and then the elements itself.

The output is sorted number in ascending order. You can modify the program source code to change the order.

## C Program to Manipulate Matrices

Matrix is a mathematical concept that we used to learn in schools and collages. To learn about matrices, you have to learn linear algebra basics.

Given a set of linear equations, you can derive a matrix and then perform simple operations on them. Linear algebra is beyond the scope of this post, we only discuss about the matrix manipulation program in C.

The C program reads two square matrices and perform addition, subtraction, multiplication and transpose operation on them.

This program is intended for intermediate level learner of C programming and written using Dev-C++ version 4.9.9.2 compiler installed on a Windows 7 64-bit machine.

To understand this program, some familiarity with matrix concepts is necessary as well as recommended.

### Problem Definition

Suppose you are given two matrices, then you can perform following simple operations on them.

2. Subtraction
3. Multiplication
4. Transpose

In this program, you are going to use only square matrices for all operations.

For example, if A and B are two matrices then,

A         $= \begin{bmatrix} 1 & 2 & 3 \\ 1 & 1 & 4 \\ 2 & 3 & 5 \end{bmatrix}$

and

B         $=\begin{bmatrix} 3 & 1 & 3 \\ 2 & 1 & 1 \\ 3 & 6 & 3 \end{bmatrix}$

A + B   $= \begin{bmatrix} 4 & 3 & 6 \\ 3 & 2 & 5 \\ 5 & 9 & 8 \end{bmatrix}$

A – B $=\begin{bmatrix}-2 & 1 & 0 \\ -1 & 0 & 3 \\ -1 & -3 & 2 \end{bmatrix}$

A * B $=\begin{bmatrix} 16 & 21 & 14 \\ 17 & 26 & 16 \\ 27 & 35 & 24 \end{bmatrix}$

AT $= \begin{bmatrix} 1 & 1 & 2 \\ 2 & 1 & 3 \\ 3 & 4 & 5 \end{bmatrix}$

In multiplication of two matrices, the row in A multiply all elements in column in B and the result is added. You do this for all rows and columns.In transpose of matrix, the row becomes the column and the column becomes the row, in the resultant matrix.The addition and subtraction of matrices are straight-forward operations.

### Program Code

/* PROGRAM IN C TO MANIPULATE MATRIX */
#include <stdio.h>
#include <stdlib.h>
#define N 10
main() {
int a[10][10],b[10][10];
int c[10][10];
int i,j,k,n,choice;
void menu(int a[N][N],int b[N][N],int c[N][N],int n);
/* Get the Size of the Matrices */
printf("ENTER SIZE OF MATRIX:");
scanf("%d",&n);
i=0;
while(choice !=5) {
i++;
}
system("PAUSE");
return 0;
}
void menu(int a[N][N],int b[N][N],int c[N][N],int n) {
int i,j,k,choice;
printf("ENTER A CHOICE:\n");
for(i=0;i<30;i++)
printf("_");printf("\n\n");
printf("2.SUBTRACTION\n");
printf("3.MULTIPLICATION\n");
printf("4.TRANSPOSE\n");
printf("5.QUIT\n");
for(i=0;i<30;i++)
printf("_");printf("\n\n");
scanf("%d",&choice);
i=0;
switch(choice) {
case 1: for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
/* Print Result */
system("cls");
printf("RESULT\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("n");
}
for(i=0;i<30;i++)
printf("*");
printf("\n\n");
break;
case 2:
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
/* Print Result */
system("cls");
printf("RESULT\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("_");printf("\n\n");
break;
case 3:
for(i=0;i<n;i++) {
for( j=0;j<n;j++) {
for( k=0;k<n;k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
/* Print the result */
system("cls");
printf("Result\n");
for( i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("_");
printf("\n\n");
break;
/* Transpose Matrix -A */
case 4:
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
c[i][j] = a[j][i];
}
}
/* Print Result */
system("cls");
printf("*RESULT*\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("_");
printf("\n\n");
break;
default: exit(0);
}
}
/* Read Matrix a and b*/
void read_matrix(int a[N][N],int b[N][N],int n) {
int i,j;
printf("ENTER MATRIX VALUES ROWWISE:\n\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%d",&a[i][j]);
} }
/* Print Matrix A */
printf("*Matrix-A*\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("*");
printf("\n\n");
printf("ENTER MATRIX VALUES ROWWISE:\n\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%d",&b[i][j]);
}
}
/*Print Matrix-B */
printf("*Matrix-B*\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%dt",b[i][j]);
}
printf("\n");
}
for(i=0;i<30;i++)
printf("*");
printf("\n\n");
}

## C Program to find Reminder without Modulo Operation

The program to find remainder is usually implemented using a modulo operator. You get the immediate result with any difficulty. However, there is another way to achieve the same results without using the modulo operator.

This is a frequent question asked in lot of C programming interviews to test the aptitude of the candidates. So it is a well known problem and it solution is also known to most of the C learners.

This program is written using Turbo C++ 3.0 compiler installed on a windows XP 32-bit system. You are free to use any standard C compiler and still achieve the same results.

### Problem Definition

The program receives an input numbers – divisor and a dividend. The quotient of the division is easily obtained using the division operator (/).

Now we cannot use the modulo operator and have to find another way to find the remainder.There are two steps to do that

1. Find and intermediate number by multiplying quotient * divisor.
2. subtract the intermediate number from dividend.

For example, divisor $= 10$, dividend $= 53$ , find the remainder.

quotient $= 53 / 10 = 3$

intermediate number $= 5 * 10 = 50$

remainder = dividend – intermediate number

remainder $= 53 - 50 = 3$

Check the flowchart below to understand this logic.

### Program Code

#include
#include
void main()
{
int n1,n2,quotient,reminder;
clrscr();
printf("Enter two numbers:\n");
scanf("%d %d",&n1,&n2);
quotient = n1/n2;
reminder = n1 - quotient * n2;
printf("Remainder = %d\n",reminder);
getch();
}

### Output

The output of the above program is given below.

## C Program to Reverse a given String

The program to reverse a given string takes input string and output reverse of the string. This is a simple C program intended for beginners of C language. The program is written and compiled using Dev-C++ compiler version 4.9.9.2 installed on a Windows 7 64-bit system. However, you can use any other standard C compiler to run this program.

To help you learn this program, we have following sections: problem definition, program source code and output of the program.

### Problem Definition

The program takes an input string at run-time using the builtin function gets (str) where str is a character array. In this array of characters, the beginning characters are exchanged with the ending characters of the string with the help of a temp variable.

For example

If the string is “ELEPHANT”. The letter is ‘E’ and ‘T’ are exchanged until all characters are reversed. The final output is TNAHPELE.

### Program Code

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char str[100],temp;
int i,j=0;
printf("\n Enter the String: ");
gets(str);
i=0;
j = strlen(str)-1;
while(i<j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\n Reverse String is %s",str);
getch();
}

### Output

The output of the above program is given below. When the program ask for a string input, the user enters – MATHEMATICS. The program reverses the string and give following output.