All important c programby makhan kumbhkar

sandeep kumbhkar
sandeep kumbhkarAsst Prof. en Christian Eminiant College indore
C programming codes



         Prof Makhan kumbhkar
         Christian Eminent
         College Indore(mp)
Leap year
   #include <stdio.h>

   main()
   {
     int year;

       printf("Enter a year to check if it is a leap yearn");
       scanf("%d", &year);

       if ( year%400 == 0)
          printf("%d is a leap year.n", year);
       else if ( year%100 == 0)
          printf("%d is not a leap year.n", year);
       else if ( year%4 == 0 )
          printf("%d is a leap year.n", year);
       else
          printf("%d is not a leap year.n", year);

       return 0;
   }
add digits of number in c


   #include <stdio.h>

   main()
   {
     int n, sum = 0, remainder;

       printf("Enter an integern");
       scanf("%d",&n);

       while(n != 0)
       {
         remainder = n % 10;
         sum = sum + remainder;
         n = n / 10;
       }

       printf("Sum of digits of entered number = %dn",sum);

       return 0;
   }
Decimal to binary conversion
   #include <stdio.h>

   main()
   {
     int n, c, k;

       printf("Enter an integer in decimal number systemn");
       scanf("%d",&n);

       printf("%d in binary number system is:n", n);

       for ( c = 31 ; c >= 0 ; c-- )
       {
         k = n >> c;

           if ( k & 1 )
              printf("1");
           else
              printf("0");
       }

       printf("n");

       return 0;
   }
C code to store decimal to binary conversion in a
string


   #include <stdio.h>
   #include <stdlib.h>

   char *decimal_to_binary(int);

   main()
   {
     int n, c, k;
     char *pointer;

       printf("Enter an integer in decimal number systemn");
       scanf("%d",&n);

       pointer = decimal_to_binary(n);
       printf("Binary string of %d is: %sn", n, t);

       free(pointer);

       return 0;
   }

   char *decimal_to_binary(int n)
   {
C code to store decimal to binary conversion in a
string


       int c, d, count;
       char *pointer;

       count = 0;
       pointer = (char*)malloc(32+1);

       if ( pointer == NULL )
          exit(EXIT_FAILURE);

       for ( c = 31 ; c >= 0 ; c-- )
       {
         d = n >> c;

         if ( d & 1 )
            *(pointer+count) = 1 + '0';
         else
            *(pointer+count) = 0 + '0';

         count++;
       }
       *(pointer+count) = '0';

       return pointer;
   }
Palindrome Numbers


   Palindrome number algorithm
   1. Get the number from user.
   2. Reverse it.
   3. Compare it with the number entered by the
    user.
   4. If both are same then print palindrome
    number
   5. Else print not a palindrome number.
Palindrome number program c


   #include<stdio.h>

   main()
   {
     int n, reverse = 0, temp;

    printf("Enter a number to check if it is a palindrome or notn");
    scanf("%d",&n);

    temp = n;

    while( temp != 0 )
Palindrome number program c


       {
           reverse = reverse * 10;
           reverse = reverse + temp%10;
           temp = temp/10;
       }

       if ( n == reverse )
          printf("%d is a palindrome number.n", n);
       else
          printf("%d is not a palindrome number.n", n);

       return 0;
   }
*
     ***
    *****
      ***
          *


    printf("Enter number of rowsn");
     scanf("%d", &n);

     space = n - 1;

     for (k = 1; k <= n; k++)
     {
       for (c = 1; c <= space; c++)
        printf(" ");

         space--;

         for (c = 1; c <= 2*k-1; c++)
          printf("*");

         printf("n");
     }
Fibonacci series in c


   Fibonacci series in c programming: c program for
    Fibonacci series without and with recursion. Using
    the code below you can print as many number of
    terms of series as desired. Numbers of Fibonacci
    sequence are known as Fibonacci numbers. First
    few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc,
    Except first two terms in sequence every other term
    is the sum of two previous terms, For example 8 = 3
    + 5 (addition of 3, 5). This sequence has many
    applications in mathematics and Computer Science.
