Posts

Sum 1 to n Recursion

 #include <stdio.h> // Standard Input/Output library for printf and scanf // Function Prototype (Declaration) - Good practice in C void sum(int n, int s); // Recursive function to calculate the sum of numbers from 1 to n // n: the current number to add // s: the running sum (accumulator) void sum(int n, int s) {     // Base Case (Stopping Condition): When n becomes 0, the recursion stops.     if (n == 0) {         // Print the final accumulated sum (s)         printf("%d\n", s);         return;     }          // Recursive Step: Call the function again with     // 1. n decreased by 1 (moving to the next number)     // 2. s increased by the current value of n (adding n to the running sum)     sum(n - 1, s + n);          // The 'return;' after the recursive call is optional here since the function returns void. ...

1 to n without any parameter Recursion

  #include <iostream> using namespace std ; void print ( int n ){     if ( n == 0 ) return ;     print ( n - 1 );     cout << n << endl ; } int main (){     int n ;     cin >> n ;     print ( n ); } //EXPLANATION /* before printing n bar bar call laga rha h same funnction ko 4 se minus hote hote 1 tak gya uske bad jb n 0 hua to return hua aur uske bad jita sara fucntion ko call kiya wo sb phirse run honge kyunki wo sb run pura nhi hua tb jake wo piche se suru hoga print krna phir jb n 1 that wo fnc khtm hoga uske bad jb n 2 tb wo hoga khtm krte krte puche se fnc khtm hoga */

Recursive Power Function

 #include <stdio.h> // Standard Input/Output library for printf and scanf // Function Prototype (Declaration) - Best practice in C int power(int a, int b); // Recursive function to calculate a to the power of b (a^b) int power(int a, int b) {     // Base Case: Any number raised to the power of 0 is 1     if (b == 0) {         return 1;     }          // Recursive Step: a^b = a * a^(b-1)     return a * power(a, b - 1); } // Main function int main() {     int a;     printf("enter a: ");     // Use %d format specifier for reading an integer     scanf("%d", &a);      int b;     printf("enter b: ");     // Use %d format specifier for reading an integer     scanf("%d", &b);      // Print the result     printf("%d raised to %d = %d\n", a, b, power(a, b));     return 0; // R...

Recursion Fibonacci

  #include <stdio.h> int fibonacci ( int n ) {     if ( n == 0 ) {         return 0 ;     } else if ( n == 1 ) {         return 1 ;     } else {         // Recursive case: F(n) = F(n-1) + F(n-2)         return fibonacci ( n - 1 ) + fibonacci ( n - 2 );     } } int main () {     int n , i ;     printf ( "Enter no.: " );     scanf ( " %d " , & n );     for ( i = 0 ; i < n ; i ++ ){         printf ( " %d " , fibonacci ( i ));     }     printf ( " \n " );     return 0 ; }

Recursion Factorial

  #include <stdio.h> int factorial ( int n ) {     if ( n == 0 || n == 1 ){         return 1 ;     } else {         return n * factorial ( n - 1 );     } } int main () {     int n ;     printf ( "Enter no.: " );     scanf ( " %d " , & n );     printf ( " %d " , factorial ( n ));     return 0 ; }

Transpose

  #include <stdio.h> int main () {     int m , n ;     printf ( "Enter the number of rows and columns of the matrix: " );     scanf ( " %d %d " , & m , & n );     int matrix [ m ][ n ]; // Original matrix     int transpose [ n ][ m ]; // Transposed matrix     printf ( "Enter the elements of the matrix: \n " );     for ( int i = 0 ; i < m ; i ++ ){         for ( int j = 0 ; j < n ; j ++ ){             printf ( "Enter element [ %d ][ %d ]: " , i , j );             scanf ( " %d " , & matrix [ i ][ j ]);         }     }     // Print the original matrix     printf ( " \n Original Matrix: \n " );     for ( int i = 0 ; i < m ; i ++ ) {         for ( int j = 0 ; j < n ; j ++ ) {         ...

Swap Without 3rd Variable

  #include <stdio.h> int main () {     int a = 4 , b = 5 ;     a = a + b ;     b = a - b ;     a = a - b ;     printf ( " %d %d " , a , b ); }

Swap Using Pointer

  #include <stdio.h> void swap ( int * x , int * y ) {     int temp ;     temp = * x ;     * x = * y ;     * y = temp ; } int main () {     int num1 , num2 ;     printf ( "Enter value of num1: " );     scanf ( " %d " , & num1 );     printf ( "Enter value of num2: " );     scanf ( " %d " , & num2 );     printf ( "Before Swapping num1 is: %d , num2 is: %d \n " , num1 , num2 );     swap ( & num1 , & num2 );     printf ( "After Swapping: num1 is: %d , num2 is: %d \n " , num1 , num2 );     return 0 ; }

