Skip to main content

Posts

Showing posts from August, 2021

C Program to print the first N even numbers in reverse order.

 C Program to print the first N even numbers in reverse order.      In this example, we will print the first N even numbers in reverse order. Here N is the total number of even numbers you want to print. PROGRAM TO PRINT THE FIRST N EVEN NUMBERS IN REVERSE ORDER: #include <stdio.h> #include <conio.h> int   main () {      int   even , n , i ;      printf ( "Enter the value of N : " );      scanf ( " %d " ,& n );      printf ( "Printing first  %d  even numbers in reverse order : " , n );      for ( i = n * 2 ; i > 2 ; i --)     {          if ( i % 2 == 0 )         {              printf ( " %d \t " , i );         }     }      return   0 ; } OUTPUT TO PRINT THE FIRST N EVEN NATURAL NUMBERS IN REVERSE ORDER: Enter the value of N : 10 Printing first 10 even numbers in reverse order : 20    18      16      14      12      10      8       6       4     NOTE:   You Can Comment Your Code if you have solved differently and we will pin i

C program to Print the Digits of a Number in Words

 C program to Print the Digits of a Number in Words.      In this example, we will print the digits of a number in words.  For example, if a number 841 is entered through the keyboard  then the program must print “Eight Four One". For solving this first we have to reverse the number than by using switch statement we willl print the digits of the number in words.    PROGRAM TO PRINT THE DIGITS OF A NUMBER IN WORDS #include <stdio.h> #include <conio.h> void   main () {      int   n , num = 0 ;      printf ( "Enter the Number : " );      scanf ( " %d " ,& n );      while ( n != 0 )     {          num =( num * 10 )+( n % 10 );          n /= 10 ;     }      printf ( "Printing the digits of a number in words is : " );      while ( num != 0 )     {          switch ( num % 10 )         {          case   0 :              printf ( "Zero " );              break ;          case   1 :              printf ( "One " );            

C Program to find out the roots of a Quadratic Equation and also test for Real and Complex roots

 C Program to find out the roots of a Quadratic Equation  and also test for Real and Complex roots.       In this example, we will calculate the roots of the quadratic equation by testing for real and complex roots. We can test real and complex roots by checking these conditions : If b 2  −4 ac  < 0, then there are no real roots. If b 2  −4 ac  = 0, then both the roots are equal. If b 2  −4 ac  > 0, then roots are real and distinct.   PROGRAM TO FIND OUT THE ROOTS OF A QUADRATIC EQUATION. #include <stdio.h> #include <conio.h> #include <math.h> int   main () {      int   a , b , c , d ;      float   x , y ;      printf ( "Enter coefficient of x^2,x and constant : " );      scanf ( " %d   %d   %d " ,& a ,& b ,& c );      d = b * b - 4 * a * c ;      if ( d < 0 )     {          printf ( "Roots are Complex and Imaginary." );     }      if ( d == 0 )     {          printf ( "Both roots are equal" );        

C Program to find Mean and Standard deviation(SD) for a set of n numbers using array.

 C Program to find Mean and Standard deviation(SD) for a set of n numbers using array.     In this example, we will calculate the mean and standard deviation for a set of n numbers entered by the user using array.    PROGRAM TO FIND THE MEAN AND STANDARD DEVIATION(SD) FOR A SET OF N NUMBERS USING ARRAY. #include <stdio.h> #include <math.h> #include <conio.h> void   main () {      int   n , i ;      float   x [ 50 ], mean , sd , sum ;      printf ( "How many numbers you want to enter : " );      scanf ( " %d " ,& n );      printf ( "Enter the numbers " );      for ( i = 0 ; i < n ; i ++)      {          scanf ( " %f " ,& x [ i ]);     }      for ( i = 0 ; i < n ; i ++)     {          sum += x [ i ];     }      mean = sum / n ;      sum = 0 ;      for ( i = 0 ; i < n ; i ++)     {          sum = sum +( x [ i ]- mean )*( x [ i ]- mean );     }      sd = sqrt ( sum / n );      printf ( "Mean = %6.3f \n "

C Program to Calculate the Factorial of a number using Function

  C Program to Calculate the Factorial of a number using Function.     In this example, we will calculate the factorial of a number using function. Here we have used functions with Arguments and return value to perform this operation. We have declared a user-defined function fact which is called in the main function when printing the result. What is a Factorial of  a Number?      The factorial of a number is the product of all positive integers less than, equal to n. Factorial is denoted by the symbol '!'. Formula of factorial is n!=n x (n-1)! For example: 6!= 6 x 5 x 4 x 3 x 2 x1    = 720 PROGRAM TO CALCULATE THE FACTORIAL OF A NUMBER USING FUNCTION #include <stdio.h> #include <conio.h> int   fact ( int ); void   main () {      int   n ;      printf ( "Enter the number : " );      scanf ( " %d " ,& n );      printf ( "Factorial of  %d  is :  %d " , n , fact ( n ));      getch (); } int   fact ( int   b ) {      int   i , f =

C Program to input and display details of students using structure

 C Program to input and display details of students using structure.       In this example, we will enter the details of students like roll number, name and total marks using structure and will be displayed. The total number of student should be entered according to the requirements of the user(maximum value is 50). We can increase the maximum number by changing the statement struct student[50];  to struct student[100];     Here in the output we have entered details of four student and displayed them accordingly. PROGRAM #include <stdio.h> #include <conio.h> struct   student {      int   roll ;      char   name [ 20 ];      int   total ; }; void   main () {      int   i , n ;      struct   student   std [ 50 ];      printf ( "Enter the number of students :" );      scanf ( " %d " ,& n );      printf ( " \n Enter the Students Details. \n " );      for ( i = 0 ; i < n ; i ++)     {          printf ( "Enter Roll Number : " );

