Posts

Showing posts from April, 2023

STRING IN C.

  #include <stdio.h> #include <string.h> int main () {     char string1 [] = "Aatifa" ;     char string2 [] = "Mugheer" ;     char string3 [ 67 ];         printf ( "The length of s1 is %d \n " , strlen ( string1 ));     printf ( "The length of s2 is %d \n " , strlen ( string2 ));     printf ( "The reversed string s1 is: " );     puts ( strrev ( string1 ));     printf ( "The reversed string s2 is: " );     puts ( strrev ( string2 ));     strcpy ( string3 , strcat ( string1 , string2 ));     puts ( string3 );       printf ( "The strcmp for string1, string2 returned %d " , strcmp ( string1 , string2 ));     return 0 ; } OUTPUT: The length of s1 is 6 The length of s2 is 7 The reversed string s1 is: afitaA The reversed string s2 is: reehguM afitaAreehguM The strcmp for string1, string2 returned -1
  #include <stdio.h> void printstr ( char str [] ) {     int i = 0 ;     while ( str [ i ]!= ' \0 ' )     {         printf ( " %c " , str [ i ]);         i ++;     }     printf ( " \n " ); } int main () {     // // char str[] = {'a','a','t','i','f','a','\0'};     // char str[7] = "aatifa";     char str [ 34 ];     gets ( str );     printf ( "Using custom function printstr \n " );     printstr ( str );     printf ( "Using printf %s \n " , str );     printf ( "Using puts: \n " );     puts ( str );     return 0 ; }

STAR PATTERN IN C.

  #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 and 1 for Reverse Star Pattern \n " );     scanf ( " %d " , & type );     printf ( "How many rows do you want? \n...

Passing Arrays As Function Arguments in C.

  #include <stdio.h> int func1 ( int array [] ) {     for ( int i = 0 ; i < 4 ; i ++)     {         printf ( "The value at %d is %d \n " , i , array [ i ]);     }     // array[0] = 457; if you change any value here, it gets reflected in main()     return 0 ; } void func2 ( int * ptr ) {     for ( int i = 0 ; i < 4 ; i ++)     {         printf ( "The value at %d is %d \n " , i , *( ptr + i ));     }     *( ptr + 2 ) = 564 ; } void func3 ( int arr [ 2 ][ 2 ]) {     for ( int i = 0 ; i < 2 ; i ++)     {         for ( int j = 0 ; j < 2 ; j ++)         {             printf ( "The value at %d , %d is %d \n " , i , arr [ i ][ j ]);         }     } } int main () {     int arr [] [ 2 ] =...

Call By Reference in C.

  #include <stdio.h> void changevalue ( int * address ) {     * address = 456 ; } int main () {     int a = 34 , b = 56 ;     printf ( "The value of a now is %d \n " , a );     changevalue (& a );     printf ( "The value of a now is %d \n " , a );     return 0 ; } OUTPUT: The value of a now is 34 The value of a now is 456

ARRAYS in C.

  #include <stdio.h> int main () {     int arr [] = { 1 , 2 , 3 , 4 , 5 , 6 , 87 };     printf ( " %d " , arr [ 0 ]);     return 0 ; } OUTPUT: 1 #include <stdio.h> int main () {     int arr [] = { 1 , 2 , 3 , 4 , 5 , 6 , 87 };     printf ( " %d " , arr [ 6 ]);     return 0 ; } OUTPUT: 87

Pointers arithmetic in C.

  #include <stdio.h> int main () {     int a = 34 ;     int * ptra = & a ;     printf ( " %d \n " , ptra ++);     printf ( " %d " , ptra + 1 );     return 0 ; } OUTPUT: 6422296 6422304

POINTERS IN C.

  #include <stdio.h> int main () {     printf ( "Let's learn about pointers \n " );     int a = 76 ;     int * ptra = & a ;     int * ptra2 = NULL ;     printf ( "The address of pointer to a is %p \n " , & ptra );       printf ( "The address of a is %p \n " , & a );     printf ( "The address of a is %p \n " , ptra );       printf ( "The address of some garbage is is %p \n " , ptra2 );     printf ( "The value of a is %d \n " , * ptra );     printf ( "The value of a is %d \n " , a );     return 0 ; } OUTPUT: Let's learn about pointers The address of pointer to a is 0061FF14 The address of a is 0061FF18 The address of a is 0061FF18 The address of some garbage is is 00000000 The value of a is 76 The value of a is 76

C program to print the marks using two dimensional array.

  #include <stdio.h> int main () {     int marks [ 2 ][ 4 ] = {{ 343 , 56 , 89 , 45 },                        { 3 , 2 , 7 , 5 }};     for ( int i = 0 ; i < 2 ; i ++)     {         for ( int j = 0 ; j < 4 ; j ++)         {             /* code */             // printf("the value of %d, %d element of the array is %d\n", i, j, marks[i][j]);             printf ( " %d\n " , marks [ i ][ j ]);         }         printf ( " \n " );     }     return 0 ; } OUTPUT: 343 56 89 45 3 2 7 5