*
     ***
    *****
      ***
             *



        space = 1;

        for (k = 1; k <= n - 1; k++)
        {
          for (c = 1; c <= space; c++)
           printf(" ");

            space++;

            for (c = 1 ; c <= 2*(n-k)-1; c++)
             printf("*");

            printf("n");
        }

        return 0;
    }
Fibonacci series in c

   #include<stdio.h>

   main()
   {
     int n, first = 0, second = 1, next, c;

       printf("Enter the number of termsn");
       scanf("%d",&n);

       printf("First %d terms of Fibonacci series are :-n",n);

       for ( c = 0 ; c < n ; c++ )
       {
         if ( c <= 1 )
            next = c;
         else
         {
            next = first + second;
            first = second;
            second = next;
         }
         printf("%dn",next);
       }

       return 0;
   }
Linear search in c


   Linear search in c programming: The following code
    implements linear search ( Searching algorithm )
    which is used to find whether a given number is
    present in an array and if it is present then at what
    location it occurs.It is also known as sequential
    search. It is very simple and works as follows: We
    keep on comparing each element with the element to
    search until the desired element is found or list ends.
    Linear search in c language for multiple occurrences
    and using function.
linear search in c


   #include<stdio.h>

   main()
   {
     int array[100], search, c, number;

    printf("Enter the number of elements in arrayn");
    scanf("%d",&number);

    printf("Enter %d numbersn", number);

    for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);

    printf("Enter the number to searchn");
linear search in c


       scanf("%d",&search);

       for ( c = 0 ; c < number ; c++ )
       {
          if ( array[c] == search ) /* if required element found */
          {
             printf("%d is present at location %d.n", search, c+1);
               break;
          }
       }
       if ( c == number )
          printf("%d is not present in array.n", search);

       return 0;
   }
C program for binary search


   C program for binary search: This code implements
    binary search in c language. It can only be used for
    sorted arrays, but it's fast as compared to linear
    search. If you wish to use binary search on an array
    which is not sorted then you must sort it using some
    sorting technique say merge sort and then use
    binary search algorithm to find the desired element in
    the list. If the element to be searched is found then
    its position is printed.
C program for binary search


   #include<stdio.h>

   main()
   {
     int c, first, last, middle, n, search, array[100];

     printf("Enter number of elementsn");
     scanf("%d",&n);

     printf("Enter %d integersn", n);

     for ( c = 0 ; c < n ; c++ )
       scanf("%d",&array[c]);

     printf("Enter value to findn");
     scanf("%d",&search);

     first = 0;
     last = n - 1;
     middle = (first+last)/2;
c program to insert an element in an array




   This code will insert an element into an array, For
    example consider an array a[10] having three
    elements in it initially and a[0] = 1, a[1] = 2 and a[2] =
    3 and you want to insert a number 45 at location 1
    i.e. a[0] = 45, so we have to move elements one step
    below so after insertion a[1] = 1 which was a[0]
    initially, and a[2] = 2 and a[3] = 3. Array insertion
    does not mean increasing its size i.e array will not be
    containing 11 elements.
c program to insert an element in an array



   #include <stdio.h>

   main()
   {
     int array[100], position, c, n, value;

     printf("Enter number of elements in arrayn");
     scanf("%d", &n);

     printf("Enter %d elementsn", n);

     for ( c = 0 ; c < n ; c++ )
        scanf("%d", &array[c]);

     printf("Enter the location where you wish to insert an elementn");
     scanf("%d", &position);
c program to insert an element in an array




       printf("Enter the value to insertn");
       scanf("%d", &value);

       for ( c = n - 1 ; c >= position - 1 ; c-- )
          array[c+1] = array[c];

       array[position-1] = value;

       printf("Resultant array isn");

       for( c = 0 ; c <= n ; c++ )
           printf("%dn", array[c]);

       return 0;
   }
C program for binary search



       while( first <= last )
       {
         if ( array[middle] < search )
            first = middle + 1;
         else if ( array[middle] == search )
         {
            printf("%d found at location %d.n", search, middle+1);
            break;
         }
         else
            last = middle - 1;

         middle = (first + last)/2;
       }
       if ( first > last )
          printf("Not found! %d is not present in the list.n", search);

       return 0;
   }
