SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
To find the sum of individual digits of a given number



#include<stdio.h>

main()

{

    int n,s,p;

    clrscr();

    printf("enter the vaue for n:n");

    scanf("%d",&n);

    s=0;

    if(n<0)

    printf("The given number is not valid");

    else

     {

         while(n!=0)       /* check the given value =0 or not */

             {

                 p=n%10;

                 n=n/10;

                 s=s+p;

             }

         printf("sum of individual digits is %d",s);

     }

    getch();

}
To print the Fibonacci series for 1 to n value



Program:



#include<stdio.h>

void main()

{

    int a,b,c,n,i;

    clrscr();

    printf("enter n value");

    scanf("%d",&n);

    a=0;

    b=1;

    if(n==1)

    printf("%d",a);

    else

    if(n==2)

    printf("%d%d",a,b);

    else

    {

            printf("%d%d",a,b);

            //LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN
            ADVANCE

                for(i=2;i<n;i++)

            {

                      c=a+b;

                      printf("%d",c);

                      a=b;

                      b=c;

                }
getch();

        }

}



            To print a prime numbers up to 1 to n

#include<stdio.h>

#include<conio.h>

void main()

{

    int n,i,fact,j;

    clrscr();

    printf("enter the number:");

    scanf("%d",&n);

    for(i=1;i<=n;i++)

    {

            fact=0;

            //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.

            for(j=1;j<=i;j++)

            {

                       if(i%j==0)

                       fact++;

            }

            if(fact==2)

            printf("n %d",i);

    }

getch( );

}

            To find the roots of the quadratic equation



#include<stdio.h>
#include<math.h>

void main()

{

float a,b,c,r1,r2,d;

clrscr();

printf("Enter the values for equation:");

scanf("%f%f%f",&a,&b,&c);

/* check the condition */

if(a==0)

printf("Enter value should not be zero ");

else

{

            d=b*b-4*a*c;

            /* check the condition */

                if(d>0)

            {

                       r1=(-b+sqrt(d)/(2*a));

                          r2=(-b-sqrt(d)/(2*a));

                          printf("roots are real and unequaln");

                          printf("%fn%fn",r1,r2);

            }

                else

                if(d==0)

            {

                       r1=-b/(2*a);

                          r2=-b/(2*a);

                          printf("roots are real and equaln");

                          printf("root=%fn",r1);

                          printf("root=%fn",r2);

                }
else

                     printf("roots are imaginary");

    }

    getch();

}




         To find the GCD of two given integers by using the recursive function

#include<stdio.h>

#include<conio.h>

int gcdrecursive(int m,int n) // starting of the sub program

{

          if(n>m)

          return gcdrecursive(n,m);

           if(n==0)

           return m;

          else

          return gcdrecursive(n,m%n); // return to the main program

}

void main()

{

         int a,b,igcd;

         clrscr();

          printf("enter the two numbers whose gcd is to be found:");

          scanf("%d%d",&a,&b);

          printf("GCD of a,b is %d",gcdrecursive(a,b)); // return to the sub program

          getch();

}

         To find the GCD of two given integers by using the non recursive function

#include<stdio.h>
#include<conio.h>

#include<math.h>

int gcdnonrecursive(int m,int n)

{

        int remainder;

        remainder=m-(m/n*n);

        if(remainder==0)

        return n;

        else

        gcdnonrecursive(n,remainder);

}



void main()

{

        int a,b,igcd;

         clrscr();

        printf("enter the two numbers whose gcd is to be found:");

        scanf("%d%d",&a,&b);

        printf("GCD of %d",gcdnonrecursive(a,b));

                         getch();

}




        To determine if the given string is a palindrome or not



Program:

#include<stdio.h>

#include<string.h>
enum Boolean{false,true};

enum Boolean IsPalindrome(char string[])

{

           int left,right,len=strlen(string);

           enum Boolean matched=true;

            if(len==0)

                return 0;

                left=0;

           right=len-1;



            /* Compare the first and last letter,second & second last & so on */

