Clab Solution

Embed Size (px)

Citation preview

  • 8/6/2019 Clab Solution

    1/37

    C Lab Problem Solve

    Lab1Write the program to add two numbers entered from a

    keyboard.#include

    #includevoid main()

    {int a,b,sum=0;printf("Enter the two number:");

    scanf("%d%d",&a,&b);sum=a+b;

    printf("Sum is:%d",sum);getch();

    }

    Write a program to print sum, product and quotient of two numbers 22 and

    7.#include#include

    void main(){int a=22,b=7,sum,prodt;

    float quet;sum=a+b;

    prodt=a*b;quet=(float)a/b;

    printf("sum is:%d",sum);printf("Product is:%d",prodt);

    printf("Question is:%f",quet);getch();

    }

    Write a C Program to calculate the volume of a pool. The equation for

    determiningthe volume is Vol ume = length x width x depth. Assume that the pool has a length of 25 feet, a width

    of10 feet and depth 6 feet.#include#include

    void main(){int l=25,b=10,d=6,volume;

    volume=l*b*d;

    printf("Volume of pool %d",volume);getch();}

    Write a program to input temperature in Celsius & to print its Fahrenheit

    equivalent.#include

    #includevoid main()

    {float c,f;

    printf("Enter the temperature in centigrade");scanf("%f",&c);

    f=(5.0/9.0)*(c+32.0);printf("Temperature in farenheit is:%f",f);

    getch();}

    Write a program to find the number of digits of an integer (Hints find logarithm and add one on

    theinteger part.)#include#include

    #includevoid main()

    {

    Er.Pramod Kumar Singh Page 1

  • 8/6/2019 Clab Solution

    2/37

    C Lab Problem Solve

    int n,number;

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

    number=log10(n)+1;printf("Number of digit=%d", number);

    getch();}

    Given a 5 digit integer number. Write a program to print it in reverse order#include

    #includevoid main()

    {int n,r,rev=0;

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

    do{

    r=n%10;rev=rev*10+r;

    n=n/10;}

    while(n>0);printf("Reverse number %d",rev);

    getch();}

    Calculate the functional values as follows:

    (a) f1 = (x + y + z )3 3 3 2

    (b) f2 = (x+y) z 2xyz + e2 2 (x- y)

    (c) f3 = sin(x) + atan(y) + log(z) /* argument within sin(x) is in radians */#include#include

    #includevoid main()

    { float x,y,z,b,f1,f2,f3;

    printf("Enter value of x:");scanf("%f",&x);printf("Enter the value of y:");

    scanf("%f",&y);printf("Enter the value of z:");

    scanf("%f",&z);b=pow(x,3)+pow(y,3)+pow(z,3);

    f1=pow(b,2);f2=pow((x+y),2)-pow(z,2)-2*x*y*z+exp(x-y);

    f3=sin(x)+atan(y)+log(z); printf("f1=%f f2=%f f3=%f",f1,f2,f3);

    getch();}

    Write a program to compute:

    2.9678 x 10 + 0.876 x 10-27 -38

    F = ________________________

    7.150X 10 9.75 x 101 6 12

    #include#include

    #include

    Er. Pramod Kumar Singh Page 2

  • 8/6/2019 Clab Solution

    3/37

    C Lab Problem Solve

    void main()

    {double i;i=(2.9678*pow(10,-27)+0.876*pow(10,-38))/(7.150*pow(10,16)-9.75*pow(10,12));

    printf("%e",i);getch();

    }

    Write a program that accepts a character from the keyboard and displays it on the monitor.

    Usegetchar and putchar i/ofunction#include

    #includevoid main()

    {clrscr();char a;

    printf("Enter the char:");a=getchar();

    printf("The character is:");putchar(a);

    getch();}

    Write a program that reads various data item ( numeric, character and string ) from the key board

    anddisplay it on the monitor. Use scanf and printf i/o

    functions#include#include

    void main(){char name[2],gender;

    clrscr();int id;

    printf("Enter ur name:");scanf("%[^\n]",&name);

    fflush(stdin);printf("Enter ur Gender:");

    scanf("%c",&gender);printf("Enter Ur Id");

    scanf("%d",&id);

    printf("\nUr name is:%s",name);printf("\nUr gender is:%c",gender);printf("\nur Id is:%d",id);

    getch();}

    Write a program that reads a string from the standard input device and display it on the monitor.

    Usegets and puts i/o

    functions.#include#include

    void main(){char name[20],address[30];

    clrscr();int id;

    printf("Enter ur name:");gets(name);

    printf("Enter Ur Address:");gets(address);

    printf("\nUr name is:");puts(name);

    printf("\nUr gender is:");puts(address);

    getch();

    Er. Pramod Kumar Singh Page 3

  • 8/6/2019 Clab Solution

    4/37

    C Lab Problem Solve

    }

    Write a program that reads a lowercase character from the keyboard and display it in uppercase onthe monitor and vice versa. Use toupper and tolower library functions; include header file

    ctype.h#include#include

    #include

    //program to convert lowercase to uppercase

    /*void main(){char a,b;

    printf("Lowercase character is:");a=getchar();

    b=toupper(a);printf("Uppercase character is:%c",b);

    getch();

    } */

    //program to convert upper to lowervoid main(){char a,b;

    printf("uppercase character is:");a=getchar();

    b=tolower(a);printf("Lowercase character is:%c",b);

    getch();}

    Write a program that reads a lowercase character from the keyboard and display it in the uppercase

    on the monitor without using toupper and tolower library

    functions.#include#include

    //convert upper to lower/*

    void main(){char b,c;

    clrscr();

    printf("enter char in upper");scanf("%c",&b);c=b+'a'-'A';

    printf("lower case letter%c",c);getch();

    }*/

    void main(){char b,c;

    clrscr();printf("enter char in lower");

    scanf("%c",&b);c=b+'A'-'a';

    printf("upper case letter%c",c);getch();

    }

    Write a program to generate the following output.

    Enter your name:

    abcdEnter your roll num, age, gender: 120, 20,

    mEnter your college name: National College of Engineering

    Your name is:

    abcd

    Er. Pramod Kumar Singh Page 4

  • 8/6/2019 Clab Solution

    5/37

    C Lab Problem Solve

    Your roll num: 120Your age: 20

    Your gender:

    mYour college name: National College of Engineering

    #include

    #includevoid main()

    {char name[20],gender,cname[20];int roll,age;

    clrscr();printf("Enter ur name:");

    scanf("%[^\n]",&name);printf("Enter ur rollnum,age,Gender:");

    scanf("%d%d %c",&roll,&age,&gender);fflush(stdin);

    printf("Enter Ur College name");scanf("%[^\n]",&cname);

    printf("\nUr name is:%s",name);printf("\n\tyour rollnum:%d ",roll);

    printf("\n\t\tyour age:%d",age);printf("\n\t\t\t your gender:%c",gender);

    printf("\n Your college name:%s",cname);getch();

    }

    Lab2

    Write a program that reads a number from the keyboard and displays message the number is odd if it isodd otherwise message the number is even.#include

    #includevoid main()

    { int x;printf("enter the number");

    scanf("%d",&x);

    if(x%2==0)printf("Number %d is Even",x);else

    printf("Number %d is Odd",x);getch();

    }

    Write a program to find all possible roots of a quadratic equation:ax + bx + c =

    0;

    2

    Possible cases: (i) Roots are real and distinct

    (ii) Roots are real and equal

    (iii) Roots are complex conjugate

    #include#include

    #includevoid main()

    {float a,b,c,d,x1,x2;printf("Enter the value of a,b,c");

    scanf("%f %f

    %f",&a,&b,&c);d=b*b-4*a*c;//calculate square root of b2-4ac

    if(d==0) //calculation of real and equal{x1=-b/(2*a);

    x2=x2;

    Er. Pramod Kumar Singh Page 5

  • 8/6/2019 Clab Solution

    6/37

    C Lab Problem Solve

    printf("Roots are x1=%f x2=%f",x1,x2);

    }else if(d>0)//calculation of real and distinct

    {x1=(-b+sqrt(d))/(2*a);x2= (-b-sqrt(d))/(2*a);

    printf("Roots are x1=%f x2=%f",x1,x2);}

    else //calculation of complex conjugate{

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

    printf("The Comple eqn is %f+%fi",x1,x2);}

    getch();}

    Write a program to find whether the given 4digit number (Year) is a leap year or not leap year .#include

    #includevoid main()

    {int year;printf("Enter the year");

    scanf("%d",&year);if(year%4==0)

    {if(year%100==0){if(year%400==0)

    printf("Year is leap year");else

    printf("year isnot leap year");}

    elseprintf("Year is leap Year");

    }else

    printf("Year is not a leap year");

    getch();}

    Write a program to read 3-digits number and test whether it is an Armstrong number or not

    Armstrong.#include#include

    void main(){int i,n,r,a,b,s=0;

    printf("Enter the range say ");scanf("%d",&n);

    b=n

    ;while(b!=0)

    {r=b

    %10;s=s+r*r*r;b=b/10;

    }if(s==n

    )printf("%d is a amstrong number",n);else

    printf("%d isnot a amstrong number",n);getch();

    return;

    Er. Pramod Kumar Singh Page 6

  • 8/6/2019 Clab Solution

    7/37

    C Lab Problem Solve

    }

    Write a program to input five digit integer and print sum of digits in it.(52137=>5+2+1+3+7=18)#include

    #includevoid main()

    {long int n,a,sum=0,i;printf("Enter the number");

    scanf("%ld",&n);for(i=0;i0)

    {c=d;

    d=r;

    goto l1;}printf("hcf=%d",d);

    e=a*b/d;printf("lcm=%d",e);

    getch();return;

    }#include

    #includevoid main()

    {int a,b,r;

    printf("enter the ewo number");scanf("%d%d",&a,&b);

    do{

    r=a%b;if(r==0)

    printf("HCF=%d",b);else

    {

    Er. Pramod Kumar Singh Page 7

  • 8/6/2019 Clab Solution

    8/37

    C Lab Problem Solve

    a=b;

    b=r;}

    }while(r!=0);

    getch();}

    Write a program to read three numbers and display the followingmenu. Menu

    : 1

    .

    Summatio

    n2

    .

    Sum of

    squares3

    .

    Sum of

    cubes4

    .

    Produc

    tand perform tasks as per users choice. (Use switch statements)#include

    #includevoid main()

    {int a,b,c,n,square,cube,sum,product;

    printf("Enter the three number");scanf("%d%d%d",&a,&b,&c);

    printf("\n1.sum\n");printf("2.sum of square\n");

    printf("3.sum of cube\n");printf("4.product\n");

    printf("Enter ur choice");scanf("%d",&n);

    switch(n){

    case 1:sum=a+b+c;

    printf("Sum is:%d",sum);break;

    case 2:

    square=a*a+b*b+c*c;printf("Sum of square is:%d",square);break;

    case 3:cube=a*a*a+b*b*b+c*c*c;

    printf("Sum of cube is:%d",cube);break;

    case 4:product=a*b*c;

    printf("Product is:%d",product);break;

    default:printf("Wrong choice");

    }getch();

    }

    Write a program to read a character and to test whether it is an alphabet or a number or a

    specialcharacter.#include

    #includevoid main()

    {

    Er. Pramod Kumar Singh Page 8

  • 8/6/2019 Clab Solution

    9/37

    C Lab Problem Solve

    char a;

    printf("Enter the character:");scanf("%c",&a);

    if(a>=65&&a=97&&a=48&&a=33&&a=58&&a=91&&a=(a+b)||a>=(b+c)||b>=(c+a))printf("Wrong value");

    else{

    s=1/2(a+b+c);area=sqrt(s*(s-a)*(s-b)*(s-c));

    printf("area of triangle=%d",area);

    }getch();

    }

    Lab3

    To print table of ASCII Characters for code 32 to 255.

    #include

    #includevoid main()

    {int i;

    for (i=32;i

  • 8/6/2019 Clab Solution

    10/37

    C Lab Problem Solve

    printf("Enter the number:");

    scanf("%d",&a);while(i

  • 8/6/2019 Clab Solution

    11/37

    C Lab Problem Solve

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

    j++;}

    while(j

  • 8/6/2019 Clab Solution

    12/37

    C Lab Problem Solve

    {

    int num,i,base=1,sum=0,rem;printf("Enter the decimal Number");

    scanf("%d",&num);while(num!=0)

    { rem=num%8;sum=sum+rem*base;

    base=base*10;num=num/8;

    }printf("octal Equivalent is:%d",sum);

    getch();}

    Write a program to read x and n, and generate the following series & print the

    sum. n

    termsx + 2/x -3/x + 4/x -5/x2 3 4 5

    #include#include

    #includevoid main()

    {clrscr();

    float sum,x,n,t;printf("\Enter the value of x and n\n");

    scanf("%f %f",&x,&n);sum=x;

    printf("%f ,",x);for(int i=1;i

  • 8/6/2019 Clab Solution

    13/37

    C Lab Problem Solve

    #include

    #includevoid main()

    {clrscr();

    int i,j,n=1;for(i=0;i

  • 8/6/2019 Clab Solution

    14/37

    C Lab Problem Solve

    }

    else{

    for(i=2*n;i

  • 8/6/2019 Clab Solution

    15/37

    C Lab Problem Solve

    printf("sum is:%ld",sum);

    getch();}

    Lab4

    Program to display the following

    menu:Menu

    1. Input three

    numbers2. Summation of thenumbers3. Sum of

    squares4. Product of the three

    numbers5. Exit prom program

    and perform task as per users choice repeatedly until he/she chooses to exit.

    #include#include

    void input(void);int sum(int,int,int);

    int sumsq(int,int,int);int pro(int,int,int);

    int a,b,c;void main()

    {clrscr();

    int ch;printf("\n\t\t\tMain Menu\n");

    printf("\t\t\t===============\n");printf(\t\t\t1. Input number\n);

    printf("\t\t\t2.Sum of numbers\n");printf("\t\t\t3. Sum of square of numbers\n");

    printf("\t\t\t4. Product of numbers\n");printf("\t\t\t5. Exit\n");

    do{

    printf("\t\t\t\t\t\t\n\nEnter your choice:");

    scanf("%d",&ch);switch(ch){ case 1:

    input();break;

    case 2:

    {printf("\nSum of three numbers=%d",sum(a,b,c));

    break;}

    case 3:{

    printf("\nSum of square of three numbers=%d",sumsq(a,b,c));break;

    }case 4:

    {printf("\nProduct of three numbers=%d",pro(a,b,c));

    break;}

    }

    Er. Pramod Kumar Singh Page 15

  • 8/6/2019 Clab Solution

    16/37

    C Lab Problem Solve

    }

    while(ch!=5);getch();

    }void input(void)

    { printf("Input three numbers");scanf("%d %d %d",&a,&b,&c);

    }

    int sum(int a,int b,int c){

    return(a+b+c);}

    int sumsq(int a,int b,int c){

    return(a*a+b*b+c*c);}

    int pro(int a,int b,int c){

    return(a*b*c);}

    Menu

    1. Circumference of a Circle2. Area of cross-section of a hemisphere

    3. Surface area of a sphere4. Volume of a sphere

    5. Exit from program#include#include

    #define pi 3.1416float cf(int);

    float csh(int);float as(int);

    float vs(int);

    void main(){clrscr();

    int r,ch;printf("Input radius:");

    scanf("%d",&r);printf("\n\t\t\tMain Menu\n");

    printf("\t\t\t===============\n");printf("\t\t\t1.circumference of a circle\n");

    printf("\t\t\t2. Cross section area of hemisphere\n");printf("\t\t\t3. Area of sphere\n");

    printf("\t\t\t4. Volume of Sphere\n");printf("\t\t\t5. Exit\n");

    do{

    printf("\t\t\t\t\t\t\n\nEnter your choice:");scanf("%d",&ch);

    switch(ch){

    case 1:{

    printf("\nCircumference of a circle=%f",cf(r));

    Er. Pramod Kumar Singh Page 16

  • 8/6/2019 Clab Solution

    17/37

    C Lab Problem Solve

    break;

    }case 2:

    {printf("\nCross section area of hemisphere=%f",csh(r));

    break;}

    case 3:{

    printf("\nArea of a sphere=%f",as(r));break;

    }case 4:

    {printf("\nVolume of a sphere=%f",vs(r));

    break;}

    }}

    while(ch!=5);getch();

    }float cf(int r)

    {return(2*pi*r);

    }float csh(int r)

    {return(3*pi*r*r);

    }float as(int r)

    {return(pi*r*r);

    }

    float vs(int r){return(4.0/3*pi*r*r*r);

    }

    Write a program to calculate factorial of a positive integer using1. Programmer defined

    function2. Recursive function#include#include

    #define pi 3.1416long int fact1(int);

    long int fact2(int);void main()

    {clrscr();

    int n,ch;printf("Input a number:");

    scanf("%d",&n);printf("\n\t\t\tMain Menu\n");

    printf("\t\t\t===============\n");printf("\t\t\t1.factorial using user defined function\n");

    printf("\t\t\t2. factorial using recursive function\n");

    Er. Pramod Kumar Singh Page 17

  • 8/6/2019 Clab Solution

    18/37

    C Lab Problem Solve

    printf("\t\t\t\t\t\t\n\nEnter your choice:");

    scanf("%d",&ch);switch(ch)

    {case 1:

    {printf("\nFactorial1=%ld",fact1(n));

    break;}

    case 2:{

    printf("\nFactorila2=%ld",fact2(n));break;

    }}

    getch();}

    long int fact1(int n){

    int i;long int fact=1;

    for(i=1;i

  • 8/6/2019 Clab Solution

    19/37

    C Lab Problem Solve

    switch(ch)

    {case 1:

    {fib1(n);

    break;}

    case 2:{

    for(i=1;i

  • 8/6/2019 Clab Solution

    20/37

    C Lab Problem Solve

    #include

    #include#include

    #includeint hextodec(char str[])

    {int m,n,b,i,ss=0,s=0;

    printf("\nEnter hexadecimal number:");scanf("%s",str);

    m=strlen(str);m--;

    for(i=0;str[i]!='\0';i++){

    if(str[i]>='A'&&str[i]='0'&&str[i]=10)hex[i]=(char)(d+55);

    elsehex[i]=(char)(d+48);

    i++;dec=dec/16;

    }printf("\nHexadecimal=");

    for(j=i-1;j>=0;j--)printf("%c",hex[j]);

    }void main()

    {clrscr();

    int ch,dec;char str[20];

    printf("\n1.Hexacimal to decimal\n");printf("\n2.Decimal to Hexadecimal \n");

    printf("\n3.Exit");do

    {

    Er. Pramod Kumar Singh Page 20

  • 8/6/2019 Clab Solution

    21/37

    C Lab Problem Solve

    printf("\n\nEnter your choice:");

    scanf("%d",&ch);switch(ch)

    {case 1:

    printf("\nHexadecimal=%d",hextodec(str));break;

    case 2:dectohex(dec);

    break;}

    }while(ch!=3);

    getch();}

    Write a program to calculate the value of the given series using user defined function.

    x x 2 x3 xn1 .......... ........ ( 1)n

    1! 2! 3! n!#include

    #include

    #include

    float series(int n,float x);main()

    { int n;float x,y;

    printf("enter the value of n:");scanf("%d",&n);

    printf("enter the value of x:");scanf("%f",&x);

    y=series(n,x);printf("y=%f",y);

    getch();return(0);

    }float series(int n,float x)

    {float term=(-x),sum=1;

    int i;for(i=1;i

  • 8/6/2019 Clab Solution

    22/37

    C Lab Problem Solve

    printf("enter the value of r:");

    scanf("%d",&r);s=n-r;

    comb=fact(n)/(fact(s)*fact(r));printf("combination=%ld",comb);

    getch();return(0);

    }long int fact(int n)

    {int i;long int fact=1;

    if(n

  • 8/6/2019 Clab Solution

    23/37

    Page 23

    C Lab Problem Solve

    for(i=0;i=0;i--)

    printf("\n%d",x[i]);getch();

    }Write a program sort the numbers in either ascending or descending order using

    onedimensionalarray.#include

    #include#include

    void main(){

    clrscr();int x[10],i,j,temp,n;

    printf("\nHow many numbers?");scanf("%d",&n);

    printf("\nEnter Numbers:");for(i=0;i

  • 8/6/2019 Clab Solution

    24/37

    C Lab Problem Solve

    printf("\nEnter elements of first matrix\n");

    for(i=0;i

  • 8/6/2019 Clab Solution

    25/37

    C Lab Problem Solve

    printf("\nHow many rows ?");

    scanf("%d",&m);printf("\nHow many columns?");

    scanf("%d",&n);printf("\nEnter elements of a matrix\n");

    for(i=0;i

  • 8/6/2019 Clab Solution

    26/37

    C Lab Problem Solve

    printf("\nTransposed Matrix:\n");

    for(i=0;i

  • 8/6/2019 Clab Solution

    27/37

    C Lab Problem Solve

    Lab 6Write a program that takes three variables (a, b, c) and rotates the values stored such that value of a

    goes to b, b to c and c to a. use pointers for

    rotation.#include#include

    void main(){

    int *a,*b,*c,temp;printf("\nEnter the three number");

    scanf("%d%d%d",a,b,c);printf("\n Before rotation");

    printf("a=%d b=%d c=%d",*a,*b,*c);temp=*a;

    *a=*b;*b=*c;

    *c=temp;printf("\nAfter Swaping ");

    printf("a=%d b=%dc=%d",*a,*b,*c);getch();

    }

    Write a computer program to find the sum of all the elements of an integer array of length k using

    theconcept of pointer#include

    #include

  • 8/6/2019 Clab Solution

    28/37

    C Lab Problem Solve

    temp=*a;

    *a=*b;*b=temp;

    }

    Write a computer program to read the score of 24 students in a class using integer type of array. Findand display the average score of that class. Use the concept of function and

    pointer.#include

    #includeint score(int *a[]);

    void main(){

    int sum,average,*a[20];for(int i=0;i

  • 8/6/2019 Clab Solution

    29/37

    C Lab Problem Solve

    {

    if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')

    count[0]++;else

    count[1]++;}

    else if(isdigit(str[i])!=0)count[2]++;

    elsecount[3]++;

    }}

    Write a computer program to sort the contents of one dimensional array of length in

    descendingorder. Use the concept of both pointer and

    function.#include#include

    void sort(float *,int);void main()

    {int n,i;float *marks;

    printf("enter the number of element");scanf("%d",&n);

    printf("Enter the element");for(i=0;i

  • 8/6/2019 Clab Solution

    30/37

    C Lab Problem Solve

    output_ptr=rev+strlen(str)-1;

    for(i=0;i

  • 8/6/2019 Clab Solution

    31/37

    C Lab Problem Solve

    printf("\nreorder level");

    scanf("%d",&p1.re_order_level);printf("\nDetail of the entered parts:\n");

    printf("Partno:%d\npart_name:%s\nstock:%d\nrate:%d\nreorderlevel:%d",p1.part_no,p1.part_name,p1.stock_qt y,p1.rate,p1.re_order_level);

    getch();}

    Write a computer program that reads several different names and addresses into thecomputer,rearranges the name in alphabetical order, and then writes out the rearranged list. Make use

    ofstructures variables within the program.#include

    #include#include

    void reorder(int n, char name[][12]);struct student

    { char name[10][12];};

    void main(){clrscr();

    struct student record;int n=0,i;

    do{printf("String %d:",n+1);

    scanf("%s ",record.name[n]);}

    while(strcmp(record.name[n++],"END"));n--;

    reorder(n,record.name);for(i=0;i

  • 8/6/2019 Clab Solution

    32/37

    C Lab Problem Solve

    int marks;

    char remarks[20];};

    void main(){ clrscr();

    struct student record[10];for( int i=1;i

  • 8/6/2019 Clab Solution

    33/37

    C Lab Problem Solve

    }

    }getch();101203

    }

    Write a program that contains the customers name (char), loan_number (int) and balance (float)

    of100 customers in a bank and print the name that has the highest loan from the

    bank.#include

    #includestruct customer

    {char name[20];

    int loan_number;float balance;

    };void main()

    { int i,j;float b,*cb; //it is used to eliminate the linker error

    //floating point formats are not linked,abnormal program termination .cb=&b; //it assign the address of b into cb so that floating

    point//version of scanf can be used without error.struct customer c[100],temp;

    printf("\nEnter the detail of customer");for(i=0;i

  • 8/6/2019 Clab Solution

    34/37

    C Lab Problem Solve

    int day;

    int month;int year;

    };struct student

    {char name[20];

    struct dob sdob[100];};

    void main(){int dday,dmonth,dyear,age[100],i;

    struct date d;getdate(&d);

    struct student s[100];for(i=0;i

  • 8/6/2019 Clab Solution

    35/37

    C Lab Problem Solve

    getch();

    return(0);

    }

    Write a program to create a file Employee.dat having several data of the following format of the

    following

    Employee code, Employee name, date of birth andsalary.#include

    #includestruct dob{

    int day;int month;

    int year;};

    struct employee{

    int employee_code;char employee_name[20];

    struct dob s1;int salary;

    };

    void main(){FILE *fp;

    struct employee e;int i,n;

    fp=fopen("employee.txt","w");clrscr();

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

    printf("\n Enter the record of the employee\n(employee_code,employee name,dob(day/month/year),salary)");for(i=0;i

  • 8/6/2019 Clab Solution

    36/37

    C Lab Problem Solve

    if(fs==NULL)

    {puts("Cannot open source file");

    exit(1);}

    ft=fopen("xyz.txt","w");if(ft==NULL)

    {puts("cannot open target file");

    fclose(fs);exit(1);

    }while(1)

    {ch=fgetc(fs);

    if(ch==EOF)break;

    elsefputc(ch,ft);

    }fclose(fs);

    fclose(ft);getch();

    }

    Write a program to open a new file. Read the name, address and telephone no. of ten persons from

    theuser and write them to the file. After writing display the content of the

    file.#include#include

    void main(){

    FILE *fp1;char name[20][10],address[20][10];

    long int telno[10];

    int i;fp1=fopen("4.txt","w");for(i=1;i

  • 8/6/2019 Clab Solution

    37/37

    C Lab Problem Solve

    Write a program to open a new file ,read roll number ,name address and phone numberof

    studentsuntil the user says no, after reading all the data write it to the file.And display the records from

    thefile in alphabetical order of students

    name.

    A file Elect.dat contains the consumers data; custom_no, old and new meter reading . Write

    aprogram to print electricity bills according to the following policy.

    Units Consumed Rate of

    charge0-200 Rs 4.50 perunit201-400 Rs 5.50 per

    unit401-above Rs 7.50 per

    unit

    Er. Pramod Kumar Singh Page 37