Linear Search: C

In this video tutorial we shall see how we can search for a number in an array in linear fashion.

First ask the user to enter the number to be searched. Store it inside a variable called key.
Now start comparing key value with a[i] using a for loop. If the key is found in the array, then assign 1 to flag orelse flag will be 0.

Depending upon the value of flag, print the message.

If flag is 1, search is successful.
If flag is 0, search failed. in the sense, the number is not present in the given array.

Video Tutorial: Linear Search: C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=AqjVd6FVFbE [Watch the Video In Full Screen.]



Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int a[20], key, N, flag = 0, i;
	clrscr();
 
	printf("Enter array limit\n");
	scanf("%d", &N);
 
	printf("Enter %d elements\n", N);
	for(i=0; i < N; i++)
	 scanf("%d",&a[i]);
 
	printf("Enter the number to be searched\n");
	scanf("%d", &key);
 
	for( i=0; i < N; i++)
	 if( a[i] == key )
	 {
		flag = 1;
		break;
	 }
 
	if(flag)
		printf("\nSearch Successful\n");
	else
		printf("\nSearch Failed\n");
	getch();
}

Output
Enter array limit
5
Enter 5 elements
77
0
13
9
55
Enter the number to be searched
9
Search Successful

Find First and Second Biggest In An Array, Without Sorting It: C

We assume that a[0] has first biggest and a[1] has the second biggest value.

Now check if this assumption is correct, if not, swap the values.

 
 if( sbig > fbig )
 {
    temp = sbig;
    sbig = fbig;
    fbig = temp;
 }

Now since we have already checked with a[0] and a[1], we start the comparison from a[2], till N.

for(i=2; i < n ; i++)
	 if(a[i] > fbig)
	 {
		sbig = fbig;
		fbig = a[i];
	 }
	 else if(a[i] > sbig)
		sbig = a[i];

Now, if a[i] contains a number which is bigger than fbig, we transfer the value of a[i] to fbig and the value present in fbig to sbig;
If the value of a[i] is not bigger than fbig, then we check it with sbig. If a[i] is bigger than sbig, then we assign the value of a[i] to sbig.

This way, at the end of the loop fbig will have the first biggest and sbig will have the second biggest element/number in the array.

Video Tutorial: Find First and Second Biggest In An Array, Without Sorting It: C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=DzZvT87WuGQ [Watch the Video In Full Screen.]



Full source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int a[20], N, i, fbig, sbig, temp;
	clrscr();
 
	printf("Enter array limit\n");
	scanf("%d", &N);
 
	printf("Enter %d array elements\n", N);
	for(i=0; i < n ; i++)
	 scanf("%d", &a[i]);
 
	fbig = a[0];
	sbig = a[1];
 
	if( sbig > fbig )
	{
	   temp = sbig;
	   sbig = fbig;
	   fbig = temp;
	}
 
	for(i=2; i < n ; i++)
	 if(a[i] > fbig)
	 {
		sbig = fbig;
		fbig = a[i];
	 }
	 else if(a[i] > sbig)
		sbig = a[i];
 
	 printf("First Big is %d and Second big is %d", fbig, sbig);
 
	 getch();
 
}

Output:
Enter array limit
6
Enter 6 array elements
99
108
777
723
786
999
First Big is 999 and Second big is 786

Find Biggest In An Array, Without Sorting It: C

First we have to assume that a[0] is biggest. Now store the value of a[0] inside a variable called big. Now compare value of big with other values in the array. If big is less than any other value, then store the bigger value inside variable big.

Video Tutorial: Biggest In An Array, Without Sorting It: C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=tKaImGrxgKo [Watch the Video In Full Screen.]



Note that, the previous value inside the variable big is discarded.

Full source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int big, a[20], N, pos, i;
	clrscr();
 
	printf("Enter the array size\n");
	scanf("%d", &N);
 
	printf("Enter %d elements\n", N);
	for(i=0; i<n ; i++)
	 scanf("%d", &a[i]);
 
	big = a[0];
	pos = 0;
 
	for(i=1; i<N; i++)
	 if(a[i] > big)
	 {
	  big = a[i];
	  pos = i+1;
 
	 }
 
	 printf("Big is %d and its position is %d", big, pos);
 
	 getch();
}
 
</n>

