Find Largest & Second Largest Number -C Program

Program:
#include<stdio.h>
//#include<conio.h>
//void
main()
{
    int i,l,a[100],sl,n;
    printf("Enter the limit: ");
    scanf("%d",&n);
    printf("Enter the elements:\n");
    for(i=0;i<n;i++)
    {
           scanf("%d",&a[i]);
    }
    l=a[0];
    sl=a[1];
    for(i=0;i<n;i++)
    {
        if(a[i]>l)
        {
            sl=l;
            l=a[i];
        }
    }
    for(i=0;i<n;i++)
    {
        if(a[i]>sl && a[i]!=l)
        {
            sl=a[i];
        }
    }
    printf("largest=%d\n",l);
    printf("second largest=%d\n",sl);
//    getch();
}

Output:
nn@linuxmint ~ $ gcc c5.c
nn@linuxmint ~ $ ./a.out
Enter the limit: 5
Enter the elements:
5
4
3
2
1
largest=5
second largest=4
nn@linuxmint ~ $

Generation of Fibonacci Series -C Program

Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
    int a[200];int i,n,t1=1,t2=1,t3,x,y;//clrscr();
    a[1]=1;a[2]=1;
    printf("Enter the no of terms:");
    scanf("%d",&n);
    if(n<=0)
        printf("Error..!");
    else if(n==1)
        printf("The fibonacci series:\n1");
    else if(n==2)
        printf("The fibonacci series:\n1,1");
    else
        printf("The fibonacci series:\n1,1");
        for(i=3;i<=n;i++)
            {
                t3=t1+t2;
                t1=t2;
                t2=t3;
                printf(",%d",t2);
                a[i]=t3;
            }
    printf(",...\n");
    //getch();
}

Output:
nn@linuxmint ~ $ gcc c4.c
nn@linuxmint ~ $ ./a.out
Enter the no of terms:5
The fibonacci series:
1,1,2,3,5,...
nn@linuxmint ~ $

Towers of Hanoi - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void hanoi(int,int,int,int);
void main()
{
    int no_of_disks, firstpeg=1,secondpeg=2,thirdpeg=3;//clrscr();;
    printf("Enter the no. of disks:");
    scanf("%d",&no_of_disks);
    printf("\n\n Tower of Hanoi problem with %d disks. \n\n", no_of_disks);
    hanoi(no_of_disks,firstpeg,secondpeg,thirdpeg);
    printf("\nSolved...\n");//getch();
}

void hanoi(int n,int peg1,int peg2,int peg3)
{
    if(n!=0)
    {
    hanoi(n-1,peg1,peg3,peg2);
    printf("move disk %d from peg%d to peg%d \n",n,peg1,peg3);
    hanoi(n-1,peg2,peg1,peg3);
    }
}

Output:
nn@linuxmint ~ $ gcc c21.c
nn@linuxmint ~ $ ./a.out
Enter the no. of disks:3


 Tower of Hanoi problem with 3 disks. 

move disk 1 from peg1 to peg3 
move disk 2 from peg1 to peg2 
move disk 1 from peg3 to peg2 
move disk 3 from peg1 to peg3 
move disk 1 from peg2 to peg1 
move disk 2 from peg2 to peg3 
move disk 1 from peg1 to peg3 

Solved...
nn@linuxmint ~ $

Generation of Prime Numbers - C Program

Program:
#include<stdio.h>
//#include<math.h>
//#include<conio.h>
main()
{
    int i,a,b,j,flag=1;//clrscr();
    printf("\nEnter the limit:\n");
    scanf("%d%d",&a,&b);
    for(i=a;i<=b;i++)
    {
        if (i==0||i==1)
            continue;
        flag=1;
        for(j=2;j<=(i/2);j++) //sqrt(i) can be used.
        {
            if(i%j==0)
            {
                flag=0;
            }
        }
        if(flag==1)
        {
             printf("%d\t",i);
        }
    }
    // getch();
     printf("\n");
}

Output:
nn@linuxmint ~ $ gcc c3.c
nn@linuxmint ~ $ ./a.out

Enter the limit:
1
10
2    3    5    7   
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...