C Program-Insertion Short Using Insertion Function

C Program for Insertion Shorting Using Insertion Function

void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)

{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}//end for 1.
}//end function.
void main()
{
int a[100],n,i;
clrscr();
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);

for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}

bubble(a,n);

printf("\n\n Finally sorted array is: ");
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);

} //end program.

/*

--------SAMPLE OUTPUT----------------------


Enter integer value for total no.s of elements to be sorted: 6


Enter integer value for element no.1 : 89


Enter integer value for element no.2 : -4


Enter integer value for element no.3 : -67


Enter integer value for element no.4 : 5


Enter integer value for element no.5 : 78


Enter integer value for element no.6 : 11


Finally sorted array is: -67 -4 5 11 78 89

------------------------------------------

*/

C Program For Bubble Sort ,Bubble Sorting Using C

C Program for Bubble Sort ,Bubble Sorting Using C Language

void bubble(int a[],int n)
  {
        int i,j,t;
         for(i=n-2;i>=0;i--)
         {
            for(j=0;j<=i;j++)

                  {
                    if(a[j]>a[j+1])
                                    {
                                      t=a[j];
                                     a[j]=a[j+1];
                                     a[j+1]=t;
                                    }
                   }
      

           }//end for 1.

  }//end function.


  void main()
  {

      int a[100],n,i;

      clrscr();

      printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
      scanf("%d",&n);

      for( i=0;i<=n-1;i++)
            { printf("\n\n Enter integer value for element no.%d : ",i+1);
              scanf("%d",&a[i]);
            }

       bubble(a,n);

       printf("\n\n Finally sorted array is: ");
       for( i=0;i<=n-1;i++)
       printf("%3d",a[i]);

  } //end program.

/*

--------SAMPLE OUTPUT----------------------


Enter integer value for total no.s of elements to be sorted: 6


Enter integer value for element no.1 : 89


Enter integer value for element no.2 : -4


Enter integer value for element no.3 : -67


Enter integer value for element no.4 : 5


Enter integer value for element no.5 : 78


Enter integer value for element no.6 : 11


Finally sorted array is: -67 -4 5 11 78 89

------------------------------------------

*/

C Program Sum of digits of a Five Digit Number

C Program Sum of digits of a Five Digit Number.If a five-digit number is input through the keyboard, write a c program to
calculate the sum of its digits.


void main ()
{
int number, last_digit, next_digit, total;
printf ("Enter the number whose sum of digits is to be calculated: ");
scanf ("%d", &number); 
last_digit = number%10;
total = last_digit; 
next_digit = (number/10) % 10; 
total = total + next_digit; 
next_digit = (number/100) % 10; 
total = total + next_digit;
next_digit = (number/1000) %10; 
total = total + next_digit; 
next_digit = (number/10000) %10; 
total = total + next_digit; 
printf ("The sum of the digits of the entered number is: %d", total); 
}

Logic :  Use the Modulus Operator '%"  Using  the normal division operator  " / " returns the quotient.
Using "% " the modulus Operator returns the Remainder. 


C Program For Detection of Leap year using Functions.

C Program For Detection of Leap year using Functions.
Any year is entered through the keyboard.

Write a function to determine whether the year is a leap year or not.

#include
main()
{

int leap_year(year);
int year, lp;

printf("Enter the year:");
scanf ("%d", &year);

lp=leap_year(year);

if (lp)

{
printf("\nThe entered year is a leap year.");
}
else
{

printf("\nThe entered year is not a leap year.");
}

}


leap_year(int y)

{
int lp;

if (y%4==0)

{
lp=1;
}
else
lp=0;

return(lp);
}

C Program For Prime Factors using Functions.

C Program For Prime Factors using Functions.
A positive integer is entered through the keyboard.

Write a function to obtain the prime factors of this number.
For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.

#include
main()
{

int number;
int prime(int number);

int primefactor(int number);


printf("Enter the number whose prime factors are to be calculated:");

scanf ("%d", &number);

primefactor(number);

}

//The following function detects a Prime number.