Output
Enter the array size
5
Enter 5 elements
2
0
100
108
55
Big is 108 and its position is 4

Bubble Sort: C

In this video tutorial we illustrate the bubble sort algorithm and also see how to write it in C programming, for 5 elements as well as to N elements.

In bubble sort, a[0] is compared with a[1]. a[1] with a[2], a[2] with a[3] .. so on
if a[0] is greater than a[1], the values are swapped. This logic continues till the end of the array.

At the end of each iteration, 1 element gets sorted.

If the array size is 5, then we will have 4 iterations. i.e., if size is N, then we will have N-1 iterations.



Bubble Sort: Full source code for 5 elements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int a[5] = { 15, 0, -2, 8, 3 };
	int i, j, temp;
	clrscr();
 
	printf("Array elements are..\n");
	for( i=0; i&lt;5; i++)
	 printf("\n%d", a[i]);
 
	for(j=3; j>=0; j--)
	 for(i=0; i< =j; i++)
	  if( a[i] > a[i+1] )
	  {
	    temp = a[i];
	    a[i] = a[i+1];
	    a[i+1] = temp;
	  }
 
	printf("\nafter sorting..\n");
	for(i=0; i&lt;5; i++)
	 printf("\n%d", a[i]);
 
	getch();
}

Output:
Array elements are..
15
0
-2
8
3

after sorting..
-2
0
3
8
15

Video Tutorial: Bubble Sort: C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=Ne9QNWuGJvw [Watch the Video In Full Screen.]



Bubble Sort: Full source code for N elements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int a[20];
	int i, j, temp, N;
	clrscr();
 
	printf("\nEnter array size\n");
	scanf("%d", &N);
 
	printf("Enter %d elements\n", N);
	for(i=0; i<n ; i++)
	 scanf("\n%d", &a[i]);
 
	printf("Array elements are..\n");
	for( i=0; i<N; i++)
	 printf("\n%d", a[i]);
 
	for(j=N-2; j>=0; j--)
	 for(i=0; i< =j; i++)
	  if( a[i] > a[i+1] )
	  {
	    temp = a[i];
	    a[i] = a[i+1];
	    a[i+1] = temp;
	  }
 
	printf("\nafter sorting..\n");
	for(i=0; i</n><n ; i++)
	 printf("\n%d", a[i]);
 
	getch();
}

Output:
Enter array size
10
Enter 10 elements
20
19
18
17
16
15
14
13
12
11

Array elements are..
20
19
18
17
16
15
14
13
12
11

after sorting..
11
12
13
14
15
16
17
18
19
20

Selection Sort of N numbers: C

Selection sort is a sorting technique which is inefficient on large list of elements. Its simple and easy to understand.

In selection sort, first we select one element and compare it with the rest of the elements. If the compared element is smaller than the selected element, then those numbers are sorted. This approach is continued till the last element in the array index.

The number of iterations is 1 less than the array size.
i.e., if the user enters array size as 5, then there will be 4 iterations.

Below is an example:
Lets assume, user has entered the array size as 5.
and entered 13, 0, -2, 8, 3 as array elements.


First Iteration:

selection-sort-first-iteration



In first iteration, a[0] is selected. and is compared with a[1], a[2], a[3] and a[4]. and is swapped with the least value in the array.


Second Iteration:

selection-sort-second-iteration



In the second iteration, we leave off a[0] and select a[1] and compare it with the rest of the elements in the array i.e., a[2], a[3] and a[4]. and swap the least value with a[1]

Third Iteration:

selection-sort-third-iteration



In third iteration, we select a[2] and compare it with a[3], a[4] and swap the least value to a[2]


Forth Iteration:

selection-sort-forth-iteration



In forth iteration, we select a[3] and compare it with a[4] and swap the least value.

At the end of 4 iterations all array elements are sorted.

Video Tutorial: Selection Sort of N numbers: C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=NWLPmRdIPBA [Watch the Video In Full Screen.]



