Skip to main content

Posts

Showing posts with the label c programming

strings in c

#include <stdio.h> int main() { char name1[] = { 's' , 'a' , 't' , 'y' , 'a' , 'm' , ' \0 ' ,}; printf ( "%s" ,name1); char name2[] = " \n satyam" ; printf ( "%s" ,name2); return 0 ; }

example of printing star pattern

#include <stdio.h> void starPattern( int rows ) { for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j <= i; j ++ ) { printf ( "*" ); } printf ( " \n " ); } } void reverseStarPattern( int rows ) { for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j <= rows - i - 1 ; j ++ ) { printf ( "*" ); } printf ( " \n " ); } } int main() { int rows, type; printf ( "Enter 0 for star pattern \n Enter 1 for reversed star pattern \n " ); scanf ( "%d" , & type); printf ( "How many rows do you want? \n " ); scanf ( "%d" , & rows); switch (type) { case 0 : starPattern (rows); break ; case 1 : reverseStarPattern (rows); break ; default: printf ( "You have entered an invalid choice" ); break ; } return 0 ; }

passing array to a function by declaring pointers in the function

#include <stdio.h> int fun1( int* ptr ) { for ( int i = 0 ; i < 4 ; ++ i) { printf ( "The value of %d is %d \n " ,i, ptr [i]); //or printf ( "The value of %d is %d \n " ,i, * ( ptr + i)); // both do the same work } * ( ptr + 2 ) = 58 ; //value gets changed of 2nd index of array } int main () { int arr[] = { 2 , 4 , 6 , 8 }; fun1 (arr); fun1 (arr); return 0 ; }

passing array to a function by declaring array as parameter in function

#include <stdio.h> int fun1( int array []) { for ( int i = 0 ; i < 4 ; ++ i) { printf ( "The value of %d is %d \n " ,i, array [i]); } array [ 0 ] = 89 ; /*if we change the formal(parameter) value effect will be reflected in actual(parameter) value as it passes the address of array*/ } int main () { int arr[] = { 2 , 4 , 6 , 8 }; printf ( "The value of index 0 is %d \n " ,arr[ 0 ]); fun1 (arr); printf ( "The value of index 0 is %d \n " ,arr[ 0 ]); return 0 ; }

Array pointers arithmetic in c

#include <stdio.h> int main () { int arr [] = { 1 , 4 , 5 , 9 , 7 , 6 }; printf ( "The value of array at position 3 is %d \n " , arr [ 3 ]); printf ( "The address of first element of array is %d \n " , & arr [ 0 ]); printf ( "The address of first element of array is %d \n " , arr ); printf ( "The address of second element of array is %d \n " , & arr [ 1 ]); printf ( "The address of first element of array is %d \n " , arr + 1 ); printf ( "The value of address at first element of array is %d \n " , * ( arr )); printf ( "The value of address at first element of array is %d \n " , * ( & arr [ 0 ])); printf ( "The value of address at second element of array is %d \n " , * ( & arr [ 1 ])); printf ( "The value of address at first element of array is %d \n " , arr [ 0 ]); printf ( "The value of address at second element of array is %d \n " , * ( ar...

Pointer arithmetic in C

#include <stdio.h> int main () { int a = 24 ; int* ptra = & a ; char b = '3' ; char* ptrb =& b ; printf ( "%d \n\n " , ptrb ); //size of char is 1 ptrb ++ ; printf ( "%d \n\n " , ptrb ); // add 1 to &b ptrb -- ; printf ( "%d \n\n " , ptrb + 1 ); // add 1 to &b printf ( "%d \n\n " , ptrb - 1 ); // subtract 1 to &b printf ( "%d \n\n " , ptrb + 1 ); // add 1 to &b printf ( "%d \n\n " , ptrb - 2 ); // subtract 2*1 to &b printf ( "%d \n\n " , ptra ); printf ( "%d \n\n " , ptra + 1 ); // size of int is 4 so, it add 4 to &a/ptra printf ( "%d \n " , ptra - 2 ); // size of int is 4 so, it subtract 2*4 to &a/ptra return 0 ; }

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 " );    ...

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 ]);         }                     ...

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 <s...

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 ...

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...

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...

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 ()...

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 ++)       {            prin...

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   sa...