C Programs

* Write a Program to print "Hello World".

#include<stdio.h>
#include<conio.h>
void main()
{
     clrscr();
     printf("Hello World");
     getch();
}


* Write a program to convert Rupees (float) to paisa (int).

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

int main()
{
             float rs;  //real variable for rupees
             int ps;    // integer variable for paisa
             clrscr();

             printf("\n\n Enter rupees to convert into paisa : ");
             scanf("%f",&rs);

             ps=rs*100;

             printf("\n Paisa of given rupees is %d",ps);

             getch();

            return 0;
}

* Write program to accept number of days and print year, month and remaining days.

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

int main()
{

int day,y,m; /*day for day input & output,
y for calculate year,
m for calculate month
           
printf("Enter the number of days : ");
            scanf("%d",&day);
           
y=day/365;    //calculate years
            day%=365;   // calculate remaining days
           
m=day/30;    // calculate months
            day%=30;    // calculate remaining days
           
printf("%d years,%d months, %d days",y,m,day);
           
getch();
           
return 0;

}

 
* Write program to check whether the entered number is PRIME or not.

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

int main()
{
            int i,n,flag=0;    // declare variable i, n & flag and initialize flag with 0
            clrscr();

            printf("\n Enter the number : ");
            scanf("%d",&n);

            for(i=2;i<n;i++)
            {
                        if(n%i==0)
                        {
                                    flag=1;
                                    break;
                        }
            }

            if(flag==0)
            {
                        printf("\n %d is a Prime number",n);
            }
            else
            {
                        printf("\n %d ia not a Prime number",n);
            }

            getch();

            return 0;

}

 
* Write a program to print the multiplication table.

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

int main()
{
            int i,j,n;
            clrscr();

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

            for(i=1;i<=10;i++)
            {
                        printf("\n %d * %d = %d",n,i,i*n);
            }

            getch();

            return 0;
}

* Write a program to find factorial of the entered number.

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

int main()
{
            int i,n,f=1;
            clrscr();

            printf("\n Enter the Number : ");
            scanf("%d",&n);

            for(i=1;i<=n;i++)
            {
                  f=f*i;
            }

            printf("\n The factorial of %d is : %d",n,f);

            getch();

            return 0;
}

* Write a program that accept basic, HRA, and convergence from the user and calculate total salary.

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

int main()
{
            float basic,HRA,cnvg,totsal;
            clrscr();

            printf("\n Enter basic salary : ");
            scanf("%f",&basic);

            printf("\n Enter HRA : ");
            scanf("%f",&HRA);

            printf("\n Enter convergence : ");
            scanf("%f",&cnvg);

            HRA=(basic*HRA)/100;
            cnvg=(basic*cnvg)/100;
            totsal=basic+HRA+cnvg;

            printf("\n\n Total salary is %.2f",totsal);

            getch();

            return 0;
}

* Print the following triangle.

            a  b  c  d  e
              a  b  c  d
                a  b  c
                  a  b
                    a

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

int main()
{
            int i,n,j,k;
            clrscr();

            printf("Enter  the number : ");
            scanf("%d",&n);
            printf("\n");
            for(i=0;i<n;i++)
            {
                        for(k=1;k<=i;k++)
                        {
                                    printf(" ");
                        }
                        for(j=1;j<=n-i;j++)
                        {
                                    printf(" %c",96+j);
                        }
                        printf("\n");
            }

            getch();

return 0;
}


* Write a program that prints the following Floyd’s triangle.

            1
            2  3                                                                                                 
            4  5  6
            7  8  9  10
            11 ………..15
            .
            .
            79 …………………91

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

int main()
{
            int i,j,n,k;
            clrscr();

            printf("\n\n Enter  the number : ");
            scanf("%d",&n);

            for(i=1,k=1;i<=n;i++)
            {
                        for(j=1;j<=i;j++,k++)
                        {
                                    printf(" %d ",k);
                        }
                        printf("\n");
            }
            getch();

            return 0;
}


* Write a program to find maximum element from 1-Dimensional array.

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

int main()
{
            int a[20],n,i,max=0;  // declared the array a with size  20
            clrscr();

            printf("\n Enter  the number of elements for 1-D array : ");
            scanf("%d",&n);

            for(i=0;i<n;i++)
            {
                        printf("\n Enter element [%d] : ",i+1);
                        scanf("%d",&a[i]);
            }
            for(i=0;i<n;i++)
            {
                        if(max<a[i])
                                    max=a[i];
            }

            printf("\n Maximum element from above array inserted is : %d",max);

            getch();

            return 0;
}


* Write a program to sort given array in ascending order.

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

int main()
{
            int a[20],i,j,n,temp;
            clrscr();

            printf("\n Enter no. of elements for 1-D array : ");
            scanf("%d",&n);

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

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

            printf("\n\n Ascending order of inserted  array is : ");
            for(i=0;i<n;i++)
            {
                        printf("\n %d ",a[i]);
            }

            getch();
           
            return 0;
}


* Give the 1-D array A and B, which are sorted in ascending order. Write   a program to merge them into a single sorted array C that contains every item from arrays A and B, in ascending order.

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

int main()
{
            int a[10],b[10],c[20],n1,n2,i,j,temp,k=0;
            clrscr();

            printf(" Enter the no. of element for 1st array : ");
            scanf("%d",&n1);

            for(i=0;i<n1;i++,k++)
            {
                        printf(" Enter element [%d] : ",i+1);
                        scanf("%d",&a[i]);
                        c[k]=a[i];
            }

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

            printf("\n After sorting 1st array : ");
            for(i=0;i<n1;i++)
            {
                        printf("\n Element [%d] = %d",i+1,a[i]);
            }


            printf("\n\n Enter the no. of element for 2nd array :  ");
            scanf("%d",&n2);

            for(i=0;i<n2;i++,k++)
            {
                        printf(" Enter element [%d] : ",i+1);
                        scanf("%d",&b[i]);
                        c[k]=b[i];
            }
            for(i=0;i<n2;i++)
            {
                        for(j=i+1;j<n2;j++)
                        {
                                    if(b[i]>b[j])
                                    {
                                                temp=b[i];
                                                b[i]=b[j];
                                                b[j]=temp;
                                    }
                        }
            }

            printf("\n After sorting 2nd array : ");
            for(i=0;i<n2;i++)
            {
                        printf("\n Element [%d] = %d",i+1,b[i]);
            }
            for(i=0;i<n1+n2;i++)
            {
                        for(j=i+1;j<n1+n2;j++)
                        {
                                    if(c[i]>c[j])
                                    {
                                                temp=c[i];
                                                c[i]=c[j];
                                                c[j]=temp;
                                    }
                        }
            }

            printf("\n\n\n After combined and sorted both array :- ");
            for(i=0;i<n1+n2;i++)
            {
                        printf("\n Element [%d] = %d",i+1,c[i]);
            }
            getch();

            return 0;
}


* Write a program to add two matrices.

#include<stdio.h>
#include<conio.h>
int main()
{
            int a[5][5],b[5][5],c[5][5],i,j,m,n;
            clrscr();
            printf("Enter the no of rows:");
            scanf("%d",&m);
            printf("Enter the no of columns:");
            scanf("%d",&n);
            printf("Enter First matrix\n");

            for(i=0; i<m; i++)
            {
                        for(j=0; j<n; j++)
                                    scanf("%d",&a[i][j]);
            }
            printf("Enter Second matrix\n");

            for(i=0; i<m; i++)
            {
                        for(j=0; j<n; j++)
                                    scanf("%d",&b[i][j]);
            }
            for(i=0; i<m; i++)
            {
                        for(j=0; j<n; j++)
                                    c[i][j]=a[i][j]+b[i][j];
            }

            printf("Addition of matrix\n");
            for(i=0; i<m; i++)
            {
                        for(j=0; j<n; j++)
                                    printf("%d ",c[i][j]);
                                    printf("\n");
            }
            getch();

            return 0;
}





* W.A.P to use of operator.

#include<stdio.h>
#include<conio.h>
void main()
{
        int d,g,a,b,c,e,f;
        clrscr();
        printf(" enter any number");
        scanf("%d",&a);
        printf(" enter any number");
        scanf("%d",&b);
        c=a>>b;
        d=a<<b;
        e=a&b;
        f=a|b;
        g=a^b;
        printf("\n%d >>%d =%d",a,b,d);
        printf("\n%d <<%d =%d",a,b,c);
        printf("\n%d & %d =%d",a,b,e);
        printf("\n%d | %d =%d",a,b,f);
        printf("\n%d ^ %d =%d",a,b,g);
        getch();
}

* W.A.P to print 1 to 10.

#include<stdio.h>
#include<conio.h>
void main()
{
      int a;
      clrscr();
      a=1;
      while(a<=10)
      {
            printf("\n %d",a);
            a++;
      }
      getch();
}

* W.A.P to print even number.

#include<stdio.h>
#include<conio.h>
void main()
{
    int a,n;
    clrscr();
    printf("enter any number");
    scanf("%d",&n);

    for (a=0;a<=n;a=a+2)
    {    printf("\n\t%d",a);

    }
    getch();
}


* W.A.P to print fibonacci series.

#include<stdio.h>
#include<conio.h>
void main()
{
    int a=0,b=1,c=0,n,i;
    clrscr();
    printf("enter any n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {      a=b;
            b=c;
           c=a+b;

        printf("\n %d",c);

    }
    getch();
}


* Write a program to find string length.

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

int main()
{
            char str[100];
            int l;
            clrscr();
            printf("Enter the string:");
            gets(str);
            for(l=0; str[l]; l++);
            printf("The length of %s is -> %d",str,l);
            getch();
            return 0;
}


* Write a program to print size of int, float, double variable.

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

int main()
{
            clrscr();

            printf("Size of int is -> %d\n",sizeof(int));

            printf("Size of long is -> %d\n",sizeof(float));

            printf("Size of double is -> %d\n",sizeof(double));

            getch();

            return 0;
}


* Write a program that will read a text and count all occurrences of a particular word.

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

int main()
{
            char str[100],find[100];
            int i,j,cnt=0,flag;
            clrscr();
            printf("Enter the string :");
            gets(str);
            printf("Enter the srting that you want to find : ");
            gets(find);
            for(i=0;str[i];i++)
            {
                        flag=0;
                        if(str[i]==find[0])
                        {
                                    flag=1;
                                    for(j=1;find[j];j++)
                                    {
                                                if(str[i+j]!=find[j])
                                                {
                                                            flag=0;
                                                            break;
                                                }
                                    }
                                    if(flag==1)
                                                cnt++;
                        }
                        else
                        {

                        }
            }
            printf("%s occurs in %s %d times\n",find,str,cnt);
            getch();
            return 0;
}

* Write a program that will read a string and rewrite it in the alphabetical order. i.e. the word STRING should be written as GINRST.

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

int main()
{
            char str[100],temp;
            int i,j;
            clrscr();
            printf("Enter the string :");
            gets(str);
            printf("%s in ascending order is -> ",str);
            for(i=0;str[i];i++)
            {
                        for(j=i+1;str[j];j++)
                        {
                                    if(str[j]<str[i])
                                    {
                                                temp=str[j];
                                                str[j]=str[i];
                                                str[i]=temp;
                                    }
                        }
            }
            printf("%s\n",str);
            getch();
            return 0;
}

 
* Write a program that appends the one string to another string.

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

int main()
{
            char str[100],app[20];
            clrscr();
            printf("Enter the string :");
            gets(str);
            printf("Enter the srting that you want to append : ");
            gets(app);
            printf("The string is : %s\n",str);
            printf("The string you want to append is : %s\n",app);
            printf("The string after append is : %s\n",strcat(str,app));
            getch();
            return 0;
}

* Write a function prime that returns 1 if its argument is a prime no. and returns 0
otherwise.

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

int prime(int);

int main()
{
            int n,p;
            clrscr();
            printf("Enter a number : ");
            scanf("%d",&n);
            p=prime(n);
            if(p==1)
                        printf("%d is prime\n",n);
            else
                        printf("%d is not prime\n",n);
            getch();
            return 0;
}

int prime(int n)
{
            int i;
            for(i=2;i<n;i++)
            {
                        if(n%i==0)
                                    return 0;
            }
            return 1;
}

* Write a function which returns 1 if the given number is palindrome otherwise
returns 0.

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

int pelindrome(int);

int main()
{
            int n,p;
            clrscr();
            printf("Enter a number : ");
            scanf("%d",&n);
            p=pelindrome(n);
            if(p==1)
                        printf("%d is pelindrome",n);
            else
                        printf("%d is not pelindrome",n);
            getch();
            return 0;
}

int pelindrome(int n)
{
            char a[6],b[6];
            itoa(n,a,10);
            strcpy(b,a);
            strrev(b);
            if(strcmp(a,b)==0)
                        return 1;
            else
                        return 0;
}



* Write a function that will scan a character string passed as an argument and convert all lower-case character into their upper-case equivalent.

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

void upper(char[]);

int main()
{
            char str[20];
            clrscr();
            printf("Enter string : ");
            gets(str);
            upper(str);
            getch();
            return 0;
}

void upper(char str[20])
{
            int i;
            printf("%s in upper case is ",str);
            for(i=0;str[i];i++)
            {
                        if(str[i]>96 && str[i]<123)
                                    str[i]-=32;
            }
            printf("%s",str);
}

 
* Define a structure called cricket that will describe the following information:
Player name
Team name
Batting average
Using cricket, declare an array player with 50 elements and wire a program to read the information about all the 50 players and print a list.

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

struct cricket
{
            char nm[20],team[20];
            int avg;
};

#define total 5

int main()
{
            struct cricket player[total],temp;
            int i,j;
            clrscr();
            for(i=0;i<total;i++)
            {
                        printf("For player %d\n",i+1);
                        printf("Enter the name of player : ");
                        fflush(stdin);
                        gets(player[i].nm);
                        printf("Enter the team : ");
                        fflush(stdin);
                        gets(player[i].team);
                        printf("Enter the batting average : ");
                        fflush(stdin);
                        scanf("%d",&player[i].avg);
            }
            printf("\nTeam       Name       Average\n");
            printf("                             \n");
            for(i=0;i<total;i++)
            {
                        printf("%-10s %-10s %7d\n",player[i].team,player[i].nm,player[i].avg);
            }
            getch();
            return 0;
}


* In a program declare following structure member: name, code, age, weight and
height. Read all members of the structure for 100 persons and find list of persons
with all related data whose weight > 50 and height > 40 and print the same with
suitable format and title.


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

#define size 3

struct
{
            char nm[20],cd;
            int height,age,weight;
}s[size];

int main()
{
            int i;
            clrscr();
            for(i=0;i<size;i++)
            {
                        printf("For person %d\n",i+1);

                        printf("Enter the name : ");
                        fflush(stdin);
                        gets(s[i].nm);

                        printf("Enter the code : ");
                        flushall(.);
                        s[i].cd=getc(stdin);

                        printf("Enter the age : ");
                        fflush(stdin);
                        scanf("%d",&s[i].age);
                        printf("Enter the weight : ");
                        fflush(stdin);
                        scanf("%d",&s[i].weight);
                        printf("Enter the height : ");
                        fflush(stdin);
                        scanf("%d",&s[i].height);
            }
            printf("\n\nData having weight>50 & height>40\n");
            printf("\nName      Code Age Height Weight\n");
            printf("--------------------------------\n");
            for(i=0;i<size;i++)
            {
                        if(s[i].weight>50 && s[i].height>40)
                        printf("%-10s%-5c%3d%7d%7d\n",
s[i].nm,s[i].cd,s[i].age,s[i].height,s[i].weight);
            }
            getch();
            return 0;
}


* Write a program using pointers to read an array of integers and print its elements
in reverse order.

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

int main()
{
            int *ptr,i,n;
            clrscr();
            printf("Enter the no of elements:");
            scanf("%d",&n);
            ptr=(int *)malloc(sizeof(int)*n);
            if(ptr==NULL)
            {
                        printf("Not enough memory");
                        exit(1);
            }
            for(i=0; i<n; i++)
            {
                        printf("Enter %d element : ",i+1);
                        scanf("%d",&ptr[i]);
            }
            printf("Array in original order\n");
            for(i=0; i<n; i++)
            {
                        printf("%d\n",ptr[i]);
            }
            printf("Array in reverse order\n");
            for(i=n-1; i>=0; i--)
            {
                        printf("%d\n",ptr[i]);
            }
            getch();
            return 0;
}

 
* Write a program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen.
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct stud
{
            int rno;
            char *nm;
};
void main()
{
            struct stud *s;
            int n,i;
            FILE *fp,*fp1;
            clrscr();
            printf("Enter how many record you want to input : ");
            scanf("%d",&n);
            s=(struct stud *)malloc(n*sizeof(struct stud));
            fp=fopen("STUD.txt","w");
            for(i=0;i<n;i++)
            {
                        printf("\n\tInformation for student : %d\n",i+1);
                        printf("Enter Roll No : ");
                        scanf("%d",&s[i].rno);
                        printf("Enter Name : ");
                        fflush(stdin);
                        gets(s[i].nm);
                        fprintf(fp,"%5d  %-20s\n",s[i].rno,s[i].nm);
            }
            fclose(fp);
            fp=fopen("STUD.txt","r");
            fp1=fopen("DATA.txt","w");
            printf("\nContent of the STUD.txt file is\n");
            printf("Roll No   Name\n");
            printf("---------------------------\n");
            while(!feof(fp))
            {
                        fscanf(fp,"%5d  %20s\n",&s[i].rno,s[i].nm);
                        fprintf(fp1,"%5d  %-20s\n",s[i].rno,s[i].nm);
                        printf("%7d   %-20s\n",s[i].rno,s[i].nm);       }
            fcloseall();
            getch();
}


* Write a program to create linear linked list interactively and print out the list and total number of items in the list.


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

struct node
{
            int info;
            struct node *next;
}*head,*last,*temp,*n;

void menu();    // to select the choise
void create();  // insert at last
void display(); // to print the link list

int i;

void main()
{
            i=0;
            clrscr();
            menu();
            getch();
}

void menu()
{
            int ch;
            printf("1. Create\n");
            printf("2. Display\n");
            printf("3. Exit\n");
            printf("Enter choise : ");
            scanf("%d",&ch);
            switch(ch)
            {
                        case 1  : create();
                        case 2  : display();
                        case 3  : printf("You have choose to exit\n");
                                      getch();    exit(0);
                        default : printf("Invalid choise\n"); menu();
            }
}
void create()
{
            n= new node;
            printf("Enter the information : ");
            scanf("%d",&n->info);
            if(i==0)
            {
                        head=n;
                        i++;
            }
            else
            {
                        last->next=n;
                        i++;
            }
            last=n;
            last->next=NULL;
            menu();
}

void display()
{
            if(head==NULL)
            {
                        printf("No information in list\n");
                        menu();
            }
            temp=head;
            while(temp!=NULL)
            {
                        printf("%d->",temp->info);
                        temp=temp->next;
            }
            printf("\n");
            printf("Total number of elements in the link list are : %d\n",i);
            menu();
}






No comments:

Post a Comment