c program for bubble sort
:

   c program for bubble sort: c programming
    code for bubble sort to sort numbers or
    arrange them in ascending order. You can
    easily modify it to print numbers in
    descending order.
c program for bubble sort


   /* Bubble sort code */

   #include<stdio.h>

   main()
   {
     int array[100], n, c, d, swap;

     printf("Enter number of elementsn");
     scanf("%d", &n);

     printf("Enter %d integersn", n);

     for ( c = 0 ; c < n ; c++ )
        scanf("%d", &array[c]);

     for ( c = 0 ; c < ( n - 1 ) ; c++ )
     {
c program for bubble sort


           for ( d = 0 ; d < n - c - 1 ; d++ )
           {
              if ( array[d] > array[d+1] ) /* For decreasing order use < */
              {
                 swap = array[d];
                 array[d] = array[d+1];
                 array[d+1] = swap;
              }
           }
       }

       printf("Sorted list in ascending order:n");

       for ( c = 0 ; c < n ; c++ )
          printf("%dn", array[c]);

       return 0;
   }
insertion sort in c


   Insertion sort in c: c program for insertion sort
    to sort numbers. This code implements
    insertion sort algorithm to arrange numbers
    of an array in ascending order. With a little
    modification it will arrange numbers in
    descending order.
insertion sort in c


   * insertion sort ascending order */

   #include <stdio.h>

   int main()
   {
     int n, array[1000], c, d, t;

    printf("Enter number of elementsn");
    scanf("%d", &n);

    printf("Enter %d integersn", n);

    for (c = 0; c < n; c++) {
      scanf("%d", &array[c]);
    }

    for (c = 1 ; c <= n - 1; c++) {
insertion sort in c


           d = c;

           while ( d > 0 && array[d] < array[d-1]) {
            t       = array[d];
            array[d] = array[d-1];
            array[d-1] = t;

               d--;
           }
       }

       printf("Sorted list in ascending order:n");

       for (c = 0; c <= n - 1; c++) {
         printf("%dn", array[c]);
       }

       return 0;
   }
selection sort in c


   Selection sort in c: c program for selection
    sort to sort numbers. This code implements
    selection sort algorithm to arrange numbers
    of an array in ascending order. With a little
    modification it will arrange numbers in
    descending order.
selection sort in c


   #include<stdio.h>

   main()
   {
     int array[100], n, c, d, position, swap;

     printf("Enter number of elementsn");
     scanf("%d", &n);

     printf("Enter %d integersn", n);

     for ( c = 0 ; c < n ; c++ )
       scanf("%d", &array[c]);

     for ( c = 0 ; c < ( n - 1 ) ; c++ )
     {
       position = c;

       for ( d = c + 1 ; d < n ; d++ )
selection sort in c


           {
               if ( array[position] > array[d] )
                  position = d;
           }
           if ( position != c )
           {
              swap = array[c];
              array[c] = array[position];
              array[position] = swap;
           }
       }

       printf("Sorted list in ascending order:n");

       for ( c = 0 ; c < n ; c++ )
         printf("%dn", array[c]);

       return 0;
   }
Matrix multiplication in c


   Matrix multiplication in c language: c program to multiply
    matrices (two dimensional array), this program multiplies two
    matrices which will be entered by the user. Firstly user will
    enter the order of a matrix. If the entered orders of two matrix is
    such that they can't be multiplied then an error message is
    displayed on the screen. You have already studied the logic to
    multiply them in Mathematics. Matrices are frequently used
    while doing programming and are used to represent graph data
    structure, in solving system of linear equations and many more.
