Basic C Programs

 

 

 

 

 

 

There are many snippets of basic c programs available on the internet, but none of them had a explanation on it.So I will try to illustrate the most common basic c programs and Explain them in Plain language.

1.Largest of three numbers

  • The first one on the list is largest of three numbers using Conditional Operator.

  • A conditional Operator is also called as a Ternary Operator.

  • The syntax of conditional operator is that after the condition it has a true part and an false part

  • If the condition is satisfied the statement after the question mark is executed else the other one.

  • Here in this program we declare 4 variables a,b,c and max.

  • At first we check if a is greater than b if true check for a>c then we assign a as the maximum number to the max variable if first one holds good and second one fails then c is assigned to max

  • If a is smaller then b is checked if it is greater than c then it is assigned to max

  • The Snippet is given below:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,max;
clrscr();
printf("Enter the value of A,B and C:");
scanf("%d%d%d",&a,&b,&c);
max=(a>b?(a>c?a:c):(b>c?b:c)));
getch();
}	

2.Fibonacci Series

  • In Mathematics Fibonacci sequence are the integers with the sequence 0,1,1,2,3,5,8,13,21,34….

  • It follows the sequence in which the subsequent numbers are the sum of previous two.

  • Fn=Fn-1+Fn-2 where f0=0 and f1=1.

  • In a C program the most commonly used input output functions are printf and scanf which is avialable at the stdio.h header file.

  • The clrscr() and getch() are available at conio.h header file.

  • At first we assign f1=-1 and f2=1 so that initially we start with a 0.

  • In the for loop from 1 to n we add f1+f2 and store it to f3 and print f3 each time.

  • Then we interchange f2 to f1 and f3 to f2 so that the sequence continues with the new set of numbers.

  • The Snippet is given below:

#include<stdio.h>
#include<conio.h>
main()
{
int  i,f1,f2,n;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
f1=-1;f2=1;
for(i=1;i<=n;i++)
{
f3=f1+f2;
printf("%d",f3);
f1=f2;
f2=f3;
}
getch();
}	

3.Basic Sorting(Bubble sort)

  • The basic Sorting technique in order to sort a set of integers is the bubble sort.

  • The main concept of bubble sort is compare each pair of adjacent terms and swap them if they are not in order.

  • At the final iteration no swap is needed because the list is sorted.

  • Bubble sort animation

 

 

 

 

  • The Snippet is given below :
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[50],i,j,n,t;
    printf("Enter the value of N:");
    scanf("%d",&n);
    printf("\ Enter the values:");
    for(i=1;i<=n;i++)
    	scanf("%d",&a[i]);
    for(i=1;i<=n-1;i++)  //n-1 passes
    {
    	for(j=0;j<=n-1;j++) //loop to compare successive elements
    	{
    		if(a[j]>a[j+1]) // Swapping if necessary
    		{
    		t=a[j];
    		a[j]=a[j+1];
    		a[j+1]=t;
    		}
    	}
    }
    for(i=1;i<=n;i++)
    printf("%d",a[i]);
    getch();
    }
    
    		

Script Download:

 

  • Click below to download Script

 

 

Thank u for reading this Article hope it was useful !

-Regards

Sathesh Bm




About Sathesh Bm

Sathesh Bm has written 26 post in this website.

Engineer, Web Lover, Thinker, Blogger

Related Posts Plugin for WordPress, Blogger...