            while(left<right&&matched)

                {

                              if(string[left]!=string[right])

                          matched=false;

                          else

                          {

                                     left++;

                                    right--;

                          }

           }

    return matched;

}



int main()

{

    char string[40];

    clrscr();

    printf("****Program to test if the given string is a palindrome****n");

    printf("Enter a string:");
scanf("%s",string);

    if(IsPalindrome(string))

    printf("The given string %s is a palindromen",string);

    else

    printf("The given string %s is not a palindromen",string);

    getch();

    return 0;

}




                                                  UNIT-V
UNIT-VII
Program 1:
/*Write a program Selection sort implementation using array ascending order
*/

#include<stdio.h>
int main(){

    int s,i,j,temp,a[20];

    printf("Enter total elements: ");
    scanf("%d",&s);

    printf("Enter %d elements: ",s);
    for(i=0;i<s;i++)
       scanf("%d",&a[i]);

    for(i=0;i<s;i++){
      for(j=i+1;j<s;j++){
          if(a[i]>a[j]){
             temp=a[i];
            a[i]=a[j];
            a[j]=temp;
          }
      }
    }

    printf("After sorting is: ");
    for(i=0;i<s;i++)
       printf(" %d",a[i]);

    getch();
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is: 0 4 5 7 21
Program 2:
/*Write a program bubble sort implementation using array ascending order
*/

#include<stdio.h>
int main(){

    int s,temp,i,j,a[20];

    printf("Enter total numbers of elements: ");
    scanf("%d",&s);

    printf("Enter %d elements: ",s);
    for(i=0;i<s;i++)
       scanf("%d",&a[i]);

    //Bubble sorting algorithm
    for(i=s-2;i>=0;i--){
       for(j=0;j<=i;j++){
          if(a[j]>a[j+1]){
             temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
          }
       }
    }

    printf("After sorting: ");
    for(i=0;i<s;i++)
       printf(" %d",a[i]);

    getch();
}



Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting: 0 2 6 9 11
Program 3:
/*Write a program quick sort implementation using array ascending order */

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){
 int x[20],size,i;

    printf("Enter size of the array: ");
    scanf("%d",&size);

    printf("Enter %d elements: ",size);
    for(i=0;i<size;i++)
     scanf("%d",&x[i]);

    quicksort(x,0,size-1);

    printf("Sorted elements: ");
    for(i=0;i<size;i++)
     printf(" %d",x[i]);

    return 0;
}

void quicksort(int x[10],int first,int last){
  int pivot,j,temp,i;

      if(first<last){
         pivot=first;
         i=first;
         j=last;

        while(i<j){
          while(x[i]<=x[pivot]&&i<last)
             i++;
          while(x[j]>x[pivot])
             j--;
          if(i<j){
             temp=x[i];
              x[i]=x[j];
              x[j]=temp;
          }
        }

        temp=x[pivot];
x[pivot]=x[j];
        x[j]=temp;
        quicksort(x,first,j-1);
        quicksort(x,j+1,last);

    }
}

Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
Program 4:
/*Write a program merge sort implementation using array in ascending order
*/

#include<stdio.h>
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main(){

    int merge[MAX],i,n;

    printf("Enter the total number of elements: ");
    scanf("%d",&n);

    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
       scanf("%d",&merge[i]);
    }

    partition(merge,0,n-1);

    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
       printf("%d ",merge[i]);
    }

    getch();
}

void partition(int arr[],int low,int high){

    int mid;

    if(low<high){
        mid=(low+high)/2;
        partition(arr,low,mid);
        partition(arr,mid+1,high);
        mergeSort(arr,low,mid,high);
    }
}

void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];
l=low;
    i=low;
    m=mid+1;

    while((l<=mid)&&(m<=high)){

        if(arr[l]<=arr[m]){
           temp[i]=arr[l];
           l++;
        }
        else{
           temp[i]=arr[m];
           m++;
        }
        i++;
    }

    if(l>mid){
        for(k=m;k<=high;k++){
          temp[i]=arr[k];
          i++;
        }
    }
    else{
        for(k=l;k<=mid;k++){
          temp[i]=arr[k];
          i++;
        }
    }

    for(k=low;k<=high;k++){
       arr[k]=temp[k];
    }
}


