## Write a C program to Sort the list of integers using Bubble Sort

Posted by Tushar Bedekar

#include(stdio.h)                                 //note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void bubble(int [ ], int);                                           // func. declaration
void main( )
{
int a[10],n,i;
clrscr();
printf("Enter the no. of elements u want to fill:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n; i++)
scanf("%d",&a[i]);

bubble(a,n);                                                             // calling function
getch();
}

void bubble(int b[], int no)                                            // called function
{
int i,j,temp;
for(i=0; i< no-1; i++)
{
for(j=0; j< no-i; j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
printf("\nAfter sorting : ");
for(i=0; i< no; i++)
printf("%5d",b[i] );
}

Output:- Enter the no. of elements u want to fill: 5
Enter the array elements : 8  1  90  5  4
After sorting : 1  4  5  8  90

## Write a C program to print the Given pyramid

Posted by Tushar Bedekar

1

1 2
1 2 3
1 2 3 4
1 2 3 4 5
.
.
.
n

Solution:-#include(stdio.h)                        // note place '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
int r,c,n,sp;
clrscr( );
printf("Enter the no. of rows u want to print:");
scanf("%d", &n);
for(r=1; r<=n; r++)

for(sp=n; sp>=r; sp--)
printf(" ");                                                 // give one space in betw quotations.
for(c=1; c<=r; c++)
printf("%2d", c);
printf("\n");
}
getch( );
return 0;
}

## Write a C program to find GCD of each of the two consecutive elements of an array. write a function GCD(a,b) that would take two consecutive elements of the array as arguments and return the GCD

Posted by Tushar Bedekar

#include(stdio.h)                                  // Note place '<' & '>' in place of '(' & ')'
int gcd(int,int);
void main()
{
int a[10],n,i,b[10];
int x,y;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n ;i++)
scanf("%d",&a[i]);
for(i=0; i< n-1 ;i++)
{
x=a[i];
y=a[i+1];
b[i]=gcd(x,y);
printf("\nG.C.D of %d and %d is : %d",x,y,b[i]);
}
getch();
}
int gcd(int a,int b)
{
int rem;
for(;;)
{
rem=a%b;
if(rem==0)
break;
a=b;
b=rem;
}
return b;
}

## Write a C program to get the maximum and minimum values of a Data type

Posted by Tushar Bedekar

#include(stdio.h)                                     // note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void main( )
{
int x, y;
clrscr( );
x=1;
while( x > 0)
{
y = x;
x + + ;
}
printf("\nMaximum value : %d ", y );
printf("\nMinimum value : %d", x);
getch( );
}

Output:- Maximum value : 32767
Minimum value : -32768

Similarly we can find the below datatypes,
long int ( %ld )
char ( %d )
unsigned ( %u )
.....

## Write a C function that defines two strings as parameters, as follows, and checks whether the second string (str2) is a substring of the first one (str1).

Posted by Tushar Bedekar

int substring_finder(char* str1, char* str2);
The function should return an integer as follows:
• 2 if str2 is a substring of str1 and also of same length
o e.g. str1 = "Hello" and str2= "Hello"
• 1 if str2 is a substring of str1 and of different lengths
o e.g. str1 = "Hello World" and str2= "Hello"
• 0 if str2 is not a substring of str1
o e.g. str1 = "Hello" and str2= "Helo"

program me starts from here:-

#include(string.h)                                         //  note place '<' & '>' in place of '(' & ')'
#include(stdio.h)
int substring_finder(char *,char *);
int main()
{
char *str1,*str2;
int fl;
clrscr();
printf("Enter the main string:");
gets(str1);
printf("Enter the sub-string:");
gets(str2);
fl=substring_finder(str1,str2);

if(fl==0)
printf("\n'str2' is not a substring of 'str1'");
else if(fl==1)
printf("\n'str2' is a substring of 'str1'");
else if(fl==2)
printf("\nBoth are equal");

getch();
return 0;
}
int substring_finder(char *str,char *sub)
{
int flag;
if(strcmp(str,sub)==0)
flag=2;
else if(strstr(str,sub))
flag=1;
else
flag=0;

return flag;
}

## Write a C program that prints a given positive integer in reverse order and also sum of the individual digits involved in the given integer.

Posted by Tushar Bedekar

#include< stdio.h>
#include< conio.h>

int main( )
{
int no, rev=0, rem, sum=0;
clrscr( );
printf("Enter the required integer:");
scanf("%d", &no);
while(no>0)
{
rem = no%10;
rev = (10*rev)+rem;
sum = sum+rem;
no=no/10;
}
printf("\nReverse Number : %d", rev);
printf("\nSum of the individual digits : %d", sum);
getch( );

return 0;
}

## Write C programs that use both recursive and non-recursive functions to find the factorial of a given integer.

Posted by Tushar Bedekar

(a).without using recursive function.

#include(stdio.h)                              // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int);                                        // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}
int fact(int n)
{
int ft,
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}

Output:- 4 Factorial is : 24

(b).using recursive function.

#include(stdio.h)                           // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int);                                      // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}
int fact(int n)
{
int ft,
if( n==1)
return 1;
else
ft= n*fact (n-1);
return ft;
}

## Write a C program to find the roots of a quadratic equation using Pointers and Functions.

Posted by Tushar Bedekar

#include(stdio.h)                    // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
void quadratic_roots(double *,double *, double *);
int main(void)
{
double a, b, c;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
getch( );
return 0;
}

void quadratic_roots(double *aa, double *bb, double *cc)
{
double root1, root2;
if((*bb)*(*bb)= = 4*(*aa)*(*cc))
{
root1= -(*bb)/2*(*aa);
printf("\nRoots are equal");
printf("\nvalue : %lf", root1);

else if((*bb)*(*bb) > 4*(*aa)*(*cc))
{
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
{
printf("\nImaginary roots");
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("roots are %f+i(%f) , %f-i(%f)",r1, r2, r1, r2);
}
}

## Design of Pyramids IN C Programming

Posted by Tushar Bedekar

A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

solution:-#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );

for( r=7; r>=1; r-- )
{
for(c=1; c<=r; c++ )
for(c=r; c>=1; c-- )

printf("\n");
}
getch();
return 0;
}

Output:-
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

Note:-
'r' and 'c' means rows and columns .
'askey' variable is for disp. values.