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;
}
Read More

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);
quadratic_roots(&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);
}
}
Read More

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( )
{
  int r,c,askey;
  clrscr( );
  
  for( r=7; r>=1; r-- )
  {
    askey=65;
    for(c=1; c<=r; c++ )
      printf("%2c", askey++ );
    askey--; 
    for(c=r; c>=1; c-- )
       printf("%2c", askey--);
   
   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.
Read More

Write a C program to delete an element from the array

Posted by Tushar Bedekar

#include< stdio.h>
#include< conio.h>
int main( )
{
int a[20], len, i, n, item, j;
clrscr( );
printf("Enter how many elements you want to enter:");
scanf("%d", &len);

printf("\nEnter the array elements:");
for(i=0; i<'len' ; i++)                                 // remove the single quotes in for loop
scanf("%d", &a[ i]);

printf("\nEnter the location, where you want to delete:");
scanf("%d", &n);

item= a[n-1];
printf("\nDeleted value is : %d", item);

for( j= n-1; j<'len-1; j++) 

{ a[ j]= a[ j+1]; }

len=len-1;
printf("\nThe new element list :");
for(i=0; i<'len; i++)

printf("%5d", a[i]);

getch( );
return 0;
}

Output:-
Enter how many elements you want to enter: 4
Enter the array elements:
10
20
30
40
Enter the location, where you want to delete: 3
Deleted value : 30
The new element list: 10 20 40
Read More

Write a C program to display the message "Welcome to C" without a Semicolon

Posted by Tushar Bedekar

// C program without a Semicolon.
This can done in three ways but one of them is infinite.(ie, while loop)
Solution:1
#include(stdio.h) // note place '<' & '>' in place of '(' & ')'
void main( )
{
if(printf("Welcome to C"))
{
}
}

Solution:2
void main( )
{
switch(printf("Welcome to C"))
{
}
}

Solution:3
void main( )
{
while(printf("Welcome to C")) //infinite loop
{
}
}
Read More

Write a C program that adds first seven terms of the following series 1/1! +2/2! + 3/3! + ..........

Posted by Tushar Bedekar


#include< stdio.h>
#include< conio.h>
int main( )
{
int n,i,j,fact;
float sum=0;
clrscr();
for(i=1; i<=7; i++) { fact=1; for(j=i; j>=1; j--)     // to find the factorial
fact=fact*j;

sum=sum+(float)i/fact;
}
printf("\nSum : %f",sum);
getch();
return 0;
}
Read More

Write a C program to find the greatest common divisor of the two given positive integers

Posted by Tushar Bedekar



#include< stido.h>
#include< conio.h>
int main( )
{
int a, b, res;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);

for(  ;  ;  )                                                  //This is empty for loop.
{
res = a%b;
if( res = = 0) break;
a = b;
b = res;
}
printf("\n The GCF is : %d", res);
getch( );
return 0;
}
Read More

HEAP SORTING IN C

Posted by Tushar Bedekar



Q. Write a C program to sort 5 numbers using heap sorting method.


Ans.


/* c program for heap sorting method*/

#include<stdio.h>
#include<conio.h>
void manage(int *, int);
void heapsort(int *, int, int);
int main()
{
 int arr[20]; 
 int i,j,size,tmp,k;
 printf("\n\t------- Heap sorting method -------\n\n");
 printf("Enter the number of elements to sort : ");
 scanf("%d",&size);
 for(i=1; i<=size; i++)
 {
   printf("Enter %d element : ",i);
   scanf("%d",&arr[i]);
   manage(arr,i);
 }
 j=size;
 for(i=1; i<=j; i++)
 {
   tmp=arr[1];
   arr[1]=arr[size];
   arr[size]=tmp;
   size--;
   heapsort(arr,1,size);
 }
 printf("\n\t------- Heap sorted elements -------\n\n");
 size=j;
 for(i=1; i<=size; i++)
     printf("%d ",arr[i]);
 getch();
 return 0;
}


void manage(int *arr, int i)
{
 int tmp; 
 tmp=arr[i];
 while((i>1)&&(arr[i/2]<tmp))
 {
   arr[i]=arr[i/2];
   i=i/2;
 }
 arr[i]=tmp;
}


void heapsort(int *arr, int i, int size)
{
 int tmp,j;
 tmp=arr[i];
 j=i*2;
 while(j<=size)
 {
   if((j<size)&&(arr[j]<arr[j+1]))
      j++;
   if(arr[j]<arr[j/2]) 
      break;
   arr[j/2]=arr[j];
   j=j*2;
 }
 arr[j/2]=tmp;
}

/************* OUTPUT ***************/

Screen shot for Heap sorting C program





Read More

C MAGICAL PROGRAM ME:-

Posted by Tushar Bedekar


void main()
{
int a;
a=300*300/300;
printf(“%d”,a);
}
What will be the output of the above Program

Few options which I can give are -

(1)300
(2)1
(3)81
(4)600
The Correct answer for this question is -

Option 3 i.e. 81

Confused???????

Check here



In C integer has range from -32768 to 32767.
Hence          32767 + 1 = -32768
Similarly       32767 + 3 = -32766

For the above question

300*300=90000

The value stored in integer variable ‘a’ is 24300

Now 24300/300 = 81 



Hence the answer is 81
Read More
back to top