output:

Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9
Program 5:
/*Write a program insertion sort implementation using array in ascending
order */

#include<stdio.h>
int main(){

    int i,j,s,temp,a[20];

    printf("Enter total elements: ");
    scanf("%d",&s);

    printf("Enter %d elements: ",s);
    for(i=0;i<s;i++)
       scanf("%d",&a[i]);

    for(i=1;i<s;i++){
      temp=a[i];
      j=i-1;
      while((temp<a[j])&&(j>=0)){
      a[j+1]=a[j];
         j=j-1;
      }
      a[j+1]=temp;
    }

    printf("After sorting: ");
    for(i=0;i<s;i++)
       printf(" %d",a[i]);

    return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting: 0 2 3 7 9
Program 6:
/*Write a program binary search in c programming language*/

#include<stdio.h>
int main(){

    int a[10],i,n,m,c=0,l,u,mid;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order: ");
    for(i=0;i<n;i++){
       scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);

    l=0,u=n-1;
    while(l<=u){
        mid=(l+u)/2;
        if(m==a[mid]){
           c=1;
           break;
        }
        else if(m<a[mid]){
           u=mid-1;
        }
        else
           l=mid+1;
    }
    if(c==0)
        printf("The number is not found.");
    else
        printf("The number is found.");

    getch();
}

output:

Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.
Program 7:
/*Write a program for linear search in c programming language*/

#include<stdio.h>
int main(){

    int a[10],i,n,m,c=0;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements of the array: ");
    for(i=0;i<=n-1;i++){
       scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);
    for(i=0;i<=n-1;i++){
        if(a[i]==m){
           c=1;
           break;
        }
    }
    if(c==0)
        printf("The number is not in the list");
    else
        printf("The number is found");

    return 0;
}

output:

Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found

Mais conteúdo relacionado

Mais procurados

All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File Harjinder Singh
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C ProgramsKandarp Tiwari
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)vinay arora
 
C basics
C basicsC basics
C basicsMSc CST
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab filesNitesh Dubey
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structuresvinay arora
 
C Prog - Strings
C Prog - StringsC Prog - Strings
C Prog - Stringsvinay arora
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
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 SolutionHazrat Bilal
 
Double linked list
Double linked listDouble linked list
Double linked listraviahuja11
 

Mais procurados (20)

All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
C programms
C programmsC programms
C programms
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
 
C basics
C basicsC basics
C basics
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C programs
C programsC programs
C programs
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
 
Daa practicals
Daa practicalsDaa practicals
Daa practicals
 
C Programming
C ProgrammingC Programming
C Programming
 
C Prog - Strings
C Prog - StringsC Prog - Strings
C Prog - Strings
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
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
 
Double linked list
Double linked listDouble linked list
Double linked list
 
C Programming lab
C Programming labC Programming lab
C Programming lab
 

Semelhante a C and C++ Program Examples for Sorting Algorithms

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointersvinay arora
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Program flowchart
Program flowchartProgram flowchart
Program flowchartSowri Rajan
 
Practical write a c program to reverse a given number
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 numberMainak Sasmal
 
Practical write a c program to reverse a given number
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 numberMainak Sasmal
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207Syed Tanveer
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C CodeSyed Ahmed Zaki
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2Koshy Geoji
 
Write a program to check a given number is prime or not
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
 
C programming function
C  programming functionC  programming function
C programming functionargusacademy
 
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 ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 

Semelhante a C and C++ Program Examples for Sorting Algorithms (20)

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
Pnno
PnnoPnno
Pnno
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
Practical write a c program to reverse a given number
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
 
Practical write a c program to reverse a given number
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
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
C programs
C programsC programs
C programs
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
Write a program to check a given number is prime or not
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
 
C programming function
C  programming functionC  programming function
C programming function
 
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 ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 

