Lab Manual FOC

download Lab Manual FOC

of 26

Transcript of Lab Manual FOC

  • 8/12/2019 Lab Manual FOC

    1/26

  • 8/12/2019 Lab Manual FOC

    2/26

    2

    SYLLABUS FOR COMPUTER PROGRAMMING LAB

    1. Program to find Largest of three Numbers.

    2. Program to find largest out of ten numbers.

    3. Program to find largest and second largest out of ten numbers.

    4. Program to add two matrices.

    5. Program to concatenate two Strings.

    6. Program to check whether a string is palindrome or not.

    7. Program to find factorial of a number entered through Keyboard.

    8. Program to swap two numbers.

    9. Program to reverse a number entered through keyboard.

    10. Program to sum digits of a number entered through keyboard.

    11. Program to reverse a string.

    12. Program to check whether a number is prime or not.

    13. Program to implement linear search.

    14. Program to Draw Pyramid of stars.

    15. Program to multiply two metrics. RATIONAL BEHIND COMPUTER PROGRAMMINGLAB

    SOFTWARE: C LANGUAGE

    OPERATING SYSTEM: ANY

    HARDWARE REQUIREMENT: 50MB RAM, 1GB

    PROCESSOR: 386 AND ABOVE

    FLOWCHART TO FIND LARGEST OF THREE NUMBERS

    START

    read a , b , c

    No Yes

    is a>b and a>c is b>a and b>c

    Yes

    No

    print

    Largest of three numbers: a print

    Largest of three numbers: b

    print

    Largest of three numbers: c

  • 8/12/2019 Lab Manual FOC

    3/26

    3

    STOPALGORITHM TO FIND LARGEST OF THREE NUMBERS

    1. float a,b,c;

    2. print Enter any threenumbers:;

    3. read a,b,c;

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

    5. print Largest of three numbers:, a;

    6. else

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

    8. print Largest of three numbers:, b;

    9. else

    10.print Largest of three numbers:, c;

    PROGRAM TO FIND LARGEST OF THREE NUMBERS

    #include

    #include

    void main( )

    float a,b,c;printf(Enter any three numbers:);

    scanf(%f%f%f,&a,&b,&c);

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

    printf(Largest of three numbers: %f,a);

    else

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

    printf(Largest of three numbers: %f,b);

    else

    printf(Largest of three numbers: %f,c);

    getch( );

    OUTPUT:

    Enter any three numbers:

    501

    450

    123

    Largest of three numbers: 501

    FLOWCHART TO FIND LARGEST NUMBER OUT OF TEN NUMBERS

    START

    Yes

    i=0 i

  • 8/12/2019 Lab Manual FOC

    4/26

    4

    No

    i++

    i=0

    ia[j] t=1

    j=i+1

    No

    t=0 is t==1

    ja[j])

    7. t 1;

    8. else

    9. t 0;

    10. break;

    11.if(t==1)12.print Largest no.:,a[i];

    13.break;

    PROGRAM TO FIND LARGEST NUMBER OUT OF TEN NUMBERS

    #include

    #include

    void main()

    {

    int a[10],i,s=0,j,t;

  • 8/12/2019 Lab Manual FOC

    5/26

    5

    clrscr();

    printf(Enter ten numbers:);

    for(i=0;i

  • 8/12/2019 Lab Manual FOC

    6/26

    6

    FLOWCHART TO READ A STRING AND WRITE IT IN REVERSE ORDER

    START

    Read array a

    i=0

    i=0

    No

    a[i]!= \0 j=g-1 No

    i

  • 8/12/2019 Lab Manual FOC

    7/26

    7

    }

    j=g-1;

    for(i=0;i

  • 8/12/2019 Lab Manual FOC

    8/26

  • 8/12/2019 Lab Manual FOC

    9/26

    9

    if(m==0)

    {

    printf("String is a Palindrome");

    }

    else

    if(m==1)

    }

    OUTPUT:

    Enter any string: Madam

    String is not a Palindrome

    Enter any string: ARORA

    String is a Palindrome

    FLOWCHART TO CONCATINATE TWO STRINGS STARTPrint Enter first string

    Read a;

    Print Enter second string

    Read b

    i=0

    j=0

    No

    a[i]!= \0

    b[j]!= \0

    Yesc[i]=a[i]

    c[i+j]=b[j]

    i++

    j++

    print

    The Concatenated string is: c

    Stop

    ALGORITHM TO CONCATINATE TWO STRINGS

    1. character array a[25],b[25],c[25];

    2. integer i,j;

    3. print Enter first string:;

    4. read a;

    5. print Enter second string:;

  • 8/12/2019 Lab Manual FOC

    10/26

    10

    6. read b;

    7. string a is copied to array c by using for loop;

    8. string b is copied to array cnext to the first string by using for loop;

    9. print The concatenated string is:,c;

    WRITE A PROGRAM TO CONCATINATE TWO STRINGS

    #include

    #include

    void main()

    char a[25],b[25],c[25];

    int i.j;

    clrscr();

    printf(Enter first string:);

    scanf(%s,a);

    printf(Enter second string:);scanf(%s,b);

    for(i=0;a[i]!=\0;i++)

    c[i]=a[i];

    for(j=0; b[j]!=\0; j++)

    c[i+j]=b[j];

    c[i+j]=\0;

    printf(The concatenated string is:\n%s,c);

    getch();

    OUTPUT:

    Enter first string: NetEnter second string: World

    The concatenated string is: NetWorld

    FLOWCHART TO MULTIPLY TWO MATRICES

    START

    read m,n,p,q;

    n=p i=0 i=0

    j=0 j=0

    No No

    Print i

  • 8/12/2019 Lab Manual FOC

    11/26

    11

    j=0

    s=0

    Yes Yes

    read a[i][j] read b[i][j]

    i

  • 8/12/2019 Lab Manual FOC

    12/26

    12

    PROGRAM TO MULTIPLY TWO MATRICES

    #include

    #include

    #include

    void main()

    {

    int a[10][10],b[10][10],c[10][10];

    int i,j,m,n,p,q,s;

    clrscr();

    printf("Input row and column of matrix-A\n");

    scanf("%d%d",&m,&n);

    printf("Input row and column of matrix-B\n");

    scanf("%d%d",&p,&q);

    if(n!=p){

    printf("Matrix cannot be multiplied\n");

    getch();

    exit(0);

    }

    printf("Input Matrix-A\n");

    for(i=0;i

  • 8/12/2019 Lab Manual FOC

    13/26

    13

    {

    for(j=0;j

  • 8/12/2019 Lab Manual FOC

    14/26

  • 8/12/2019 Lab Manual FOC

    15/26

    15

    WRITE A PROGRAM TO SOLVE QUADRATIC EQUATION

    #include

    #include

    #include

    void main()

    void quad(int a, int b ,int c);

    int a,b,c;

    float r;

    clrscr();

    printf(Enter values of a,b,c of a quadratic equation:\n );

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

    quad(a,b,c);

    getch();

    void quad(int a, int b ,int c)

    float d,r1,r2,r3,r4,r;

    d=(b*b)-(4*a*c);

    switch(d)case

  • 8/12/2019 Lab Manual FOC

    16/26

    16

    NUMBER OUT OF GIVEN NUMBER

    START

    print

    How many no. will you enter:

    Read n

    i=0

    No

    il)

    7. the largest value is stored in l and printed

    8. print "Largest element is:",l;

    9. again the above process is continued but if a[i]equals to largest value l then

    value of i is incremented and second largest value is stored in sl

    10.print "Second Largest element is:",sl;

  • 8/12/2019 Lab Manual FOC

    17/26

    17

    WRITE A PROGRAM TO FIND LARGEST AND SECOND LARGEST NUMBER OUT

    OF GIVEN NUMBER

    #include

    #include

    void main()

    int i,a[55],n,l=0,sl;

    clrscr();

    printf("How many no. will you enter: ");

    scanf("%d",&n);

    for(i=0;i

  • 8/12/2019 Lab Manual FOC

    18/26

  • 8/12/2019 Lab Manual FOC

    19/26

    19

    float mavg,favg,msum=0,fsum=0;

    clrscr();

    printf(enter the height of male\n);

    for(i=0;i

  • 8/12/2019 Lab Manual FOC

    20/26

    20

    WRITE A PROGRAM TO FIND FACTORIAL OF A NUMBER

    #include

    #include

    void main()

    int a,i.fact=1;

    clrscr();

    printf(Enter the number \n);

    scanf(%d%,&a);

    for(i=1;i

  • 8/12/2019 Lab Manual FOC

    21/26

    21

    printf(%d\t,t);

    f=s;

    s=t;

    getch();

    OUTPUT:

    Enter the number of series

    1 1 2 3 5 8

    ALGORITHM TO FIND THE REVERSE OF A NUMBER

    STEP 1 : Declare the int variable n,b,s=0

    STEP 2: enter the number in n (which can be reverse).

    STEP 3: using while loop(n!=0)

    STEP 4 : b=n%10

    s=(s*10)+b

    n=n/10

    STEP 5 : print the reverse number in s.STEP 6. Stop.

    WRITE A PROGRAM TO REVERSE OF NUMBER

    #include

    #include

    void main()

    int n,b,s=0;

    clrscr();

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

    while(n!=0)

    b=n%10;

    s=(s*10)+b;

    n=n/10;

    printf(The reverse number is %d,s);

    getch();

    OUTPUT:

    Enter the number

    123

    The reverse number is

    321

  • 8/12/2019 Lab Manual FOC

    22/26

    22

    ALGORITEM TO SWAP TWO NUMBERS

    STEP 1 : Declare the int variable a,b,temp.

    STEP 2: enter the number in a &b.

    STEP 3: temp=a

    a=b

    b=temp

    STEP 4 : print the swapped value in a,b.

    STEP 5. Stop.

    WRITE A PROGRAM TO SWAP OF TWO NUMBER

    #include

    #include

    void main()

    int a,,b,temp;

    clrscr();

    printf(Enter the number of a&b \n);

    scanf(%d\n%d\n,&a,&b);temp=a;

    a=b;

    b=temp;

    printf(the swapped values are \n);

    printf(%d\n%d\n,a,b);

    getch();

    OUTPUT:

    Enter the number of a&b

    the swapped values are

    ALGORITHM TO PRINT PYRAMIND OF STARS

    STEP 1 : Declare the int variable i.a.j.k

    STEP 2: enter the number of line in a.

    STEP 3: using for loop i is compare when it is not equal to and less than a.

    j is initialize for blank space.

    k is initialize to print the star.

    STEP 4. Stop.

    -

  • 8/12/2019 Lab Manual FOC

    23/26

    23

    WRITE A PROGRAM TO PRINT PYRAMID STAR

    #include

    #include

    void main()

    int i,a,j,k;

    clrscr();

    printf(enter the number of line \n);

    scanf(%d,&a);

    for(i=0;i

  • 8/12/2019 Lab Manual FOC

    24/26

  • 8/12/2019 Lab Manual FOC

    25/26

    25

    enter the element of matrix B(row wise)

    the element of matrix A is

    6

    9

    the element of matrix B is

    4 6

    2 8

    the addition of matrix is :

    7 12

    9 17New ideas/ experiments required for Computer programming lab

    behind university syllabus1.

    LIST OF FREQUENTLY ASKED QUESTIONS

    1. How do you decide which integer type to use?2.

    What should the 64-bit type on new, 64-bit machines be?

    3. What can I safely assume about the initial values of variables which4. are not explicitly initialized? If global variables start out as ``zero,''5. is that good enough for null pointers and floating-point zeroes?6. How can I read a single character from the keyboard without7. waiting for a newline8. How can I copy a float into a string?9. Why doesn't C have an exponentiation operator?10.Why does everyone say not to use gets()?11.Why doesn't the code work? a[i] = i++;12.How does struct passing and returning work?13.Why can't you compare structs?14.How do I enter values using hexadecimal?15.What does extern mean in a function declaration?16.How do I ``get'' a null statement in my programs? I had the definition in one source file,

    and in another I char a[6]

    17.declared . Why did it work?18.extern char a[]19.why are array and pointer declarations interchangeable as function20.formal parameters?21.what is the difference between arrays and pointers?22.How does free() know how many bytes to free?23.How can I get the numeric (character set) value corresponding to a24.character, or vice versa?25.What is the right type to use for boolean values in C? Why isn't it a26.standard type? Should s or s be used for the true and false #define enum27.values?

  • 8/12/2019 Lab Manual FOC

    26/26

    28.What is the ``ANSI C Standard?''29.What's the difference between ``char const *p'' and ``char * const30.p''?31.What's wrong with this code:32.char c;33.while((c = getchar()) != EOF)...34.How can I print a ``%'' character in a printf format string?35.Which is larger, ``2'' or ``2.0''?