Posts

Showing posts from March, 2022

encypting a string

  #include <stdio.h>   void encrypt( char * c)  {       char *ptr=c; while (*ptr!= '\0' ) {     *ptr=*ptr+ 1 ; ptr++; }  } int main(){     char str [] = "Anchal" ; encrypt(str); printf( "The encrypte string is:%s" ,str);   return 0 ; }

copying a string from source to target

  #include <stdio.h> #include <string.h> char strcopy( char *st1, char *st2) {     int i= 0 ;     char * ptr=st1;     int l=strlen(st1);     while (i<l)     { st1[i]=st2[i]; i++;     }     st1[i]= '\0' ; } int main(){ char str1 [] = "anchal" ; char str2 [] = "anaya" ; printf( "Old strings are :%s %s\n" ,str1,str2); strcopy(str1,str2); printf( "New strings are :%s %s" ,str1,str2);   return 0 ; }

making your own strcmp function

  #include <stdio.h> #include <string.h> int main(){ char st1[ 30 ]; char st2[ 30 ]; char c; int i= 0 ; printf( "Enter string 1:\n" ); scanf( "%s" ,st1); printf( "Enter string 2 character by character:\n" ); while (c!= '\n' ) { fflush( stdin ); scanf( "%c" ,&c); st2[i]=c; i++; } st2[i- 1 ]= '\0' ; printf( "String 1 is:%s\n" ,st1); printf( "string 2 is:%s\n" ,st2); printf( "The comparison of string 1 and string 2 returns:%d" ,strcmp(st1,st2));   return 0 ; }

slicing a string

  #include <stdio.h> char slice( char *st, int m, int n) {     int i= 0 ;     while ((i+m)<n)     {         st[i]=st[i+m];         i++;     }    st[i]= '\0' ; } int main(){ char str [] = "Anchalanaya" ; slice(str, 1 , 8 ); printf( "The sliced string is:%s\n" ,str);   return 0 ; }

slicing a string

  #include <stdio.h> char slice( char * str, int m, int n) {     int i= 0 ;     char str1[ 3 ];     while ((i+m)<n)     {         str1[i]=str[i+m];         i++;                     }     str1[i]= '\0' ;     printf( "The sliced string is:%s" ,str1); } int main(){ char c [] = "AnchalAggarwal" ; slice(c, 2 , 5 );   return 0 ; }

calculating length of a string

  #include <stdio.h> int strlen_mine( char *str) {     int len= 0 ;     char * ptr=str;     while (*ptr!= '\0' )     {         ptr++;         len++;     }     printf( "The length of the string is:%d\n" ,len); } int main(){ char c[ 30 ]; printf( "Enter the string\n" ); //scanf("%s",c); gets(c); strlen_mine(c);   return 0 ; }

comparing strings

  #include <stdio.h> #include <string.h> int main(){ char st1 [] = "Anchal" ; char *st2= "Anchal" ; int val= strcmp(st1,st2); printf( "%d" ,val);   return 0 ; }

printing a string

  #include <stdio.h> int main(){ char a [] ={ 'A' , 'N' , 'C' , 'H' , 'A' , 'L' , '\0' }; char b [] = "ANAYA" ; char *ptr=b; while (*ptr!= '\0' ) {     printf( "%c" ,*ptr);     ptr++; }   return 0 ; }

printing the elements of a 3d Array

  #include <stdio.h> int main(){ int arr[ 2 ][ 3 ][ 4 ]; for ( int i= 0 ; i< 2 ; i++)  {       for ( int j= 0 ; j< 3 ; j++)      {           for ( int k= 0 ;k< 4 ;k++)          {              printf( "Enter the elements of array :\n" );              scanf( "%d" ,&arr[i][j][k]);          }      }  }   for ( int i= 0 ; i< 2 ; i++)  {       for ( int j= 0 ; j< 3 ; j++)      {           for ( int k= 0 ;k< 4 ;k++)          {              printf( "The  of the elements of array is:%u\n" ,arr[i][j][k]);          }      }  }   return 0 ; }

populate a multi dimentional array with multi table of 3,6 and 9

  #include <stdio.h> void print_table( int * multable, int num, int n); int main(){ int multable[ 3 ][ 10 ]; print_table(multable[ 0 ], 3 , 10 ); print_table(multable[ 0 ], 6 , 10 ); print_table(multable[ 0 ], 9 , 10 );   return 0 ; } void print_table( int * multable, int num, int n) {     printf( "The multiplication table of %d:\n\n" ,num);             for ( int i = 0 ; i < n; i++)     {         multable[i]=num*(i+ 1 );     }             for ( int i= 0 ;i<n;i++)      {        printf( "%dX%d=%d\n" ,num,i+ 1 ,multable[i]);     }     }