C Program to find the sum of two numbers using user-defined function.

 C Program to find the sum of two numbers using user-defined function.          In this example, we will calculate the sum of to numbers using user-defined function.  Here in the output we have taked two numbers 23 and 65 which after calculation gives 88. PROGRAM #include <stdio.h> #include <conio.h> int   sum ( int   a , int   b ); void   main () {      int  x,y,res= 0 ;      printf ( "Enter the values of x and y :" );      scanf ( " %d%d " ,&x,&y);     res= sum (x,y);      printf ( "The sum is :  %d " ,res);      getch (); } int   sum ( int   a , int   b ) {      return (a+b); } OUTPUT Enter the values of x and y :23 65 The sum is : 88 NOTE:   You Can Comment Your Code if you have solved differently and we will pin it in the comment section.  Let's Learn together.  MOTTO:  You Learn, I Learn together We Learn.

C Program to find the sum of the series 1^2+2^2+3^2+…..+n^2

C Program to find the sum of the series  1 2 +2 2 +3 2 +…..+n 2 Enter the end limit at the runtime.       In this example, we calculate the sum of the above mentioned series till the end-limit entered by the user. We have used built-in function pow(x,y) to find the power of the given number.     Here in the output we have entered the value of end-limit as number 7 which gives a sum of 140.    PROGRAM #include <stdio.h> #include <math.h> #include <conio.h> void   main () {      int  n,sum= 0 ,i;      printf ( "Enter the end limit :" );      scanf ( " %d " ,&n);      for (i= 1 ;i<=n;i++)     {         sum=sum+ pow (i, 2 );     }      printf ( "Sum of the series is :  %d " ,sum);      getch (); } OUTPUT   Enter the end limit :7  Sum of the series is : 140 NOTE:   You Can Comment Your Code if you have solved differently and we will pin it in the comment section.  Let's Learn together.  MOTTO:  You Learn, I Learn together We Le

C Program to Swap the values of two variables using function.

C Program to Swap the values of two variables using function.     In this example, we swap the values of two variables using function. We have declared a function swap. PROGRAM #include <stdio.h> #include <conio.h> void   swap ( int *, int  *); int   main () {      int   a , b ;      printf ( "Enter the values of a and b : " );      scanf ( " %d%d " ,& a ,& b );      printf ( "Before swapping the values in main a= %d  , b= %d \n " , a , b );      swap (& a ,& b );      printf ( "After swapping the values in main a= %d  , b= %d \n " , a , b );      getch (); } void   swap ( int  * a , int  * b ) {      int   temp ;      temp =* a ;     * a =* b ;     * b = temp ;      printf ( "After swapping values in function a= %d , b= %d \n " ,* a ,* b ); } OUTPUT  Enter the values of a and b : 10  20  Before swapping the values in main a=10 , b=20  After swapping values in function a=20, b=10    After swapping the va

C Program to Multiply two Matrices

 C Program to Multiply two Matrices.     In this example, we will calculate the product of the two matrices allowing the user to enter their required number of rows and columns. In matrix multiplication the number of columns, in the first matrix must be equal to the number of  rows in the second matrix, otherwise multiplication is not possible. The product will have the number of rows of the first matrix and the number of columns of the second matrix.     Here in the output we have taken two matrices of order 2x3 and 3x2. The resulting matrix after calculating the product will be of the order 2x2. PROGRAM #include <stdio.h> #include <conio.h> void   main () {      int   row1 , row2 , column1 , column2 , i , j , k ;      int   a [ 10 ][ 10 ], b [ 10 ][ 10 ], mul [ 10 ][ 10 ];      printf ( "Enter the row size of the 1st Matrix : " );      scanf ( " %d " ,& row1 );      printf ( "Enter the column size of the 1st Matrix : " );      scanf ( &

C Program to find the Transpose of a Matrix.

C Program to find the Transpose of  a Matrix.       In this example, we will find the transpose of a given matrix according to the requirements of the row size and column size.      Here in the output we have entered a 3x2 Matrix whose transpose will be 2x3 Matrix. PROGRAM #include <stdio.h> #include <conio.h> int   main () {      int   row , column , i , j , a [ 100 ][ 100 ];      printf ( "Enter the row size of the matrix :" );      scanf ( " %d " ,& row );      printf ( "Enter the column size of the matrix : " );      scanf ( " %d " ,& column );      printf ( "Enter the elements of the matrix : " );      for ( i = 0 ; i < row ; i ++)     {          for ( j = 0 ; j < column ; j ++)         {              scanf ( " %d " ,& a [ i ][ j ]);         }     }      printf ( "The transpose of the MAtrix is :  \n " );      for ( j = 0 ; j < column ; j ++)     {          for ( i = 0 ; i <

C Program to Add Two Matrices Using Multi-dimensional Array.

C Program to Add Two Matrices Using Multi-dimensional Array.       In this example, we will add two matrices using multi-dimensional array. Two matrices can be added if they are equal means their order/dimensions must be same. Addition of matrices is performed by adding the corresponding elements of the two matrices.     Here in the Output we have  allowed the user to enter the row and column size of their matrices according to their requirements but they can enter the value of row size and column size less than 100. PROGRAM #include <stdio.h> #include <conio.h> int   main () {      int   row , column , i , j , a [ 100 ][ 100 ], b [ 100 ][ 100 ], c [ 100 ][ 100 ];      printf ( "Enter the row size of the matrices(max. value 100) : " );      scanf ( " %d " ,& row );      printf ( "Enter the column size of the matrices(max. value 100) : " );      scanf ( " %d " ,& column );      printf ( "Enter the elements of the 1st Mat