Reverse String

  #include <stdio.h> #include <string.h> int main (){     char c [ 10 ];     fgets ( c , sizeof ( c ), stdin );     int length = strlen ( c );     for ( int i =-- length ; i >= 0 ; i -- ){         printf ( " %c " , c [ i ]);     } }

To Reverse Number

  #include <stdio.h> int main () {     int n , rev = 0 ;     printf ( "Enter no.: " );     scanf ( " %d " , & n );     while ( n != 0 ){         rev = rev * 10 + n % 10 ;         n = n / 10 ;     } printf ( " %d " , rev );     return 0 ; }

Star Pyramid

  #include <stdio.h> int main () {     int rows ;     printf ( "Enter the no. of rows: " );     scanf ( " %d " , & rows );     for ( int i = 1 ; i <= rows ; i ++ ){         for ( int j = 1 ; j <= rows - i ; j ++ ){             printf ( " " );         }         for ( int k = 1 ; k <= i ; k ++ ){ //for 1 3 5 stars 2*i-1             printf ( "* " );       // remove space after *         }         printf ( " \n " );     }     return 0 ; } Enter the no. of rows: 5 * * * * * * * * * * * * * * *

Small & Large Array

  #include <stdio.h> int main () {     int n ;     printf ( "Enter no. of element: " );     scanf ( " %d " , & n );     int arr [ n ];     for ( int i = 0 ; i < n ; i ++ ){         scanf ( " %d " , & arr [ i ]);     }     int max = arr [ 0 ];     int min = arr [ 0 ];     for ( int i = 0 ; i < n ; i ++ ){         if ( max < arr [ i ]){             max = arr [ i ];         }         if ( min > arr [ i ]){             min = arr [ i ];         }     } printf ( " %d is max and %d is min" , max , min );         return 0 ; }

Pascal Triangle

  #include <stdio.h> int main () {     int rows , i , j ;     printf ( "Enter the number of rows: " );     scanf ( " %d " , & rows );     for ( i = 0 ; i < rows ; i ++ ){         for ( j = 0 ; j < rows - i - 1 ; j ++ ) {             printf ( "  " );         }         int val = 1 ;         for ( j = 0 ; j <= i ; j ++ ) {             printf ( " %4d " , val );             val = val * ( i - j ) / ( j + 1 );         }         printf ( " \n " );     }     return 0 ; } Enter the number of rows: 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

Floyd Triangle

  #include <stdio.h> int main () {     int rows ;     int n = 1 ;     printf ( "Enter the no. of rows: " );     scanf ( " %d " , & rows );     for ( int i = 1 ; i <= rows ; i ++ ){         for ( int j = 1 ; j <= i ; j ++ ){             printf ( " %d " , n );             n ++ ;         }         printf ( " \n " );     }     return 0 ; } Enter the no. of rows: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Fibonacci

  #include <stdio.h> int main () {     int num ;     printf ( "Enter no.: " );     scanf ( " %d " , & num );     int a = 0 , b = 1 ;     int c = 0 ;     if ( num > 0 ) printf ( " %d " , a );     if ( num > 1 ) printf ( " %d " , b );     for ( int i = 2 ; i <= num ; i ++ ){         c = a + b ;         printf ( " %d " , c );         a = b ;         b = c ;     }     return 0 ; }

Factorial

  #include <stdio.h> int main () {     int n ;     printf ( "Enter no.: " );     scanf ( " %d " , & n );     int prod = 1 ;     for ( int i = 1 ; i <= n ; i ++ ){         prod = prod * i ;     } printf ( " %d " , prod );     return 0 ; }

To print even odd in pyramid

  #include <stdio.h> int main () {     int rows ;     int n = 2 ;     int p = 1 ;     printf ( "Enter the no. of rows: " );     scanf ( " %d " , & rows );     for ( int i = 1 ; i <= rows ; i ++ ){         for ( int j = 1 ; j <= rows - i ; j ++ ){             printf ( " " );         }         if ( i % 2 == 0 ){             for ( int j = 1 ; j <= i ; j ++ ){             printf ( " %d " , n );             n += 2 ;             }         } else {             for ( int j = 1 ; j <= i ; j ++ ){             printf ( " %d " , p );             p += 2 ;         ...

To print ASCII value

  #include <stdio.h> int main () {     char c ;     printf ( "Enter char: " );     scanf ( " %c " , & c );     printf ( "ASCII value is: %d " , c );     return 0 ; }