comparing pointers

  #include <stdio.h> int main(){ int arr[ 10 ]; int * ptr= &arr[ 0 ]; ptr=ptr+ 2 ; if (ptr==&arr[ 2 ]) {     printf( "These point to the same memory location\n%u %u" ,ptr,&arr[ 2 ]); }     else     {         printf( "These do not point to the same memory location\n" );     }   return 0 ; }

multi dimentional array

  #include <stdio.h> int main() { int num_student= 3 ; int num_subject= 5 ; int marks[ 3 ][ 5 ]; for ( int i= 0 ;i<num_student;i++) {     for ( int j= 0 ;j<num_subject;j++)     {         printf( "Enter the marks of student %d for subject %d:\n" ,i+ 1 ,j+ 1 );         scanf( "%d" ,&marks[i][j]);     } } for ( int i= 0 ;i<num_student;i++) {     for ( int j= 0 ;j<num_subject;j++)     {         printf( "The  marks of student %d for subject %d is: %d\n" ,i+ 1 ,j+ 1 ,marks[i][j]);     } }   return 0 ; }

print array

  #include <stdio.h> void print_array( int *ptr, int n); int main() {     int a [] = { 33 , 54 , 675 , 78 , 45 , 456 };     print_array(a, 6 );     return 0 ; } void print_array( int *ptr, int n) {     for ( int i = 0 ; i < n; i++)     {         printf( "The %d element of array is:%d\n" , i + 1 , *(ptr + i));     } }

pointer subtraction

  #include <stdio.h> int main(){ int a [] ={ 3 , 5 , 6 }; int * ptr1=&a[ 0 ]; int * ptr2=&a[ 1 ]; printf( "The value of ptr1 is: %u\n" ,ptr1); printf( "The value of ptr2 is: %u\n" ,ptr2); printf( "The difference of ptr2 and ptr1 is %u\n" , ptr2-ptr1);   return 0 ; }

sum and average using pointers

  # include < stdio.h > void sum_average ( int a , int b , int * sum , float * avg ); int main () {     int a , b , sum ;     float avg ;     printf (" Enter two numbers: \n ");     scanf (" %d %d ", & a , & b );     sum_average ( a , b , & sum , & avg );     printf (" The value of sum is %d and average is %f : \n ", sum , avg );     return 0 ; } void sum_average ( int a , int b , int * sum , float * avg ) {     * sum = a + b ;     * avg = (float) * sum / 2 ; }

making the value of a variable to its 10 times

# include < stdio.h > void change_val ( int * x ); int main (){ int a = 10 ; printf (" The value of a before change fucntions is: %d \n ", a ); change_val ( & a ); printf (" The value of a before change fucntions is: %d \n ", a );   return 0 ; } void change_val ( int * x ) {     * x = ( * x ) * 10 ; }

number swapping

  # include < stdio.h > void swap ( int * x , int * y ); int main () {     int a = 3 , b = 4 ;     printf (" The value of a and b before swap is: %d and %d \n ", a , b );     swap ( & a , & b );     printf (" The value of a and b after swap is: %d and %d \n ", a , b );     return 0 ; } void swap ( int * x , int * y ) {     int temp ;     temp = * x ;     * x = * y ;     * y = temp ;     return ; }

pointers and pointer to pointer

  # include < stdio.h > int main (){ int i = 60 ; int * j =& i ; int ** k =& j ; printf (" The value of i is: %d \n ", i ); printf (" The value of i is: %d \n ", * j ); printf (" The address of i is: %u \n ", & i ); printf (" The address of i is: %u \n ", j ); printf (" The address of j is: %u \n ", & j ); printf (" The value of j is: %u \n ", * ( & j )); printf (" The value of j is: %u \n ", * k ); printf (" The address of j is: %u \n ", k ); printf (" The value of k is: %u \n ", * ( & k ));   return 0 ; }

printing star with iteration

  # include < stdio.h > void star ( int a ); int main () {     int a ;     printf (" Enter how many rows you want to print: \n ");     scanf (" %d ", & a );     star ( a );     return 0 ; } void star ( int a ) {     for ( int i = 1 ; i <= a ; i ++ )     {         for ( int j = 0 ; j < ( 2 * i - 1 ); j ++ )         {             printf (" * ");         }         printf (" \n ");     } }