Matrix multiplication in c

   #include <stdio.h>

   int main()
   {
     int m, n, p, q, c, d, k, sum = 0;
     int first[10][10], second[10][10], multiply[10][10];

    printf("Enter the number of rows and columns of first matrixn");
    scanf("%d%d", &m, &n);
    printf("Enter the elements of first matrixn");

    for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
        scanf("%d", &first[c][d]);

    printf("Enter the number of rows and columns of second matrixn");
    scanf("%d%d", &p, &q);

    if ( n != p )
      printf("Matrices with entered orders can't be multiplied with each other.n");
    else
    {
      printf("Enter the elements of second matrixn");

     for ( c = 0 ; c < p ; c++ )
       for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);
Matrix multiplication in c


           for ( c = 0 ; c < m ; c++ )
           {
             for ( d = 0 ; d < q ; d++ )
             {
               for ( k = 0 ; k < p ; k++ )
               {
                 sum = sum + first[c][k]*second[k][d];
               }

                   multiply[c][d] = sum;
                   sum = 0;
               }
           }

           printf("Product of entered matrices:-n");

           for ( c = 0 ; c < m ; c++ )
           {
             for ( d = 0 ; d < q ; d++ )
              printf("%dt", multiply[c][d]);

               printf("n");
           }
       }

       return 0;
   }
1 de 34

Recomendados

Basic c programs updated on 31.8.2020 por
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
147 vistas11 diapositivas
Cpds lab por
Cpds labCpds lab
Cpds labpraveennallavelly08
342 vistas23 diapositivas
Practical File of C Language por
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
33.2K vistas82 diapositivas
C lab manaual por
C lab manaualC lab manaual
C lab manaualmanoj11manu
1.9K vistas36 diapositivas
C programs por
C programsC programs
C programsMinu S
8.5K vistas181 diapositivas
Data Structure using C por
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
913 vistas44 diapositivas

Más contenido relacionado

La actualidad más candente

Computer programming subject notes. Quick easy notes for C Programming.Cheat ... por
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
1.8K vistas4 diapositivas
The solution manual of c by robin por
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
1.3K vistas49 diapositivas
Core programming in c por
Core programming in cCore programming in c
Core programming in cRahul Pandit
6.2K vistas98 diapositivas
c-programming-using-pointers por
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointersSushil Mishra
9K vistas48 diapositivas
SaraPIC por
SaraPICSaraPIC
SaraPICSara Sahu
549 vistas18 diapositivas
C Programming por
C ProgrammingC Programming
C ProgrammingSumant Diwakar
4.3K vistas34 diapositivas

La actualidad más candente(20)

Computer programming subject notes. Quick easy notes for C Programming.Cheat ... por DR B.Surendiran .
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .1.8K vistas
Core programming in c por Rahul Pandit
Core programming in cCore programming in c
Core programming in c
Rahul Pandit6.2K vistas
c-programming-using-pointers por Sushil Mishra
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
Sushil Mishra9K vistas
SaraPIC por Sara Sahu
SaraPICSaraPIC
SaraPIC
Sara Sahu549 vistas
Let us C (by yashvant Kanetkar) chapter 3 Solution por Hazrat Bilal
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal28K vistas
Practical write a c program to reverse a given number por Mainak Sasmal
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal2.4K vistas
LET US C (5th EDITION) CHAPTER 2 ANSWERS por KavyaSharma65
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
KavyaSharma65573 vistas
Chapter 8 c solution por Azhar Javed
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed9.1K vistas
C Prog. - Strings (Updated) por vinay arora
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
vinay arora1.1K vistas
C faq pdf por DebiPanda
C faq pdfC faq pdf
C faq pdf
DebiPanda1.2K vistas

Destacado

week-20x por
week-20xweek-20x
week-20xKITE www.kitecolleges.com
308 vistas6 diapositivas
C programming language por
C programming languageC programming language
C programming languageAbin Rimal
1K vistas29 diapositivas
Palindrome number program in c por
Palindrome number program in cPalindrome number program in c
Palindrome number program in cmohdshanu
1K vistas2 diapositivas
Pointers and call by value, reference, address in C por
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
4.9K vistas6 diapositivas
Module 03 File Handling in C por
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in CTushar B Kute
7.4K vistas25 diapositivas
C lab-programs por
C lab-programsC lab-programs
C lab-programsTony Kurishingal
9.1K vistas74 diapositivas

Similar a All important c programby makhan kumbhkar