Since the array index starts from 0, and we need not compare the last element in the array with any other elements, we iterate the outer loop from 0 to N-2.
We iterate the inner loop from i+1 till N-1.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int a[20], i, j, N, temp;
	clrscr();
 
	printf("Enter the size of the array\n");
	scanf("%d", &N);
 
	printf("Enter %d elements\n", N);
	for(i=0; i<n ; i++)
	 scanf("%d", &a[i]);
 
	for( i=0; i<=N-2; i++ )
	 for( j=i+1; j<=N-1; j++)
	  if(a[i] > a[j])
	  {
	     temp = a[i];
	     a[i] = a[j];
	     a[j] = temp;
	  }
 
         printf("\nSorted Array:\n");
	  for(i=0; i</n><n ; i++)
	   printf("\n%d", a[i]);
 
	   getch();
 
}

Output:
Enter the size of the array
5
Enter 5 elements
13
0
-2
8
3
Sorted Array:
-2
0
3
8
13

Find the Factorial of a Number: C

This video tutorial illustrates finding factorial of a given number using C programming.

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Ex:
if user enters 5, then the factorial is 1 * 2 * 3 * 4 * 5 i.e., factorial = 120
This logic must be handled with a c program.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int num, i, fact = 1;
	clrscr();
 
	printf("Enter a number\n");
	scanf("%d", &num);
 
	for( i = 1; i < = num; i++ )
	 fact = fact * i;                // fact *= i;
 
	printf("\nFactorial of %d is %d", num, fact);
 
	getch();
 
}

Here the for loop stars from 1 and not 0. As anything multiplied by 0 would also be zero.

Note: Factorial of 0 is 1. i.e., 0! = 1

factorial-cpp


fact *= i; is the compact representation of fact = fact * i;

Video Tutorial: Factorial in c


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=PYLaSrO84nU [Watch the Video In Full Screen.]



Output:
Enter a number
5
Factorial of 5 is 120

Also Watch/Read: Find the Factorial of a Number: C++

Find Sum of Digits In A Given Number: C

Video Tutorial to illustrate, C Program to find sum of digits in the given/user entered integer number.

In this program we assign variable sum = 0 to avoid garbage values in sum before the calculation, which would result in wrong output.
We store the user entered value in num.

1
2
3
4
5
6
 while( num )
  {
    rem = num % 10;
    sum = sum + rem;
    num = num / 10;
  }

Here the loop executes until the value of num is zero.

If user enters 123, we apply the modulus to get the individual values.
Ex:
123 % 10 = 3
12 % 10 = 2
1 % 10 = 1

We get 123, 12 and 1 by dividing the original value by 10.
Ex:
123 user entered value.
123 / 10 = 12
12 / 10 = 1

Now the sum.

sum = sum + rem;

3 = 0 + 3
5 = 3 + 2
6 = 5 + 1

So the sum of digits in the given integer number is 6.

Video Tutorial: Find Sum of Digits In A Given Number: C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=UnXfCTtHNWw [Watch the Video In Full Screen.]



Full Free Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include < stdio.h >
#include < conio.h >
 
void main()
{
	int num, rem, sum = 0;
	clrscr();
 
	printf("Enter an integer number\n");
	scanf("%d", &num);
 
	while(num)
	{
	  rem = num % 10;
	  sum = sum + rem;
	  num = num / 10;
	}
 
	printf("\nThe sum of digits is %d", sum);
	getch();
}

Output:
Enter an integer number
123
The sum of digits is 6

Find Given Number Is Prime or Not: C

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

A natural number greater than 1 that is not a prime number is called a composite number. For example, 7 is prime, as only 1 and 7 divide it, whereas 10 is composite, since it has the divisors 2 and 5 in addition to 1 and 10.

In this video tutorial we show you 3 methods of finding whether the user entered number is a prime number or not.

Since every number is divisible by 1, we start the division(in for loop) from 2.

Note: A number can not be divided(to get a whole number) by another number which is greater than itself.

First Method:
Since every number is divisible by 1, we start the division(in for loop) from 2 till one number less than the user entered value.
Example: If user entered 10. For loop starts from 2 to 9.
for(i=2; i<10; i++) or for(i=2; i< =9; i++) But the draw back: If user enters 500, the loop must get executed from 2 till 499. i.e., 497 times. Second Method:
Here we reduce the number of iterations in for loop.
i.e., we divide the user entered value by 2 and divide the user entered value from 2 till num / 2;
Example: If user entered 500, we start the division from 2 till 500/2 i.e., till 250 times.

Third Method:
Here we still reduce the number of iterations in the for loop.
i.e., we take the square root of the user entered number and divide the user entered number from 2 till square root of number.
Example: If user entered 500, we start the division from 2 till sqrt(500) i.e., till 22 times.

