Showing posts with label for loop. Show all posts
Showing posts with label for loop. Show all posts

Monday, September 12, 2011

Selection Sort


//                    -: Selection Sort :-
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,n,a[10],temp;
    clrscr();
    printf("\nEnter the number of terms:");
    scanf("%d",&n);
    printf("\n Enter the elements:");
    for(i=0;i<n;i++)
    {
        printf("\n a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
            if(a[i]>a[j])
            {
temp=a[j];
                a[j]=a[i];
                a[i]=temp;
            }
    for(i=0;i<n;i++)
        printf("\n %d",a[i]);
    getch();
}

convert decimal number into binary

// to convert decimal  number into binary
#include<stdio.h>
#include<conio.h>
main()
{
    int n,a[25],i=0,j;
    clrscr();
    printf("\n Enter any integer : ");
    scanf("%d",&n);
    while(n>0)
    {
        a[i]=n%2;
        n=n/2;
        i++;
    }
    for(j=i-1;j>=0;j--)
      printf(" %d ",a[j]);
       getch();
}

Program for prime number

#include<conio.h>
#include<time.h>
#include<dos.h>
#include<math.h>
#include<iostream.h>
long isprime( long n){
long no =n;
long sw=0;
for(long i = 2;i<(sqrt(n)+1);i++){
//for(long i = 2;i<n;i++){
if(no%i==0){
sw =1;
break;
}}
if(sw==1)
return 0;
return 1;
}

void main(){
clrscr();
 time_t t1 , t2;
time(&t1);
long l,u;
cout<<"\n Enter lower range : ";
cin>>l;
cout<<"\n Enter upper range : ";
cin>>u;
cout<<"\n From "<<l<<" to "<<u<<" the following numbers are prime : \n";
for(long i=l;i<=u;i++){
if(isprime(i))
cout<<i<<"\t";
}
time(&t2);

cout<<"The difference is "<<difftime(t2,t1);
getch();
}

Popular 5 Posts Last 7 days