prime(int num)
{
int i, ifprime;


for (i=2; i<=num-1; i++)

{
if (num%i==0)
{

ifprime=0;
}
else
ifprime=1;

}
return (ifprime);
}


//The following function prints the prime factors of a number.

primefactor(int num)
{
int factor,ifprime;
for (factor=2; factor<=num;)

{
prime(factor); //so that the factors are only prime and nothing else.
if (ifprime)

{
if (num%factor==0) //diving by all the prime numbers less than the number itself.
{

printf("%d ", factor);
num=num/factor;

continue;
}
else
{
factor++;//this cannot be made a part of the for loop

}
}
}
return 0;
}

C Program - Calculation of Area and Circumference of a Circle using Pointers

C Program for Calculation of Area and Circumference of a Circle using Pointers.
Write a function that calculates both Area and Perimeter/ Circumference of the Circle, whose Radius is entered through the keyboard.

#include
main()
{

int radius;
float area, perimeter;

printf("\nEnter radius of a circle:");

scanf ("%d", &radius);
areaperi (radius, &area, &perimeter);

printf("Area=%f", area);
printf("\nPerimeter=%f", perimeter);

}


areaperi(int r, float *a, float *p)

{
*a=3.14*r*r;
*p=2*3.14*r;

}

Calculation of Product of Two Numbers using Function

C Program for Calculation of Product of Two Numbers using Function.
Write a function which receives a float and an int from main(), finds the product

of these two and returns the product which is printed through main().

#include
main()
{

int i;
float j, prod;
float product (int x, float y);

printf("Enter the i(int) and j(float):");
scanf ("%d %f", &i, &j);

prod = product(i,j);

printf("Product:%f", prod);

}


product (int x, float y)
{

float product;
product = x*y;
return (product);

}

C-Program Of Sum, Average and Standard Deviation.

C-Program of Calculation of Sum, Average and Standard Deviation using Functions and Pointers.
Write a function that receives 5 integers and returns the sum, average and standard
deviation of these numbers. Call this function from main() and print the results in main().


#include
#include

int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);

int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;

float sd=0.0;

printf("Enter Five Numbers:");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);

calc (a, b, c, d, e, &sum, &avg, &sd);


printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);

printf("\nStandard Deviation=%f\n", sd);


getchar();

return 0;
}

calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)

{
float Calc=0.0;

*sum = a+b+c+d+e;

*avg = *sum / 5.0;

Calc += ( a - *avg) * ( a - *avg);

Calc += ( b - *avg) * ( b - *avg);

Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);

Calc += ( e - *avg) * ( e - *avg);


*sd = sqrt((double)Calc/5.0);

}

Calculation of Area and Circumference of a Circle(By using Pointers)

Write a function that calculates both Area and Perimeter/ Circumference of the Circle, whose Radius is entered through the keyboard.
(Program shows the use of Call By Reference.)

#include
#include
void main()
{

int radius;
float area, perimeter;

printf("\nEnter radius of a circle:");

scanf ("%d", &radius);
areaperi (radius, &area, &perimeter);

printf("Area=%f", area);
printf("\nPerimeter=%f", perimeter);

getch();

}


areaperi(int r, float *a, float *p)

{
*a=3.14*r*r;
*p=2*3.14*r;

}


Fibonacci Sequence using Recursion.

Write a recursive function to obtain the first 25 numbers of a Fibonacci
sequence. In a Fibonacci sequence the sum of two successive terms gives the
third term. Following are the first few terms of the Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55 89 ...


#include
main()
{

static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);


}

fibonacci (int prev_number, int number)

{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;


if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number);//recursion

}
getch();
}

Calculate Factorial of a number using Recursion (C-Program)

Write a program to calculate the factorial of a number.
(Recursion method )

#include
#include
main
()
{
int a, fact;

printf
("\nEnter any number: ");

scanf
("%d", &a);

fact
=rec (a);

printf("\nFactorial Value = %d", fact);

}

rec (int x)

{

int f;

if (x==1)

return (1);

else

f=x*rec(x-1);

return (f);

getch();

}