So this third method is most optimum, as it involves least number of looping.

Video Tutorial: Find Given Number Is Prime or Not: C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=3MYx9PrpXEg [Watch the Video In Full Screen.]



Full Free Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
#include< stdio .h>
#include< conio .h>
#include< math .h>
 
void main()
{
	int num, i, fact, inum;
	clrscr();
 
	printf("Enter a number\n");
	scanf("%d", &num);
 
	inum = sqrt(num);
 
	for(i=2; i<inum ; i++)
	 if( num % i == 0 )
	 {
	   fact = 0;
	   break;
	 }
	 else
	   fact = 1;
 
	if(fact)
	 printf("%d is Prime\n", num);
	else
	 printf("%d is NOT prime\n", num);
 
	getch();
}

Table of all prime numbers up to 1,000:


Colorful Text Output: C program

This video tutorial shows a simple method to get the output in different colors, on the dosprompt: using a simple C program.

Video Tutorial: Colorful Text Output: C program


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=TpasN_6RMMM [Watch the Video In Full Screen.]



Full Source code: Colorful Text Output: C program C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include< stdio.h> /* Leave No space b/w < and stdio.h */
 
void main()
{
	int i;
	clrscr();
 
	for( i=0; i<=15; i++ )
	{
		textcolor(i);
		cprintf("Microsoft, Google, Yahoo, Oracle, eBay, PayPal \r\n");
	}
 
	getch();
 
}

textcolor() function takes color code as it’s parameter, and this color is taken by cprintf() function and the parameter(string) passed to cprintf is printed in the color given by textcolor() function.

Color Name
———-
Color code
———-
BLACK
BLUE
GREEN
CYAN
RED
MAGENTA
BROWN
LIGHTGRAY
DARKGRAY
LIGHTBLUE
LIGHTGREEN
LIGHTCYAN
LIGHTRED
LIGHTMAGENTA
YELLOW
WHITE
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

OutPut:
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal

Write Data To Text File In C: Character by Character

This video tutorial demonstrates the creation of a simple file and writing data to text file ‘character by character’ using a simple C program.

  #include< stdio.h>

is the only header file required to include.

DOSBOX
I’m using a software called DOSBOX to write my c program. Using this software we can run 32-bit programs on our 62-bit machine.
I’m mounting my C:/DOSFOLDER here. So C:/DOSFOLDER will be treated as my C drive.

Here is the DOS code to do that:

z:\> mount c "c:/DOSFOLDER"
z:\>c:
c:\>cd TC
c\TC:>tc

We take a pointer variable of FILE type, so that this variable can be used to further reference to the file.

FILE *p;
char ch;

ch is a char type variable to store user entered characters and to transfer it to the file.

  p = fopen("c:\\Hello.txt","w");

fopen function takes two parameters. First one being the path of the file(if the file doesn’t exist, it creates one by itself), second parameter indicates the write mode. i.e., the file is opened in write mode and is intended for the write operation.

Note: If you are using DOSBOX software, then the file will be created at this location c:/DOSFOLDER/Hello.txt

       clrscr();
 
        printf("\nEnter the text nd hit Enter to Terminate\n");

clrscr() clears the screen and the print statement asks the user to enter some text and hit enter to terminate.

while( (ch=getche()) !='\r' )
	fputc( ch,p);

getche() is a function that gets a character from the keyboard, echoes to screen.
These characters are stored in char variable ch. If the entered character is not “Enter Key” that is \r charriage return character, then the control enters the while loop and using fputc function it transfers the content(character) present in ch to p(variable which references the file we have opened for writing).

fclose(p);

closes the file, opened for write operation.

Video Tutorial: Creating and Writing Data To A Simple File: in C


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=4V1qAw-XImI [Watch the Video In Full Screen.]



Full Source code to Creating and Writing Data To A Simple File: in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include< stdio.h> /* no space b/w < and stdio.h */
 
void main()
{
	FILE *p;
	char ch;
 
	p = fopen("c:\Hello.txt","w");
	clrscr();
 
	printf("\nEnter the text nd hit Enter to Terminate\n");
 
	while( (ch = getche()) != '\r' )
	 fputc( ch, p );
 
	fclose(p);
}

Output:
Recruitment’s in C.