C program to print the marks of students using one dimensional array.

  #include <stdio.h> int main () { int marks [ 4 ] = { 343 , 56 , 89 , 45 };     for ( int i = 0 ; i < 4 ; i ++)     {         printf ( "the value of %d element of the array is %d \n " , i , marks [ i ]);             }     return 0 ; } OUTPUT: the value of 0 element of the array is 343 the value of 1 element of the array is 56 the value of 2 element of the array is 89 the value of 3 element of the array is 45

One Dimensional array in C.

#include <stdio.h> int main () {     // printf("Hello World\n");     int marks [ 4 ];     for ( int i = 0 ; i < 4 ; i ++)     {         printf ( "Enter the value of %d element of the array \n " , i );         scanf ( " %d " , & marks [ i ]);     }     for ( int i = 0 ; i < 4 ; i ++)     {         printf ( "the value of %d element of the array is %d \n " , i , marks [ i ]);             }         // int marks[4];     // marks[0] = 34;     // printf("marks of student 1 is %d\n", marks[0]);     //  marks[0] = 5674;     // printf("marks of student 1 is %d\n", marks[0]);     // marks[0] = 5674;     // marks[1] = 5674;     // marks[2] = 5674;     // marks[3] = 5674;     // printf("marks of studen...

C Program to find the factorial of any number.

  #include <stdio.h> int factorial ( int number ) {     if ( number == 1 || number == 0 )     {         return 1 ;     }     else {         return ( number * factorial ( number - 1 ));     }     } int main () {     int num ;     printf ( "Enter the number you want the factorial of \n " );     scanf ( " %d " , & num );     printf ( "The factorial of %d is %d \n " , num , factorial ( num ));     return 0 ; } OUTPUT: Enter the number you want the factorial of 3 The factorial of 3 is 6 Enter the number you want the factorial of 4 Enter the number you want the factorial of 0 The factorial of 0 is 1

Function Without Argument And With Return Value

  #include <stdio.h> int takenumber () {     int i ;     printf ( "Enter a number" );     scanf ( " %d " , & i );     return i ; } int main () {     int a , b , c ;     a = 9 ;     b = 87 ;     c = takenumber ();     printf ( "The number entered is %d \n " , c );     return 0 ; } OUTPUT: Enter a number67 The number entered is 67

Fuction with argument and with return value

  #include <stdio.h> int sum ( int a , int b ); void printstar ( int n ) {     for ( int i = 0 ; i < n ; i ++)     {         printf ( " %c " , '*' );     }     } int main () {     printstar ( 7 );     // printf("The sum is %d\n", c);     return 0 ; } OUTPUT: *******

Function with argument and with return value

  #include <stdio.h> int sum ( int a , int b ) {     return a + b ; } int main () {     int a , b , c ;     a = 9 ;     b = 87 ;     c = sum ( a , b );     printf ( "The sum is %d \n " , c );     return 0 ; } OUTPUT: The sum is 96

C Program to print multiplication table of any number.

  #include <stdio.h> int main () {     int num ;     printf ( "Enter the number you want multiplication table of: \n " );     scanf ( " %d " , & num );     printf ( "Multiplication table of %d is as follows: \n " , num );     for ( int i = 1 ; i < 11 ; i ++)     {         printf ( " %d x %d = %d \n " , num , i , num * i );     }         return 0 ; } Enter the number you want multiplication table of: 4 Multiplication table of 4 is as follows: 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 4 x 10 = 40 Enter the number you want multiplication table of: 67 Multiplication table of 67 is as follows: 67 x 1 = 67 67 x 2 = 134 67 x 3 = 201 67 x 4 = 268 67 x 5 = 335 67 x 6 = 402 67 x 7 = 469 67 x 8 = 536 67 x 9 = 603 67 x 10 = 670 Enter the number you want multiplication table of: 436 Multiplication table of 436...

C Program to convert Kms to Miles, Inches to foot, cms to Inches, Pound to Kgs, Inches to Metre

  include <stdio.h> int main () {     char input ;     float KmsToMiles = 0.621371 ;     float inchesToFoot = 0.0833333 ;     float cmsToInches = 0.393701 ;     float poundToKgs = 0.453592 ;     float inchesToMetres = 0.0254 ;     float first , second ;     while ( 1 )     {         printf ( "Enter the input character. q to quit \n       1. kms To miles \n       2. inches to foot \n       3. cms to inches \n       4. pound to kgs \n       5. inches to metre \n " );                 scanf ( " %c " , & input );         switch ( input )         {         case 'q' :             printf ( "Quiting the program...." );         ...