calculate force

#include <stdio.h> float force ( float mass ); int main () {     float m ;     printf ( "Enter the value of masss in kgs: \n " );     scanf ( " %f " , & m );     printf ( "The value of force in Newton is: % 0.2f " , force ( m ));     return 0 ; } float force ( float mass ) {     return mass * 9.8 ; }

celsius to fhrenheit with functions

  #include <stdio.h> float convert_cel_to_fah ( float c ); int main (){ float tempc ; printf ( "Enter the temperature in celsius \n " ); scanf ( " %f " ,& tempc ); printf ( "The converted temperature is: % 0.2f " , convert_cel_to_fah ( tempc ));   return 0 ; } float convert_cel_to_fah ( float c ) { return ( c * 9 / 5 )+ 32 ; }

average function

#include <stdio.h> float average ( int a , int b , int c ); int main (){ int a , b , c ; printf ( "Enter the value of a: \n " ); scanf ( " %d " ,& a ); printf ( "Enter the value of b: \n " ); scanf ( " %d " ,& b ); printf ( "Enter the value of c: \n " ); scanf ( " %d " ,& c ); printf ( "The value of average is: % 0.2f " , average ( a , b , c ));   return 0 ; } float average ( int a , int b , int c ) { return ( float )( a + b + c )/ 3 ; }

factorial-recursion

  #include <stdio.h> int factorial ( int a ); int main () {     int a = 5 ;     printf ( "The factorial of %d is: %d \n " , a , factorial ( a ));     return 0 ; } int factorial ( int x ) {     if ( x == 0 || x == 1 )     {         return 1 ;     }     else     {         return x * factorial ( x - 1 );     } }

change function

#include <stdio.h> int change ( int a ); int main (){ int b = 32 ; printf ( "The value of b before change: %d \n " , b ); change ( b ); printf ( "The value of b after change: %d \n " , b ); int d = change ( b ); printf ( "The value of b in function: %d \n " , d );   return 0 ; } int change ( int a ) {     a = 77 ;     return a ; }

add function

  #include <stdio.h> int sum ( int a , int b ); int main (){ int c = 4 , d = 5 ; printf ( "The sum of %d and %d is: %d " , c , d , sum ( c , d ));   return 0 ; } int sum ( int a , int b ) {     return ( a + b ); }

functions

  #include <stdio.h> void goodmorning (); void goodafternoon (); void goodevening (); int main (){ goodmorning ();   return 0 ; } void goodmorning () {     printf ( "Good Morning! \n " );     goodafternoon (); } void goodafternoon () {     printf ( "Good Afternoon! \n " );       goodevening (); } void goodevening () {     printf ( "Good Evening! \n " );     }

game-guess a number between 1 and 100

  #include <stdio.h> #include <stdlib.h> #include <time.h> int main () {     int i = 1 , num , gnum ;     srand ( time ( 0 ));     num = rand () % 100 + 1 ;     do     {         printf ( "Guess the number between 1 and 100: \n " );         scanf ( " %d " , & gnum );         if ( gnum < num )         {             printf ( "Enter a higher number \n " );         }         else if ( gnum > num )         {             printf ( "Enter a lower number\n" );         }         else         {             printf ( "You guessed the number in %d attempts" , i );         }         ...

prime number

  #include <stdio.h> int main () {     int i , n ;     printf ( "Enter a number to check: \n " );     scanf ( " %d " , & n );     for ( i = 2 ; i < n ; i ++)     {         if ( n % i != 0 )         {             printf ( "The number is a prime number \n " );             break ;         }         else         {             printf ( "The number is not a prime number \n " );             break ;         }     }     return 0 ; }

sum of table of n

  #include <stdio.h> int main () {     int i , sum = 0 , n ;     printf ( "Enter the number: \n " );     scanf ( " %d " , & n );     for ( i = 1 ; i <= 10 ; i ++)     {         sum = sum + ( n * i );     }     printf ( "The sum of %d is: %d " , n , sum );     return 0 ; }

Sum of n natural numbers

  #include <stdio.h> int main () {     int i = 1 , sum = 0 , n ;     printf ( "Enter the value of n \n " );     scanf ( " %d " , & n );     while ( i <= n )     {         sum += i ;         i ++;     }     printf ( "The sum of %d natural numbers is: %d " , n , sum );     return 0 ; }