C Programs Part-II

by uptu
0 comment

Introducing you some important programs of C language with coding . These programs  are  provided by Mr. Anuj Khanna (Asst. Professor),Krishna Institute of Technology,Kanpur

Topics

Series & Pattern generation using loops

Modular Programming & Functions/ Programs on Recursive Functions, POINTERS,    ARRAY

 

_____________________________________________________________________

banner

Series & Pattern generation using loops

19. Program to calculate the sum of the series up to first 100 terms: 14 + 34 + 54 + 74 + … + n terms.

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main ()

{ int n , i , sum = 0 ;

clrscr();

printf(“ENTER THE NUMBER OF TERMS\n”) ;

scanf(“%d” , &n) ;

for (i=1 ; i<=n ; i++)

{

sum = sum + pow (i, 4) ;

}

printf(“Sum of the series = %d” , sum) ;

getch() ;

}

 

20. Program to generate the sum of the following series:- sum = x + x2 / 2! + x4 / 4! + …… + x n / n!

#include<stdio.h>

#include<conio.h>

#include<math.h>

 

void main ()

{

int , x , n , , j ,fact =0,

float sum ;

clrscr();

printf(Enter the no. whose series is to be found: ”) ;

scanf(“%d”, &x) ;

printf(“Enter the no. of terms up to which series is to be generated”) ;

scanf(“%d”, &n) ;

sum = x ;

for (i=1 ; i<=n ; i++)

{

if (i%2 = = 0)

{

fact =1;

for (j=1 ; j<=i ; j++)

{ fact = fact * j ; }

Sum = sum + pow (x ,i) / fact ;

} // End of IF BLOCK

} // End of outer for loop

printf (“Sum of the series = %f”, sum) ;

getch() ;

} // End of main ()

 

21 .Printing the pattern of program

#include<stdio.h>

#include<conio.h>

void main ()

{ int row, col;

clrscr();

for (row=1;row<=5; row++)

{ for (col=1 ; col <=row; col ++)

printf( “ * “);

printf( “\n”); } getch(); }

 

Output:

 

*

* *

* * *

* * * *

* * * * *

 

22. Program to print the following pattern : 5 4 3 2 1

4 3 2 1

3 2 1

2 1

1

void main()

{ int i , j ;

clrscr () ;

for ( i = 5 ; i > = 1 ; i — )

{ for (j = 1 ; j < = 1 ; j –)

{ printf (“%d” , j ) ; }

printf (“\n” ) ;

}

getch(); }

 

23. Program to print the following pattern (FLOYD’S TRAINGLE) :

1

2 3

4 5 6

7 8 9 10

void main()

{

int n , r , val =1, j ;

clrscr () ;

printf ( “Enter the number of rows in the Floyd’s triangle\t : ” )

scanf (“%d” , &n ) ;

for ( r = 1 ; r <= n ; r ++ ) // print n rows

{

for (j = 1 ; j <=r ; j ++)

{ printf (“%d” , val ) ;

val ++ ; }

printf (“\n” ) ; } getch(); }

 

Modular Programming & Functions/ Programs on Recursive Functions

 

24. WAP in C for finding the sum of three no. and their average using user defined functions sum() and avg().

#include<stdio.h>

#include<conio.h>

int sum (int , int , int ) ;

float avg (int , int , int ) ; // Prototype or declaration of function


void main()

{

int a , b , c, sum=0 ;


float av =o ;

s= sum (a , b , c); // function call


av = avg (a , b , c) ;

scanf (“%d%d%d”, &a, &b, &c) ; // actual parameters

printf (“Sum= %d\t Average = %f ” , s , av) ;

getch() ;

} // End of main ()

 

// Definition of user defined function sum ()

int sum (int x , int y , int z ) // x , y and z are formal parameters

{ return (x + y + z) ; }

// Definition of user defined function avg ()

float avg (int x , int y , int z )

{

float m ;

m = (float) (x + y + z ) / 3;

return m ;

}

 

25. Program to find the ratio using user defined function ratio()

#include<stdio.h>

#include<conio.h>

float ratio(int , int , int ) ; // Prototype or declaration of function


void main()

{

int a , b , c;

float ratio(); // function call

scanf (“%d%d%d”, &a, &b, &c); // actual parameters

printf (“%f\n”, ratio(a, b, c) );

}

 


float ratio(x,y,z) // x , y and z are formal parameters in function definition

int x, y, z;

{

if(difference(y,z))

return(x/y-z));

else

return(0,0);

}

difference(p, q)

