Skip to main content

Posts

Showing posts from July, 2020

pointers in c

#include <stdio.h> int   main () {      int   a = 27 ;      int  * ptr  = & a ;      int  * ptr1 = NULL ;      printf ( "The value of a is %d \n " , a );      printf ( "The value of a is %d \n " ,* ptr );      printf ( "The address of a is %p \n " ,& a );      printf ( "The address of a is %p \n " , ptr );      printf ( "The address of ptr is %p \n " ,& ptr );      printf ( "The value of some garbage is %p \n " , ptr1 );      printf ( "The address of some garbage is %p \n " ,& ptr1 );      return   0 ; }

calculator to add,subtract,multiply,divide by using switch statement and infinite while loop statement

#include   <stdio.h> int   main () {      float   a , b ;      char   input ;      while ( 1 )     {      printf ( "press 1 to add \n " );      printf ( "press 2 to subtract \n " );      printf ( "press 3 to multiply \n " );      printf ( "press 4 to divide \n " );      printf ( "press q to quit the program \n " );      scanf ( " %c" , & input );      switch  ( input )        {             case   'q' :             printf ( "The program is quitting...... \n " );             goto   end ;             break ;         case   '1' :      printf ( "Enter first number to add \n " );      scanf ( "%f" ,& a );      printf ( "Enter second number to add \n " );      scanf ( "%f" ,& b );      printf ( "%f+%f = %f \n " , a , b , a + b );       break ;       case   '2' :      printf ( "Enter first number to subtract \n " );      sca

Array example 3

#include <stdio.h> int   main () {      int   marks [ 5 ][ 5 ]={{ 95 , 93 , 97 , 87 , 94 },                      { 87 , 89 , 88 , 85 , 83 }};           for  ( int   i  =  0 ;  i  <  2 ;  i ++)     {          for  ( int   j  =  0 ;  j  <  5 ;  j ++)         {              printf ( "The value of %d, %d value of the array is %d \n " , i , j , marks [ i ][ j ]);         }                       }           return   0 ; }

1-D array example 2

#include <stdio.h> int   main () {      int   marks [ 5 ]; //declare without initialization      for  ( int   i  =  0 ;  i  < 5 ;  i ++)     {          printf ( "Enter the %d of the valve of array \n " , i );          scanf ( "%d" ,& marks [ i ]);     }      for  ( int   i  =  0 ;  i  <  5 ;  i ++)     {          printf ( "The value of %d value of the array is %d \n " , i , marks [ i ]);     }           return   0 ; } //Or we can also do like this #include <stdio.h> int   main () {      int   marks [ 5 ]={ 95 , 97 , 91 , 93 , 99 }; // declare with initialization           for  ( int   i  =  0 ;  i  <  5 ;  i ++)     {          printf ( "The value of %d value of the array is %d \n " , i , marks [ i ]);     }           return   0 ; }

Array simple example 1

#include <stdio.h> int   main () {      int   marks [ 5 ];      marks [ 0 ]= 98 ;      marks [ 1 ]= 95 ;      marks [ 2 ]= 93 ;      marks [ 3 ]= 97 ;      marks [ 4 ]= 91 ;      printf ( "The marks of student [0] is %d \n  " , marks [ 0 ]);      printf ( "The marks of student [1] is %d \n  " , marks [ 1 ]);      printf ( "The marks of student [2] is %d \n  " , marks [ 2 ]);      printf ( "The marks of student [3] is %d \n  " , marks [ 3 ]);      printf ( "The marks of student [4] is %d \n  " , marks [ 4 ]);      printf ( "The average marks of students is %d \n " ,     ( marks [ 0 ]+ marks [ 1 ]+ marks [ 2 ]+ marks [ 3 ]+ marks [ 4 ])/ 5 );      return   0 ; }

unit conversion example with recursion

#include <stdio.h> float   conversion1 ( float   number ) {      return   number * 100 ; // conversion metre to cm } float   conversion2 ( float   number ) {      return   number * 74.93 ;  // conversion usd to rupees } int   main () {      int   num ;      float   num1 , num2 ;      char   metre , cm , usd , rupees , kg , pounds ;      printf ( "Enter 1 for conversion metre to cm \n " );      printf ( "Enter 2 for conversion usd to rupees \n " );      scanf ( "%d" ,& num );      if  ( num == 1 )     {          printf ( "Enter the number you want conversion of:- \n " );          scanf ( "%f" ,& num1 );          printf ( "%f metre=%f cm \n " , num1 , conversion1 ( num1 ));     }      if  ( num == 2 )     {          printf ( "Enter the number you want conversion of:- \n " );          scanf ( "%f" ,& num2 );          printf ( "%f usd=%f rupees \n " , num2 , conversion2 ( num2 ));   