Introduction to Basic C programming 02 por
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
9.4K vistas78 diapositivas
C Programming Example por
C Programming ExampleC Programming Example
C Programming ExamplePRATHAMESH DESHPANDE
3.4K vistas6 diapositivas
Ds por
DsDs
Dskooldeep12345
393 vistas4 diapositivas
Write a program to check a given number is prime or not por
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or notaluavi
159 vistas9 diapositivas
C file por
C fileC file
C filesimarsimmygrewal
372 vistas26 diapositivas
C lab por
C labC lab
C labrajni kaushal
691 vistas34 diapositivas

Similar a All important c programby makhan kumbhkar(20)

Introduction to Basic C programming 02 por Wingston
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston9.4K vistas
Write a program to check a given number is prime or not por aluavi
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
aluavi159 vistas
PCA-2 Programming and Solving 2nd Sem.pdf por Ashutoshprasad27
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad2714 vistas
PCA-2 Programming and Solving 2nd Sem.docx por Ashutoshprasad27
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad272 vistas
C basics por MSc CST
C basicsC basics
C basics
MSc CST322 vistas
'C' language notes (a.p) por Ashishchinu
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
Ashishchinu6.2K vistas
design and analysis of algorithm Lab files por Nitesh Dubey
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey770 vistas
Daapracticals 111105084852-phpapp02 por Er Ritu Aggarwal
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal124 vistas
C programming codes for the class assignment por Zenith SVG
C programming codes for the class assignmentC programming codes for the class assignment
C programming codes for the class assignment
Zenith SVG103 vistas

Último

ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1} por
ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}
ANATOMY AND PHYSIOLOGY UNIT 1 { PART-1}DR .PALLAVI PATHANIA
234 vistas195 diapositivas
American Psychological Association 7th Edition.pptx por
American Psychological Association  7th Edition.pptxAmerican Psychological Association  7th Edition.pptx
American Psychological Association 7th Edition.pptxSamiullahAfridi4
74 vistas8 diapositivas
Universe revised.pdf por
Universe revised.pdfUniverse revised.pdf
Universe revised.pdfDrHafizKosar
108 vistas26 diapositivas
Narration lesson plan.docx por
Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docxTARIQ KHAN
99 vistas11 diapositivas
The Open Access Community Framework (OACF) 2023 (1).pptx por
The Open Access Community Framework (OACF) 2023 (1).pptxThe Open Access Community Framework (OACF) 2023 (1).pptx
The Open Access Community Framework (OACF) 2023 (1).pptxJisc
77 vistas7 diapositivas
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx por
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxGopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxDebapriya Chakraborty
553 vistas81 diapositivas

Último(20)

American Psychological Association 7th Edition.pptx por SamiullahAfridi4
American Psychological Association  7th Edition.pptxAmerican Psychological Association  7th Edition.pptx
American Psychological Association 7th Edition.pptx
SamiullahAfridi474 vistas
Universe revised.pdf por DrHafizKosar
Universe revised.pdfUniverse revised.pdf
Universe revised.pdf
DrHafizKosar108 vistas
Narration lesson plan.docx por TARIQ KHAN
Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docx
TARIQ KHAN99 vistas
The Open Access Community Framework (OACF) 2023 (1).pptx por Jisc
The Open Access Community Framework (OACF) 2023 (1).pptxThe Open Access Community Framework (OACF) 2023 (1).pptx
The Open Access Community Framework (OACF) 2023 (1).pptx
Jisc77 vistas
Class 10 English lesson plans por TARIQ KHAN
Class 10 English  lesson plansClass 10 English  lesson plans
Class 10 English lesson plans
TARIQ KHAN239 vistas
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively por PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 457 vistas
Are we onboard yet University of Sussex.pptx por Jisc
Are we onboard yet University of Sussex.pptxAre we onboard yet University of Sussex.pptx
Are we onboard yet University of Sussex.pptx
Jisc71 vistas
Narration ppt.pptx por TARIQ KHAN
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
TARIQ KHAN110 vistas
7 NOVEL DRUG DELIVERY SYSTEM.pptx por Sachin Nitave
7 NOVEL DRUG DELIVERY SYSTEM.pptx7 NOVEL DRUG DELIVERY SYSTEM.pptx
7 NOVEL DRUG DELIVERY SYSTEM.pptx
Sachin Nitave56 vistas
Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation) por AnshulDewangan3
 Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation) Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation)
Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation)
AnshulDewangan3275 vistas
Scope of Biochemistry.pptx por shoba shoba
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba121 vistas
The basics - information, data, technology and systems.pdf por JonathanCovena1
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdf
JonathanCovena177 vistas
11.28.23 Social Capital and Social Exclusion.pptx por mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239112 vistas
Use of Probiotics in Aquaculture.pptx por AKSHAY MANDAL
Use of Probiotics in Aquaculture.pptxUse of Probiotics in Aquaculture.pptx
Use of Probiotics in Aquaculture.pptx
AKSHAY MANDAL81 vistas