{

int p,q;

{

if(p!=q)

return(1);

else

return(0);

}


26. Program to compute factorial of a number using recursive function.

#include<stdio.h>

#include<conio.h>

          long  int  rec_fact(long int) ;    
          void main ()  
         {
            int num , fact ;
            clrscr() ;
            printf(“Enter the number”) ;
            scanf (“%ld”, &num) ;
           fact = rec_fact (num) ;
           printf(“Factorial of %ld = %ld”, num , fact) ;
          getch() ;
        }
 
          long  int  rec_fact(long  int  x) ;                   // recursive definition of factorial
          {   int f ; 
               if(x = = 1 )
                   return 1;                                             // base case
               else 
                    {f =  x * rec_fact(x-1) ;                   // Recursive case
                     return (f) ; 
                    }       

 

27. Program to generate the Fibonacci series using recursive function

#include<stdio.h>

#include<conio.h>

         int  fibo( int) ;    
          void main ()  
         {
            int  n  , f ;
            clrscr() ;
            printf(“Enter the number upto which series is to be generated \n”) ;
            scanf (“%d”, &n) ;
            printf(“Fibonacci series up to %d terms is : \n \n  ” , n) ;
             for (int i=1 ; i< = n ; i++) ;
              {
                  f = fibo (i) ;

}

 

       int fibo (int  n)                                 // Recursive definition            
     {                                             
        if (n = = 1)
          return 0 ;                                      // Base case
       if ( n = = 2)
           return 1 ;
      return ( fibo(n - 1) + fibo(n - 2) ) ;   // At a time two recursive function called so binary
    }

 

 

28. Write a C program to read two integers and to swap their values. Use a user-defined function for swapping.

#include <stdio.h>

#include <conio.h>

void main()

{

float M , N;

 

void swap(float *ptr1, float *ptr2 ); /* Function Declaration or prototype*/

printf(“Enter the values of M and N\n”);

scanf(“%f %f”, &M, &N);

printf (“Before Swapping: M = %f\t N = %f\n”, M,N);

// Function call

swap( &M , &N) ;

printf (“After Swapping = % \t N = %f\n”, M , N) ;

 

} // End of main()

 

// Definition of Function swap() – to interchange the contents of two items

void swap(float *ptr1, float *ptr2 )

{

float temp;

temp=*ptr1;

*ptr1=*ptr2;

*ptr2=temp;

 

} // End of Function

 

 

/* —————————————-

Output

Enter the values of M and N

32 29

Before Swapping = 32.00 N = 29.00

After Swapping = 29.00 N = 32.00

——————————————*/

 

POINTERS

29. Write a program in C to illustrate the use of pointers in arithmetic expressions.

#include<stdio.h>

#include <conio.h>

void main()

{

int a , b , *p1 , *p2 , x , y ,z ;

a= 14 ; b = 7 ;

p1 = &a ; p2 = &b ;

x = *p1 * *p2 – 6 ;

y = 4 * – *p2 / *p1 + 10 ;

printf (“Address of a = % u \n”, p1 ) ;

printf (“Address of b = % u \n” , p2 ) ;

printf (“ ********************************* ” ) ;

printf ( “a = % d , b = %d \n”, a , b) ;

printf ( “x = % d , y = %d \n”, x , y) ;

 

*p2 = *p2 + 3 ;

*p1 = *p2 – 5 ;

z = *p1 * *p2 – 6 ;

printf (“\n a = %d \t a = %d\t a=%d \n” , a , *(&a) , *p1 ) ;

printf (“\n b = %d \t b = %d \t b = %d \n” , b , *(&b) , *p2 ) ;

printf(“ z = %d \n ” , z ) ;

getch() ;

}

 

30. Write a program in C to show the use of array of pointers.

#include<stdio.h>

#include<conio.h>

void main ()

{

int a = 10 , b = 20 , c = 30 ;

int* arr [3] = { &a , &b , &c} ;

clrscr() ;

printf(“The values of variables are:\n”) ;

printf (“%d %d %d\n”, a , b , c) ;

printf(“(“%d %d %d\n”, *arr [0], * arr[1] , *arr [2] ) ;

getch() ;

}

31. Write a program using pointers to compute the sum of all elements stored in a 1-D array.

#include<stdio.h>

#include<conio.h>

void main()

{ int arr [6] , sum = 0 , i = 0 ;

int *p ;

p = arr ; // pointer initialized with base address of array

clrscr() ;

 

printf (“Enter the elements of array: \n”) ;

for (i=0 ; i < = 5 ; i++)

{

scanf (“%d” , &arr [i] ) ;

}

printf (“Elements of array are\n”) ;

for (i=0 ; i < = 5 ; i++)

{

printf (“%d \t” , arr [i] ) ;

}

printf ( “Elements Value Address\n\n ” ) ;

for (i=0 ; i < = 5 ; i++)

{

printf (“arr [%d] %d %u \n” , i , *p , p) ;

sum = sum + *p ;

i++ ; p ++ ; }

printf (“\n Sum = %d \n” , sum ) ;

printf (“\n &arr [0] = % u\n”, &arr [0] ) ;

printf (“\n p = %u \n” , p) ;

getch () ;

}

Arrays

 32.Write a C program to accept a matrix of order m x n and find its transpose .

#include <stdio.h>

#include <conio.h>

void main ()

{

int mat [10][10];

int i, j, m, n ;

printf (“Enter the order of the matrix \n”);

scanf (“%d %d”, &m ,&n);

printf (“Enter the elements of the matrix\n”);

for (i=0; i<m;++i)

{

for (j=0;j<n;++j)

{

scanf (“%d”,& mat[i][j]);

}

}

printf (“The given matrix is \n”);

for (i=0;i<m;++i) // Here outer loop is now for counter variable i

{

for (j=0;j<n;++j)

{

printf (” %d”, mat[i][j]);

}

printf (“\n”);

}

 

printf (“Transpose of matrix is \n”);

for (j=0; j<n; ++j) // Here outer loop is now for counter variable j

{

for (i=0 ;i<m ;++i)

{

printf (” %d”,mat[i][j]);

}

printf (“\n”);

}

} /* End of main() */

 

————————————————————-

Output

Enter the order of the matrix

2 2

Enter the coefiicients of the matrix

3 -1

6 0

The given matrix is

3 -1

6 0

Transpose of matrix is

3 6

-1 0

 

 

33. Write a C program to accept N numbers and arrange them in an ascending order(sorting) .

 

#include <stdio.h>

#include <conio.h>

void main ()

{

int i,j, a, n, number[30];

printf (“Enter the value of N\n”);

scanf (“%d”, &n);

printf (“Enter the numbers \n”);

for (i=0; i<n; ++i)

scanf (“%d”,& number[i]) ;

for (i=0; i<n ; ++i)

{ for (j=i+1; j<n; ++j)

if (number[i] > number[j])

{

a= number[i];

number[i] = number[j];

number[j] = a;

}

}

printf (“The numbers arranged in ascending order are given below\n”);

for (i=0; i<n; ++i)

printf (“%d”,number[i]);

} /* End of main() */

 

/*——————————————————————

Output

Enter the value of N

5

Enter the numbers 80 20 67 10 45

The numbers arranged in ascending order are given below

10 20 45 67 80

——————————————————————*/

 

34. Write a C program to accept a matrix of order M x N and find the sum of each row and each column

#include <stdio.h>

#include<conio.h>

void main ()

{

int m1[10][10];

int i, j, m, n, sum=0;

clrscr() ;

printf (“Enter the order of the matrix\n”);

scanf (“%d %d”, &m, &n);

printf (“Enter the co-efficients of the matrix\n”);

for (i=0; i<m ; ++i)

{ for (j=0 ; j<n ; ++j)

{ scanf (“%d”, &m1[i][j]) ; }

}

for (i=0;i<m;++i)

{ for (j=0;j<n;++j)

{ sum = sum + m1[i][j] ; }

printf (“Sum of the %d row is = %d\n”, i, sum) ;

sum = 0; }

 

for (j=0; j<n ;++j )

{ for (i=0 ; i< m ; ++i)

{ sum = sum+m1[i][j] ; }

printf (“Sum of the %d column is = %d\n”, j, sum);

 

} getch () ;

} /*End of main() */

/*—————————————————

Output

Enter the order of the matrix

3 3.Enter the co-efficients of the matrix

1 2 3

4 5 6

7 8 9

Sum of the 0 row is = 6

Sum of the 1 row is = 15

Sum of the 2 row is = 24

Sum of the 0 column is = 12

Sum of the 1 column is = 15

Sum of the 2 column is = 18

 

————————————————–

 

You may also like

Leave a Comment

Unified Platform for Technical Universities (UPTU) Notes is a one-stop destination of all academic need. its a Global community of scholars to facilitate the sharing of knowledge and resources and sparking discourses on the various bodies of knowledge.

Edtior's Picks

Latest Articles

@2022 – All Right Reserved. Designed and Developed by Softweblink.

JEE Advanced 2022: Provisional answer keys out