Último

How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipKarl Donert
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxAvaniJani1
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 

Último (20)

How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenship
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptx
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 

C and C++ Program Examples for Sorting Algorithms

  • 1.
  • 2. To find the sum of individual digits of a given number #include<stdio.h> main() { int n,s,p; clrscr(); printf("enter the vaue for n:n"); scanf("%d",&n); s=0; if(n<0) printf("The given number is not valid"); else { while(n!=0) /* check the given value =0 or not */ { p=n%10; n=n/10; s=s+p; } printf("sum of individual digits is %d",s); } getch(); }
  • 3. To print the Fibonacci series for 1 to n value Program: #include<stdio.h> void main() { int a,b,c,n,i; clrscr(); printf("enter n value"); scanf("%d",&n); a=0; b=1; if(n==1) printf("%d",a); else if(n==2) printf("%d%d",a,b); else { printf("%d%d",a,b); //LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE for(i=2;i<n;i++) { c=a+b; printf("%d",c); a=b; b=c; }
  • 4. getch(); } } To print a prime numbers up to 1 to n #include<stdio.h> #include<conio.h> void main() { int n,i,fact,j; clrscr(); printf("enter the number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact=0; //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT. for(j=1;j<=i;j++) { if(i%j==0) fact++; } if(fact==2) printf("n %d",i); } getch( ); } To find the roots of the quadratic equation #include<stdio.h>
  • 5. #include<math.h> void main() { float a,b,c,r1,r2,d; clrscr(); printf("Enter the values for equation:"); scanf("%f%f%f",&a,&b,&c); /* check the condition */ if(a==0) printf("Enter value should not be zero "); else { d=b*b-4*a*c; /* check the condition */ if(d>0) { r1=(-b+sqrt(d)/(2*a)); r2=(-b-sqrt(d)/(2*a)); printf("roots are real and unequaln"); printf("%fn%fn",r1,r2); } else if(d==0) { r1=-b/(2*a); r2=-b/(2*a); printf("roots are real and equaln"); printf("root=%fn",r1); printf("root=%fn",r2); }
  • 6. else printf("roots are imaginary"); } getch(); } To find the GCD of two given integers by using the recursive function #include<stdio.h> #include<conio.h> int gcdrecursive(int m,int n) // starting of the sub program { if(n>m) return gcdrecursive(n,m); if(n==0) return m; else return gcdrecursive(n,m%n); // return to the main program } void main() { int a,b,igcd; clrscr(); printf("enter the two numbers whose gcd is to be found:"); scanf("%d%d",&a,&b); printf("GCD of a,b is %d",gcdrecursive(a,b)); // return to the sub program getch(); } To find the GCD of two given integers by using the non recursive function #include<stdio.h>
  • 7. #include<conio.h> #include<math.h> int gcdnonrecursive(int m,int n) { int remainder; remainder=m-(m/n*n); if(remainder==0) return n; else gcdnonrecursive(n,remainder); } void main() { int a,b,igcd; clrscr(); printf("enter the two numbers whose gcd is to be found:"); scanf("%d%d",&a,&b); printf("GCD of %d",gcdnonrecursive(a,b)); getch(); } To determine if the given string is a palindrome or not Program: #include<stdio.h> #include<string.h>
  • 8. enum Boolean{false,true}; enum Boolean IsPalindrome(char string[]) { int left,right,len=strlen(string); enum Boolean matched=true; if(len==0) return 0; left=0; right=len-1; /* Compare the first and last letter,second & second last & so on */ while(left<right&&matched) { if(string[left]!=string[right]) matched=false; else { left++; right--; } } return matched; } int main() { char string[40]; clrscr(); printf("****Program to test if the given string is a palindrome****n"); printf("Enter a string:");
  • 9. scanf("%s",string); if(IsPalindrome(string)) printf("The given string %s is a palindromen",string); else printf("The given string %s is not a palindromen",string); getch(); return 0; } UNIT-V
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. UNIT-VII Program 1: /*Write a program Selection sort implementation using array ascending order */ #include<stdio.h> int main(){ int s,i,j,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=0;i<s;i++){ for(j=i+1;j<s;j++){ if(a[i]>a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("After sorting is: "); for(i=0;i<s;i++) printf(" %d",a[i]); getch(); } Output: Enter total elements: 5 Enter 5 elements: 4 5 0 21 7 The array after sorting is: 0 4 5 7 21
  • 16. Program 2: /*Write a program bubble sort implementation using array ascending order */ #include<stdio.h> int main(){ int s,temp,i,j,a[20]; printf("Enter total numbers of elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); //Bubble sorting algorithm for(i=s-2;i>=0;i--){ for(j=0;j<=i;j++){ if(a[j]>a[j+1]){ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); getch(); } Output: Enter total numbers of elements: 5 Enter 5 elements: 6 2 0 11 9 After sorting: 0 2 6 9 11
  • 17. Program 3: /*Write a program quick sort implementation using array ascending order */ #include<stdio.h> void quicksort(int [10],int,int); int main(){ int x[20],size,i; printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); return 0; } void quicksort(int x[10],int first,int last){ int pivot,j,temp,i; if(first<last){ pivot=first; i=first; j=last; while(i<j){ while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j){ temp=x[i]; x[i]=x[j]; x[j]=temp; } } temp=x[pivot];
  • 18. x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } } Output: Enter size of the array: 5 Enter 5 elements: 3 8 0 1 2 Sorted elements: 0 1 2 3 8
  • 19. Program 4: /*Write a program merge sort implementation using array in ascending order */ #include<stdio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main(){ int merge[MAX],i,n; printf("Enter the total number of elements: "); scanf("%d",&n); printf("Enter the elements which to be sort: "); for(i=0;i<n;i++){ scanf("%d",&merge[i]); } partition(merge,0,n-1); printf("After merge sorting elements are: "); for(i=0;i<n;i++){ printf("%d ",merge[i]); } getch(); } void partition(int arr[],int low,int high){ int mid; if(low<high){ mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(arr,low,mid,high); } } void mergeSort(int arr[],int low,int mid,int high){ int i,m,k,l,temp[MAX];
  • 20. l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)){ if(arr[l]<=arr[m]){ temp[i]=arr[l]; l++; } else{ temp[i]=arr[m]; m++; } i++; } if(l>mid){ for(k=m;k<=high;k++){ temp[i]=arr[k]; i++; } } else{ for(k=l;k<=mid;k++){ temp[i]=arr[k]; i++; } } for(k=low;k<=high;k++){ arr[k]=temp[k]; } } output: Enter the total number of elements: 5 Enter the elements which to be sort: 2 5 0 9 1 After merge sorting elements are: 0 1 2 5 9
  • 21. Program 5: /*Write a program insertion sort implementation using array in ascending order */ #include<stdio.h> int main(){ int i,j,s,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=1;i<s;i++){ temp=a[i]; j=i-1; while((temp<a[j])&&(j>=0)){ a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; } Output: Enter total elements: 5 Enter 5 elements: 3 7 9 0 2 After sorting: 0 2 3 7 9
  • 22. Program 6: /*Write a program binary search in c programming language*/ #include<stdio.h> int main(){ int a[10],i,n,m,c=0,l,u,mid; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements in ascending order: "); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); l=0,u=n-1; while(l<=u){ mid=(l+u)/2; if(m==a[mid]){ c=1; break; } else if(m<a[mid]){ u=mid-1; } else l=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found."); getch(); } output: Enter the size of an array: 5 Enter the elements in ascending order: 4 7 8 11 21 Enter the number to be search: 11 The number is found.
  • 23. Program 7: /*Write a program for linear search in c programming language*/ #include<stdio.h> int main(){ int a[10],i,n,m,c=0; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("The number is not in the list"); else printf("The number is found"); return 0; } output: Enter the size of an array: 5 Enter the elements of the array: 4 6 8 0 3 Enter the number to be search: 0 The number is found