unit conversion example without recursion

#include   <stdio.h> int   main () {      int   selection ;      float   inr ,  usd ,  pounds ,  kg ,  meter ,  cm ;      printf ( "please press 1 to convert USD to INR \n " );      printf ( "please press 2 to convert POUNDS to KG \n " );      printf ( "please press 3 to convert CM to METER \n " );      scanf ( "%d" , & selection );     //im using if/else condition..i can also use switch statement but it will better for me xD      if  ( selection  ==  1 )     {          printf ( "please enter USD amount to convert into INR \n " );          scanf ( "%f" , & inr );          usd  =  inr  *  75.36 ;          printf ( "INR = %f" ,  usd );          printf ( "Thanks for using conversion program \n " );  //this is my credit     }      else   if  ( selection  ==  2 )     {          printf ( "please enter kg to convert into pounds \n " );          scanf ( "%f" , & kg );         

Recursion in C. To find the factorial of any number

//Any function which call itself is called recursion in c //A termination condition is imposed on such functions to stop them executing copies of themselves forever. // example to find factorial of number #include<stdio.h> int factorial(int number) { if (number==0||number==1) { return 1; } else { return(number*factorial(number-1)); } } int main() { int a=5,b=6; do { int num; printf("Enter the number you want factorial of:-\n"); scanf("%d",&num); printf("The factorial of %d is %d\n",num,factorial(num)); } while(a<b); return 0; }

example of typecasting in c

/*Type Casting in C Typecasting allows us to convert one data type into other. */ //simple example to cast int value into the float. #include <stdio.h>    int   main (){   float   f = ( float ) 9 / 4 ;     printf ( "f : %f \n " ,  f  );     return   0 ;   }     

example of goto statement in c

/* C goto statement The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer  the program control to a predefined label.  The goto statement can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple  loops which can't be done by using a single break statement. However, using goto is  avoided these days since it makes the  program less readable and complicated. */ //simple example to use goto statement in C language. #include   <stdio.h>    int   main ()    {      int   num , i = 1 ;       printf ( "Enter the number whose table you want to multiplication of:- \n " );       scanf ( "%d" ,& num );      table :       printf ( "%d x %d = %d \n " , num , i , num * i );      i ++;      if ( i <= 10 )      goto   table ;     }   //When should we use goto? /*The only condition in which using goto is preferable is  when we need to break the multiple loo

break statement in C

/* The break is a keyword in C which is used to bring the program control out of the loop.  The break statement is used inside loops or switch statement. The break statement   breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner  loop first and then proceeds to outer loops.  */ //example of break statement in loop statement #include <stdio.h>    int   main  ()   {        int   i ;        for ( i  =  0 ;  i < 10 ;  i ++)       {            printf ( "%d " , i );            if ( i  ==  5 )            break ;       }        printf ( "came outside of loop i = %d" , i );          }   //examples of break statement in switch statement #include <stdio.h>    int   main (){     int   number = 0 ;      printf ( "enter a number: \n " );     scanf ( "%d" ,& number );     switch ( number ) {     case   10 :     printf ( "number is equals to 10" );     break ;     case   50 :     printf ( "number is

concept and example of function in c

v /* Types of functions 1) Predefined standard library functions:- Standard library functions are also known as built-in functions.  Functions such as puts(), gets(), printf(), scanf() etc are   standard library functions For example, printf() function is defined in <stdio.h>  header file so in order to use the printf() function,   we need to include the <stdio.h> header file in our    program using #include <stdio.h>. 2) User Defined functions The functions that we create in a program are known  as user defined functions or in other words you can   say that a function created by user is known as  user defined function. Now we will learn how to create user defined functions and how to use them in C Programming Syntax of a function return_type function_name (argument list) {     Set of statements – Block of code } return_type: Return type can be of any data type such as int, double, char, void, short etc. function_name:- It can be anything, however it is advised to hav