To check Sign using Switch case

  #include <stdio.h> int main () {     int num ;     printf ( "Enter no.: " );     scanf ( " %d " , & num );     switch ( num > 0 ){         case 1 :             printf ( " %d is Positive no." , num );             break ;         case 0 :             switch ( num < 0 ){                 case 1 :                     printf ( " %d is Negative no." , num );                     break ;                 case 0 :                     printf ( " %d is Zero" , num );                     break ;         ...

Palindrome

  #include <stdio.h> int main () {     int n , rev = 0 ;     printf ( "Enter no.: " );     scanf ( " %d " , & n );     int n2 = n ;     while ( n2 != 0 ){         rev = rev * 10 + n2 % 10 ;         n2 = n2 / 10 ;     }     if ( rev == n ) printf ( "Yes Palindrome" );     else printf ( "Not a Palindrome" );     return 0 ; }

Leap Year

  #include <stdio.h> int main () {     int year ;     printf ( "Enter a year: " );     scanf ( " %d " , & year );     if (( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) {         printf ( " %d is a leap year. \n " , year );     } else {         printf ( " %d is not a leap year. \n " , year );     }     return 0 ; }

Structure Basic Details

  #include <stdio.h> struct Student {     char name [ 50 ];         int roll_number ;       float marks ;     }; int main () {     struct Student s1 ;     printf ( "Enter student name: " );     fgets ( s1 . name , 50 , stdin );     printf ( "Enter roll number: " );     scanf ( " %d " , & s1 . roll_number );     printf ( "Enter marks: " );     scanf ( " %f " , & s1 . marks );     // Print the student's details     printf ( " \n --- Student Details --- \n " );     printf ( "Name: %s \n " , s1 . name );     printf ( "Roll Number: %d \n " , s1 . roll_number );     printf ( "Marks: %.2f \n " , s1 . marks );     return 0 ; }

Selection Sort

  #include <stdio.h> int main () {     int data [] = { 64 , 25 , 12 , 22 , 11 };     int n = sizeof (data) / sizeof ( data [ 0 ]);     int i, j, min_idx, temp;     printf ( "Original Array: " );     for (i = 0 ; i < n; i ++ ) {         printf ( " %d " , data [i]);     }     printf ( " \n " );     for (i = 0 ; i < n - 1 ; i ++ ) {         min_idx = i;         for (j = i + 1 ; j < n; j ++ ) {             if ( data [j] < data [min_idx]) {                 min_idx = j;             }         }         if (min_idx != i) {             temp = data [i];             data [i] = data [min_idx];      ...

Merge Array

 #include <stdio.h> int main() {     int arr1[] = {1, 2, 3};     int size1 = sizeof(arr1) / sizeof(arr1[0]);     int arr2[] = {4, 5, 6, 7};     int size2 = sizeof(arr2) / sizeof(arr2[0]);     int mergedSize = size1 + size2;     int mergedArr[mergedSize];     for (int i=0;i<size1;i++) {         mergedArr[i] = arr1[i];     }     for (int i=0;i<size2;i++){         mergedArr[size1+i] = arr2[i];     }     printf("Merged array: ");     for (int i=0;i<mergedSize;i++){         printf("%d ", mergedArr[i]);     }     printf("\n");     return 0; }

Linear search

 #include <stdio.h> int main() {     int n;     printf("Enter no. of element: ");     scanf("%d",&n);     int arr[n];     for(int i=0;i<n;i++){         scanf("%d",&arr[i]);     }     int check=0;     int target;     printf("Enter no. to search: ");     scanf("%d",&target);     for(int i=0;i<n;i++){         if(target==arr[i]){             check=1;             break;         }     }if(check){         printf("found");     }else         printf("not found");     return 0; }

Bubble sort

 #include <stdio.h> int main() {     int a[50]={4,17,9,12,5};     int i, j, temp;     for(i=0;i<5;i++){         for(j=0;j<4-i;j++){             if(a[j]>a[j+1]){                 temp=a[j];                 a[j]=a[j+1];                 a[j+1]=temp;             }         }     }     for(i=0;i<5;i++){         printf("%d ",a[i]);     }     return 0; }

binary search

 #include <stdio.h> int main() {     int arr[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};     int size = sizeof(arr) / sizeof(arr[0]);     int target = 30;     int low = 0;     int high = size - 1;     int mid;     int index = -1;     while (low <= high) {         mid = low + (high - low) / 2;         // Check if the target is exactly the middle element         if (arr[mid] == target) {             index = mid;             break;         }         // If the target is greater than the middle element, ignore the left half         if (arr[mid] < target) {             low = mid + 1;         }         // If the target is less than the middle element, ...