CP lab ex only pgms

Embed Size (px)

Citation preview

  • 8/10/2019 CP lab ex only pgms

    1/28

    INTEGER TO FLOAT CONVERSION

    PROGRAM

    #include

    void main()

    {

    float b;

    int a;

    printf("\nEnteranInteger\n");

    scanf("%d",&a);

    b=(float)a;

    printf("\nTheConvertedfloatvalue is%f",b);

    }

    OUTPUT

    Enter an Integer 45

    The Converted float value is 45.00000

    RESULT

    Thus the c program was written,entered, executed successfullyand the output

    was verified

  • 8/10/2019 CP lab ex only pgms

    2/28

    AREA OF CIRCLE

    PROGRAM

    #include

    void main()

    {

    float r,area;

    printf(\nEnter the radius of the circle);

    scanf(%f,&r);

    area=3.14*r*r;printf(\nArea=%f,area);

    }

    OUTPUT

    Enter the radius of the circle 4

    Area = 50.24

    RESULT

    Thus the c program was written,entered, executed successfullyand the output

    was verified

  • 8/10/2019 CP lab ex only pgms

    3/28

    TEMPERATURE CONVERSION

    PROGRAM

    #include

    void main()

    {

    floatcel, fah ,c ,f;

    clrscr();

    printf(\nEnter the Fahrenheit value:);

    scanf(%f,&f);

    cel=(1.8)*(f-32);

    printf(Celsius=%f,cel);

    printf(\nEnter the Celsius value:);

    scanf(%f,&c);

    fah=(1.8)*c+32;

    printf(Fahrenheit=%f,fah);

    getch();

    }

    OUTPUT

    Enter the Fahrenheit value:8

    Celsius = -13.33

    Enter the Celsius value:10

    Fahrenheit = 50

    RESULT

    Thus the c program was written,entered, executed successfully and the output

    was verified

  • 8/10/2019 CP lab ex only pgms

    4/28

    EVALUATE THE GIVEN EXPRESSION

    PROGRAM

    #include

    void main()

    {

    int a,b,c;

    float x,y,z;

    printf ("Enter the values for a,b,c \n");

    scanf("%d%d%d", &a,&b,&c);x = (a * b)c;

    y = (b/c) * a;

    z = (a - b) / c

    printf(The value of x is %f,x);

    printf(The value of y is %f,y);

    printf(The value of z is %f,z);

    }

    OUTPUT

    Enter the value for a,b,c.

    a= 5; b = 6; c= 12;

    x = 28.000

    y = 2.5000

    z = 0.0555

    RESULT

    Thus the c program was written,entered, executed successfully and the output

    was verified

  • 8/10/2019 CP lab ex only pgms

    5/28

    DECISION MAKING & LOOPING, SCIENTIFIC PROBLEMS

    Find the Given year is Leap Year or Not (using if else)

    PROGRAM

    #include

    void main()

    {

    int year;

    printf ("Enter the year \n");

    scanf("%d", &year);

    if (year%4==0)

    printf ("It is a Leap Year \n");

    else

    printf ("It is Not a Leap Year\n");

    }

    OUTPUT

    Enter the year 2004

    It is a Leap Year

    Enter the year 1998

    It is not a Leap Year

    RESULT

    Thus the c program was written, entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    6/28

    Largest of three numbers (using nested if else)

    PROGRAM

    #include

    #include

    void main()

    {

    inta,b,c;

    clrscr();

    printf(Biggest of three Nos);

    printf(Enter the values of A,B,C);scanf(%d%d%d,&a,&b,&c);

    if((a>b)&&(b>c))

    printf(\n a=%d is greatest,a);

    elseif(b>c)

    {

    printf(\n b=%d is greatest,b);

    }

    else

    {

    printf(\n c=%d is greatest,c);

    }

    getch();

    }

    OUTPUT

    Enter the values of A,B,C:10 20 5

    B = 20 is greatest

    RESULT

    Thus the c program was written,entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    7/28

    SUMOFN NATURAL NUMBERS (using while)

    PROGRAM

    #include

    void main()

    {

    int i,n,sum=0;

    printf("Enter the range\n");

    scanf("%d",&n);

    i=1;while(i

  • 8/10/2019 CP lab ex only pgms

    8/28

    SUM OF DIGITS (using do while)

    PROGRAM

    #include

    void main()

    {

    int n,i,sum=0;

    printf("Enter a Number\n");

    scanf("%d",&n);

    do

    {i=n%10;

    sum=sum+i;

    n=n/10;

    }

    while(n>0);

    printf("The Sumof digitis%d\n",sum);

    }

    OUTPUT

    Enter a Number

    5891

    The Sum of digitis 23

    RESULT

    Thus the c program was written,entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    9/28

    ARITHMETIC OPERATIONS (Using SwitchCase)

    PROGRAM

    #include

    #include

    void main()

    {

    int a, b, c, n;

    clrscr();

    printf(1. Addition\n);

    printf(2. Subtraction\n);printf(3. Multiplication\n);

    printf(4. Division\n);

    printf(0. Exit\n);

    printf(Enter your choice : );

    scanf(%d,&n);

    printf(Enter the two numbers :);

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

    switch(n)

    {

    case 1:

    c = a + b;

    printf(Addition :%d\n,c);

    break;

    case 2:

    c = ab;

    printf(Subtraction :%d\n,c);

    break;

    case 3:

    c = a * b;

    printf(Multiplication :%d\n,c);

    break;

  • 8/10/2019 CP lab ex only pgms

    10/28

    case 4:

    c = a / b;

    printf(Division :%d\n,c);

    break;case 0:

    exit(0);

    break;

    }

    getch();

    }

    OUTPUT

    1. Addition

    2. Subtraction

    3. Multiplication

    4. Division

    0. Exit

    Enter Your Choice : 1

    Enter the two nos a and b: 2 8

    Addition : 10.

    Enter Your Choice : 2

    Enter the two nos a and b: 5 2

    Subtraction : 3.

    Enter Your Choice : 3

    Enter the two nos a and b: 2 8

    Multiplication : 16.Enter Your Choice : 4.

    Enter the two nos a and b: 8 4

    Division : 2.

    Enter Your Choice : 0.

    Exit.

    RESULT

    Thus the c program was written,entered, executed successfullyand the output wasverified

  • 8/10/2019 CP lab ex only pgms

    11/28

    GENERATION OF ARMSTRONG NUMBERS

    PROGRAM

    #include

    void main(){

    int r;

    long number = 0, c, sum = 0, temp;

    printf("Enter an integer upto which you want to find armstrong numbers\n");

    scanf("%ld",&number);

    printf("Following armstrong numbers are found from 1 to %ld\n",number);

    for( c = 1 ; c

  • 8/10/2019 CP lab ex only pgms

    12/28

    ONE DIMENSIONAL ARRAY, TWO DIMENSIONAL ARRAY

    ONE DIMENSIONAL ARRAY

    PROGRAM

    #include

    #include

    void main()

    {

    int a[3],i;

    clrscr();

    printf("\n\t Enter threenumbers : ");for(i=0; i

  • 8/10/2019 CP lab ex only pgms

    13/28

    TWODIMENSIONAL ARRAY-MATRIX MULTIPLICATION

    PROGRAM

    #include

    #include

    void main()

    {

    int a[3][3], b[3][3], c[3][3], i, j, k;

    clrscr();

    printf("\nENTER 'A' MATRIX\n");

    for(i=0;i

  • 8/10/2019 CP lab ex only pgms

    14/28

    {

    c[i][j]=0;

    }

    for(k=0;k

  • 8/10/2019 CP lab ex only pgms

    15/28

    PROBLEMS UNDER STRING FUNCTIONS

    Solving problems using String functions

    PROGRAM

    #include

    #include

    int main()

    {

    char a[100], b[100];

    printf("Enter the first string\n");

    gets(a);printf("Enter the second string\n");

    gets(b);

    if(strcmp(a,b) == 0 )

    printf("Entered strings are equal.\n");

    else

    printf("Entered strings are not equal.\n");

    return 0;

    }

    OUTPUT

    Enter the first string

    SVS

    Enter the second string

    SVS College of Engineering

    Entered the strings are not equal

    RESULT

    Thus the c program was written, entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    16/28

    STRINGFUNCTIONS-CONCATENATE TWO STRINGS

    PROGRAM

    #include

    #include

    void main()

    {

    char s1[20], s2[20]; \

    printf("\nEnter first string:");

    gets(s1);printf("\nEnter second string: ");

    gets(s2);

    strcat(s1, s2);

    printf("\nThe concatenated string is:%s", s1);

    getch();

    }

    OUTPUT

    Enter first string: College of

    Enter second string: Engineering

    The concatenated string is: College ofEngineering

    RESULT

    Thus the c program was written, entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    17/28

    STRING FUNCTIONS-COPIESTHE STRING

    PROGRAM

    #include

    #include

    void main()

    {

    char s1[20], s2[20];

    printf("\nEnter string into s1: ");

    gets(s1);

    strcpy(s2, s1);

    printf("\ns2: %s", s2);

    getch();

    }

    OUTPUT

    Enter string into s1: Engineering

    s2: Engineering

    RESULT

    Thus the c program was written, entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    18/28

    STRING CONVERTION TO LOWERCASE, UPPERCASE, STRINGLENGTH

    PROGRAM

    #include

    #include

    #include

    void main()

    {

    charstr[50];

    clrscr();printf("\n\t Enteryour name : ");

    gets(str);

    printf("\nLower caseof string: %s",strlwr(str));

    printf("\nUpper caseof string: %s",strupr(str));

    printf("\nReverseof string: %s",strrev(str));

    printf("\nLength of String: %d",strlen(str));

    getch();

    }

    OUTPUT

    Enteryour name :College

    Lower caseof string: college

    Upper caseof string: COLLEGE

    Reverseof string: EGELLOC

    Length of String: 8

    RESULT

    Thus the c program was written, entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    19/28

    PROGRAMS USING USER DEFINED FUNCTIONS

    WITHOUT ARGUMENTS AND RETURN TYPE

    PROGRAM

    #include

    voidisleap();

    voidisleap()

    {

    int yr;

    printf("Enter a Year\n");

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

    printf("GivenYear is Leap year");

    else

    printf("GivenYear is Not a Leap year");

    }

    void main()

    {

    isleap();

    getch();

    }

    OUTPUT

    Enter a Year

    1965

    GivenYear is Not a Leap year

    RESULT

    Thus the c program was written,entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    20/28

    FUNCTIONS WITHOUT ARGUMENTS &WITH RETURNTYPE

    PROGRAM

    #include

    #include

    float area();

    float area()

    {

    int a,b,c;

    float s,ar;

    printf("Enter 3 Sides\n");scanf("%d%d%d",&a,&b,&c);

    s=(a+b+c)/2;

    ar=sqrt(s*(s-a)*(s-b)*(s-c));

    return ar;

    }

    void main()

    {

    float a;

    a=area();

    printf("The Area of Triangle is%f\n",a);

    }

    OUTPUT

    Enter3Sides

    12 8 7

    The Area of Triangle is19.748418

    RESULT

    Thus the c program was written, entered, executed successfullyand the output was

    verified

  • 8/10/2019 CP lab ex only pgms

    21/28

    FUNCTIONS WITH ARGUMENTS & WITHOUT RETURNTYPE

    PROGRAM

    #include

    void sorting(int a[],int n)

    {

    int i,j,t;

    for(i=0;i

  • 8/10/2019 CP lab ex only pgms

    22/28

    printf("Array Elemets before sorting\n");

    for(i=0;i

  • 8/10/2019 CP lab ex only pgms

    23/28

    USER DEFINED FUNCTIONS- SWAPPING OF TWO NUMBERS

    CALL BY VALUE AND REFERENCE

    PROGRAM

    #include

    #include

    void swap(int *,int *);

    void swap1(int,int);

    void main()

    {

    int a,b,c,d;

    clrscr();

    printf("Enter the values of a and b:= ");

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

    printf("Enter the values of c and d:= ");

    scanf("%d %d",&c,&d);

    printf("\n BEFORE SWAPPING : ");

    printf("\n The value of a and b is : %d\t %d ",a,b);

    printf("\n The value of c and d is : %d\t %d ",c,d);

    printf("\n AFTER SWAPPING : ");

    swap(a,b);

    swap1(&c,&d);

    printf("\n Method is:-Call by Value");

    printf("\n **************************");

    printf("\n The value of a and b is : %d\t %d",a,b);

    printf("\n Method is:-Call by Address or Reference");

    printf("\n ***************************");

    printf("\n The value of c and d is : %d\t %d",c,d);

    }

  • 8/10/2019 CP lab ex only pgms

    24/28

    void swap(int *c,int *d)

    {

    int t;

    t=*c;

    *c=*d;

    *d=t;

    }

    void swap1(int a,int b)

    {

    int t;

    t=a;

    a=b;

    b=a;

    }

    OUTPUT:

    Enter the values of a and b: 2 4

    Enter the values of c and d: 6 5

    BEFORE SWAPPING:

    The value of a and b is: 2 4

    The value of c and d is: 6 5

    AFTER SWAPPING:Method is:-Call by Value

    The value of a and b is: 2 4

    Method is:-Call by Address or Reference

    The value of c and d is: 5

    RESULT

    Thus the c program was written, entered, executed successfully and the output wasverified

  • 8/10/2019 CP lab ex only pgms

    25/28

  • 8/10/2019 CP lab ex only pgms

    26/28

    PROGRAM FOR STRUCTURE

    PROGRAM

    #include

    #include

    struct student

    {

    char name[20];

    int rno;

    }s;

    struct cse

    {

    int m1, m2, m3, m4, tot;

    float avg;

    struct student s;

    }c[10];

    void main()

    {

    int i,n;

    clrscr();

    printf("ENTER THE VEALUE OF n\n\n");

    scanf("%d",&n);

    printf("\nENTER NAME,RNO AND MARKS \n\n");

    for(i=0;i

  • 8/10/2019 CP lab ex only pgms

    27/28

    scanf("%d",&c[i].m4);

    }

    printf("\n\n\n");

    printf("\t\t******************STUDENT DETAILS***************\n");

    printf("\t------------------------------------------------------------------\n");

    printf("\tNAME \tRNO \tM1 \tM2 \tM3 \tM4 \tTOTAL \tAVERAGE\n");

    printf("\t------------------------------------------------------------------\n");

    for(i=0;i

  • 8/10/2019 CP lab ex only pgms

    28/28