All important c programby makhan kumbhkar

  • 1. C programming codes Prof Makhan kumbhkar Christian Eminent College Indore(mp)
  • 2. Leap year  #include <stdio.h>   main()  {  int year;   printf("Enter a year to check if it is a leap yearn");  scanf("%d", &year);   if ( year%400 == 0)  printf("%d is a leap year.n", year);  else if ( year%100 == 0)  printf("%d is not a leap year.n", year);  else if ( year%4 == 0 )  printf("%d is a leap year.n", year);  else  printf("%d is not a leap year.n", year);   return 0;  }
  • 3. add digits of number in c  #include <stdio.h>   main()  {  int n, sum = 0, remainder;   printf("Enter an integern");  scanf("%d",&n);   while(n != 0)  {  remainder = n % 10;  sum = sum + remainder;  n = n / 10;  }   printf("Sum of digits of entered number = %dn",sum);   return 0;  }
  • 4. Decimal to binary conversion  #include <stdio.h>   main()  {  int n, c, k;   printf("Enter an integer in decimal number systemn");  scanf("%d",&n);   printf("%d in binary number system is:n", n);   for ( c = 31 ; c >= 0 ; c-- )  {  k = n >> c;   if ( k & 1 )  printf("1");  else  printf("0");  }   printf("n");   return 0;  }
  • 5. C code to store decimal to binary conversion in a string  #include <stdio.h>  #include <stdlib.h>   char *decimal_to_binary(int);   main()  {  int n, c, k;  char *pointer;   printf("Enter an integer in decimal number systemn");  scanf("%d",&n);   pointer = decimal_to_binary(n);  printf("Binary string of %d is: %sn", n, t);   free(pointer);   return 0;  }   char *decimal_to_binary(int n)  {
  • 6. C code to store decimal to binary conversion in a string  int c, d, count;  char *pointer;   count = 0;  pointer = (char*)malloc(32+1);   if ( pointer == NULL )  exit(EXIT_FAILURE);   for ( c = 31 ; c >= 0 ; c-- )  {  d = n >> c;   if ( d & 1 )  *(pointer+count) = 1 + '0';  else  *(pointer+count) = 0 + '0';   count++;  }  *(pointer+count) = '0';   return pointer;  }
  • 7. Palindrome Numbers  Palindrome number algorithm  1. Get the number from user.  2. Reverse it.  3. Compare it with the number entered by the user.  4. If both are same then print palindrome number  5. Else print not a palindrome number.
  • 8. Palindrome number program c  #include<stdio.h>   main()  {  int n, reverse = 0, temp;   printf("Enter a number to check if it is a palindrome or notn");  scanf("%d",&n);   temp = n;   while( temp != 0 )
  • 9. Palindrome number program c  {  reverse = reverse * 10;  reverse = reverse + temp%10;  temp = temp/10;  }   if ( n == reverse )  printf("%d is a palindrome number.n", n);  else  printf("%d is not a palindrome number.n", n);   return 0;  }
  • 10. * *** ***** *** *  printf("Enter number of rowsn");  scanf("%d", &n);   space = n - 1;   for (k = 1; k <= n; k++)  {  for (c = 1; c <= space; c++)  printf(" ");   space--;   for (c = 1; c <= 2*k-1; c++)  printf("*");   printf("n");  }
  • 11. Fibonacci series in c  Fibonacci series in c programming: c program for Fibonacci series without and with recursion. Using the code below you can print as many number of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.
  • 12. * *** ***** *** *   space = 1;   for (k = 1; k <= n - 1; k++)  {  for (c = 1; c <= space; c++)  printf(" ");   space++;   for (c = 1 ; c <= 2*(n-k)-1; c++)  printf("*");   printf("n");  }   return 0;  }
  • 13. Fibonacci series in c  #include<stdio.h>   main()  {  int n, first = 0, second = 1, next, c;   printf("Enter the number of termsn");  scanf("%d",&n);   printf("First %d terms of Fibonacci series are :-n",n);   for ( c = 0 ; c < n ; c++ )  {  if ( c <= 1 )  next = c;  else  {  next = first + second;  first = second;  second = next;  }  printf("%dn",next);  }   return 0;  }
  • 14. Linear search in c  Linear search in c programming: The following code implements linear search ( Searching algorithm ) which is used to find whether a given number is present in an array and if it is present then at what location it occurs.It is also known as sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found or list ends. Linear search in c language for multiple occurrences and using function.
  • 15. linear search in c  #include<stdio.h>   main()  {  int array[100], search, c, number;   printf("Enter the number of elements in arrayn");  scanf("%d",&number);   printf("Enter %d numbersn", number);   for ( c = 0 ; c < number ; c++ )  scanf("%d",&array[c]);   printf("Enter the number to searchn");
  • 16. linear search in c  scanf("%d",&search);   for ( c = 0 ; c < number ; c++ )  {  if ( array[c] == search ) /* if required element found */  {  printf("%d is present at location %d.n", search, c+1);  break;  }  }  if ( c == number )  printf("%d is not present in array.n", search);   return 0;  }
  • 17. C program for binary search  C program for binary search: This code implements binary search in c language. It can only be used for sorted arrays, but it's fast as compared to linear search. If you wish to use binary search on an array which is not sorted then you must sort it using some sorting technique say merge sort and then use binary search algorithm to find the desired element in the list. If the element to be searched is found then its position is printed.
  • 18. C program for binary search  #include<stdio.h>   main()  {  int c, first, last, middle, n, search, array[100];   printf("Enter number of elementsn");  scanf("%d",&n);   printf("Enter %d integersn", n);   for ( c = 0 ; c < n ; c++ )  scanf("%d",&array[c]);   printf("Enter value to findn");  scanf("%d",&search);   first = 0;  last = n - 1;  middle = (first+last)/2;
  • 19. c program to insert an element in an array  This code will insert an element into an array, For example consider an array a[10] having three elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to insert a number 45 at location 1 i.e. a[0] = 45, so we have to move elements one step below so after insertion a[1] = 1 which was a[0] initially, and a[2] = 2 and a[3] = 3. Array insertion does not mean increasing its size i.e array will not be containing 11 elements.
  • 20. c program to insert an element in an array  #include <stdio.h>   main()  {  int array[100], position, c, n, value;   printf("Enter number of elements in arrayn");  scanf("%d", &n);   printf("Enter %d elementsn", n);   for ( c = 0 ; c < n ; c++ )  scanf("%d", &array[c]);   printf("Enter the location where you wish to insert an elementn");  scanf("%d", &position);
  • 21. c program to insert an element in an array   printf("Enter the value to insertn");  scanf("%d", &value);   for ( c = n - 1 ; c >= position - 1 ; c-- )  array[c+1] = array[c];   array[position-1] = value;   printf("Resultant array isn");   for( c = 0 ; c <= n ; c++ )  printf("%dn", array[c]);   return 0;  }
  • 22. C program for binary search   while( first <= last )  {  if ( array[middle] < search )  first = middle + 1;  else if ( array[middle] == search )  {  printf("%d found at location %d.n", search, middle+1);  break;  }  else  last = middle - 1;   middle = (first + last)/2;  }  if ( first > last )  printf("Not found! %d is not present in the list.n", search);   return 0;  }
  • 23. c program for bubble sort :  c program for bubble sort: c programming code for bubble sort to sort numbers or arrange them in ascending order. You can easily modify it to print numbers in descending order.
  • 24. c program for bubble sort  /* Bubble sort code */   #include<stdio.h>   main()  {  int array[100], n, c, d, swap;   printf("Enter number of elementsn");  scanf("%d", &n);   printf("Enter %d integersn", n);   for ( c = 0 ; c < n ; c++ )  scanf("%d", &array[c]);   for ( c = 0 ; c < ( n - 1 ) ; c++ )  {
  • 25. c program for bubble sort  for ( d = 0 ; d < n - c - 1 ; d++ )  {  if ( array[d] > array[d+1] ) /* For decreasing order use < */  {  swap = array[d];  array[d] = array[d+1];  array[d+1] = swap;  }  }  }   printf("Sorted list in ascending order:n");   for ( c = 0 ; c < n ; c++ )  printf("%dn", array[c]);   return 0;  }
  • 26. insertion sort in c  Insertion sort in c: c program for insertion sort to sort numbers. This code implements insertion sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
  • 27. insertion sort in c  * insertion sort ascending order */   #include <stdio.h>   int main()  {  int n, array[1000], c, d, t;   printf("Enter number of elementsn");  scanf("%d", &n);   printf("Enter %d integersn", n);   for (c = 0; c < n; c++) {  scanf("%d", &array[c]);  }   for (c = 1 ; c <= n - 1; c++) {
  • 28. insertion sort in c  d = c;   while ( d > 0 && array[d] < array[d-1]) {  t = array[d];  array[d] = array[d-1];  array[d-1] = t;   d--;  }  }   printf("Sorted list in ascending order:n");   for (c = 0; c <= n - 1; c++) {  printf("%dn", array[c]);  }   return 0;  }
  • 29. selection sort in c  Selection sort in c: c program for selection sort to sort numbers. This code implements selection sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
  • 30. selection sort in c  #include<stdio.h>   main()  {  int array[100], n, c, d, position, swap;   printf("Enter number of elementsn");  scanf("%d", &n);   printf("Enter %d integersn", n);   for ( c = 0 ; c < n ; c++ )  scanf("%d", &array[c]);   for ( c = 0 ; c < ( n - 1 ) ; c++ )  {  position = c;   for ( d = c + 1 ; d < n ; d++ )
  • 31. selection sort in c  {  if ( array[position] > array[d] )  position = d;  }  if ( position != c )  {  swap = array[c];  array[c] = array[position];  array[position] = swap;  }  }   printf("Sorted list in ascending order:n");   for ( c = 0 ; c < n ; c++ )  printf("%dn", array[c]);   return 0;  }
  • 32. Matrix multiplication in c  Matrix multiplication in c language: c program to multiply matrices (two dimensional array), this program multiplies two matrices which will be entered by the user. Firstly user will enter the order of a matrix. If the entered orders of two matrix is such that they can't be multiplied then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics. Matrices are frequently used while doing programming and are used to represent graph data structure, in solving system of linear equations and many more.
  • 33. Matrix multiplication in c  #include <stdio.h>   int main()  {  int m, n, p, q, c, d, k, sum = 0;  int first[10][10], second[10][10], multiply[10][10];   printf("Enter the number of rows and columns of first matrixn");  scanf("%d%d", &m, &n);  printf("Enter the elements of first matrixn");   for ( c = 0 ; c < m ; c++ )  for ( d = 0 ; d < n ; d++ )  scanf("%d", &first[c][d]);   printf("Enter the number of rows and columns of second matrixn");  scanf("%d%d", &p, &q);   if ( n != p )  printf("Matrices with entered orders can't be multiplied with each other.n");  else  {  printf("Enter the elements of second matrixn");   for ( c = 0 ; c < p ; c++ )  for ( d = 0 ; d < q ; d++ )  scanf("%d", &second[c][d]);
  • 34. Matrix multiplication in c   for ( c = 0 ; c < m ; c++ )  {  for ( d = 0 ; d < q ; d++ )  {  for ( k = 0 ; k < p ; k++ )  {  sum = sum + first[c][k]*second[k][d];  }   multiply[c][d] = sum;  sum = 0;  }  }   printf("Product of entered matrices:-n");   for ( c = 0 ; c < m ; c++ )  {  for ( d = 0 ; d < q ; d++ )  printf("%dt", multiply[c][d]);   printf("n");  }  }   return 0;  }