computer programming lab programs

Upload: tejaswini

Post on 28-Feb-2018

231 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/25/2019 Computer Programming Lab Programs

    1/23

    1.Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c)

    of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots.

    Implement a C program for the developed flowchart/algorithm and execute the same tooutput the possible roots for a given set of coefficients with appropriate messages.

    #include

    #include

    int main()

    {

    float a, b, c, d;

    float root1, root2, real, img;

    printf("\nEnter the values of a b and c:\t");

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

    if((a == 0)&&(b == 0))

    {printf("\nInvalid inputs");

    }

    else

    {

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

    if(d == 0)

    {

    printf("\nRoots are real and equal:::");

    root1 = root2 = -b/(2*a);

    printf("\nRoot1 = %.3f \nRoot2 = %.3f", root1, root2);

    }

    else if(d > 0)

    {

    printf("\nRoots are real and distinct:::");

    root1 = (-b+sqrt(d))/(2*a);

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

    printf("\nRoot1 = %.3f \nRoot2 = %.3f", root1, root2);

    }

    else{

    printf("\nRoots are real and imaginary:::");

    real = -b/(2*a);

    img = (sqrt(fabs(d)))/(2*a);

    printf("\nRoot1 = %.3f+i%.3f", real, img);

    printf("\nRoot2 = %.3f-i%.3f", real, img);

    }

    }

    }

  • 7/25/2019 Computer Programming Lab Programs

    2/23

    Output:1)

    Enter the values of a b and c: 1 6 9

    Roots are real and equal:::

    Root1 = -3.000

    Root2 = -3.000

    2)

    Enter the values of a b and c: 1 -5 3

    Roots are real and distinct:::

    Root1 = 4.303

    Root2 = 0.697

    3)

    Enter the values of a b and c: 1 4 7

    Roots are real and imaginary:::

    Root1 = -2.000+i1.732

    Root2 = -2.000-i1.732

  • 7/25/2019 Computer Programming Lab Programs

    3/23

    2.Design and develop an algorithm to find the reverse of an integer number NUM and

    check whether it is PALINDROME or NOT. Implement a C program for the developed

    algorithm that takes an integer number as input and output the reverse of the same withsuitable messages.

    #include

    main()

    {

    int num, rev=0, rem, n;

    printf("\nEnter a number");

    scanf("%d", &num);

    n = num;

    while(num != 0)

    {

    rem = num%10;

    rev = rev*10+rem;num = num/10;

    }

    printf("\nReversed number is %d", rev);

    if(rev == n)

    {

    printf("\nGiven number is Palindrome");

    }

    else

    {

    printf("\nGiven number is not Palindrome ");

    }

    }

    Output:1)

    Enter a number: 1221

    Reversed number is 1221

    Given number is Palindrome

    2)

    Enter a number: 1234

    Reversed number is 4321

    Given number is not Palindrome

  • 7/25/2019 Computer Programming Lab Programs

    4/23

    3a).Design and develop a flowchart to find the square root of a given number N. Implement

    a C program for the same and execute for all possible inputs with appropriate messages.

    #include

    main()

    {

    double j;

    int i, n;

    printf("\nEnter a number:");

    scanf("%d", &n);

    j = n;

    for(i=1; i

  • 7/25/2019 Computer Programming Lab Programs

    5/23

    3b). Design and develop a C program to read a year as an input and find whether it is leap

    year or not. Also consider end of the centuries.

    #include

    main()

    {

    int year;printf("\nEnter valid year:");

    scanf("%d", &year);

    if(((year%4==0) && (year%100!=0)) | |(year%400==0))

    {

    printf("\n%d is a leap year", year);

    }

    else

    {

    printf("\n%d is not a leap year", year);

    }

    }

    Output:

    1)

    Enter valid year: 2012

    2012 is a leap year

    2)

    Enter valid year: 2200

    2200 is not a leap year

  • 7/25/2019 Computer Programming Lab Programs

    6/23

    4.Design and develop algorithm to evaluate polynomial f (x) = a4x4 + a3x3 + a2x2 +a1x +

    a0,for a given value of x and its coefficients using Horners method. Implement C program

    for the same and execute the program with different set of values of coefficients and x.

    #include

    main()

    {

    int n,sum=0, i ,a[50], x;

    printf("\nEnter the degree of polynomial:");

    scanf("%d", &n);

    printf("\nEnter the n+1 coeffients:");

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

    {

    scanf("%d", &a[i]);

    }

    printf("\nEnter value of x:");scanf("%d", &x);

    for(i=n; i>=1; i--)

    {

    sum = x*(sum+a[i]);

    }

    sum = sum+a[0];

    printf("\nSum of given polynomial is %d", sum);

    }

    Output:

    Enter the degree of polynomial: 4

    Enter the n+1 coeffients: 1 2 3 4 5

    Enter value of x: 2

    Sum of given polynomial is 57

  • 7/25/2019 Computer Programming Lab Programs

    7/23

    5. Draw the flowchart and Write a C Program to compute Sin(x) using Taylor series

    approximation given by Sin(x) = x - (x3/3!) + (x5/5! ) - (x7/7!) + .Compare your result

    with the built- in Library function. Print both the results with appropriate messages.

    #include

    #include

    main()

    {float degree, x, term, sum, i;

    printf("\nEnter the value in degree:");

    scanf("%f", &degree);

    x = degree*(3.14/180);

    sum = 0;

    term = x;

    i = 1;

    do

    {

    sum = sum+term;

    term = ((-1)*term*x*x)/((i+2)*(i+1));

    i = i+2;

    }while(fabs(term)>=0.000001);

    printf("\n Value of sin(%0.2f) is %0.2f", degree, sum);

    printf("\n Using library function value of sin(%0.2f) is %0.2f", degree, sin(x));

    }

    Output:

    1)

    Enter the value in degree: 45

    Value of sin(45.00) is 0.71

    Using library function value of sin(45.00) is 0.71

    2)

    Enter the value in degree: 30

    Value of sin(30.00) is 0.50

    Using library function value of sin(30.00) is 0.50

  • 7/25/2019 Computer Programming Lab Programs

    8/23

    6.Develop an algorithm, implement and execute a C program that reads N integer numbers

    and arrange them in ascending order using Bubble Sort.

    #include

    main()

    {

    int n, temp, a[50], i, j;

    printf("\nEnter number of elements to be taken:");

    scanf("%d", &n);

    printf("\nEnter elements:");

    for(i=0; i

  • 7/25/2019 Computer Programming Lab Programs

    9/23

    7. Develop, implement and execute a C program that reads two matricesA (m x n) and B (p

    x q ) and Compute product of matrices A and B. Read matrix A and matrix B in row major

    order and in column major order respectively. Print both the input matrices and resultant

    matrix with suitable headings and output should be in matrix format only. Program must

    check the compatibility of orders of the matrices for multiplication. Report an appropriate

    message in case of incompatibility.

    #include

    main()

    {

    int m, n, p, q, a[50][50], b[50][50], c[50][50], i, j, k;

    printf("\nEnter number of rows and columns of Matrix A:");

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

    printf("\nEnter matrix elements A:");

    for(i=0; i

  • 7/25/2019 Computer Programming Lab Programs

    10/23

    printf("\nMatrix elements B:\n");

    for(i=0; i

  • 7/25/2019 Computer Programming Lab Programs

    11/23

    Output:

    (1)

    Enter number of rows and columns of Matrix A: 2 2

    Enter matrix elements A: 1 2 3 4

    Enter number of rows and columns of Matrix B: 2 3

    Enter matrix elements B: 5 8 6 9 7 10

    Matrix elements A:

    1 2

    3 4

    Matrix elements B:

    5 6 7

    8 9 10

    Matrix elements C:

    21 24 27

    47 54 61

    (2)

    Enter number of rows and columns of Matrix A: 2 2

    Enter matrix elements A: 1 2 3 4

    Matrix elements A:

    1 2

    3 4

    Enter number of rows and columns of Matrix B: 3 2

    Enter matrix elements B: 1 2 3 4 5 6

    Matrix elements B:

    1 2

    3 4

    5 6

    Multiplication not possible

  • 7/25/2019 Computer Programming Lab Programs

    12/23

    8.Develop, implement and execute a C program to search a Name in a list of names using

    Binary searching Technique.

    #include

    #include

    main()

    {

    int n, i, low, high, mid;char name[100][100], key[100];

    printf("\nEnter number of names to be taken:");

    scanf("%d", &n);

    printf("\nEnter the names in alphabetical order:");

    for(i=0; ihigh)

    {

    printf("\nKey name not found");

    }

    }

    Output:

    Enter number of names to be taken: 5

    Enter the names in alphabetical order: aaa bcd eef ghi mno

    Enter key name to be searched: bcd

    Key name is found at pos 2

  • 7/25/2019 Computer Programming Lab Programs

    13/23

    9a). Write and execute a C program that, implements string copy operation STRCOPY (str1,

    str2) that copies a string str1 to another string str2 without using library function.

    #include

    void STRCOPY(char s1[], char s2[]);

    main()

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

    printf("\nEnter a string:");

    gets(s1);

    printf("\nString s1:");

    puts(s1);

    STRCOPY(s1, s2);

    printf("\nString s2:");

    puts(s2);

    }

    void STRCOPY(char s1[], char s2[])

    {

    int i = 0;

    while(s1[i] != '\0')

    {

    s2[i] = s1[i];

    i++;

    }

    s2[i] = '\0';

    }

    Output:

    Enter a string: hello how are you

    String s1: hello how are you

    String s2: hello how are you

  • 7/25/2019 Computer Programming Lab Programs

    14/23

    9 b)Write and execute a C program that, Read a sentence and print frequency of vowels and

    total count of consonants.

    #include

    #include

    #include

    main()

    {

    char s[50], ch;

    int ac=0, ec=0, ic=0, oc=0, uc=0, con=0, n, i;

    printf("\nEnter a sentence:");

    gets(s);

    n = strlen(s);

    for(i=0; i

  • 7/25/2019 Computer Programming Lab Programs

    15/23

    10a. Design and develop a C function RightShift(x ,n) that takes two integers x and n as

    input and returns value of the integer x rotated to the right by n positions. Assume the

    integers are unsigned. Write a C program that invokes this function with different values for

    x and n and tabulate the results with suitable headings.

    #include

    unsigned RightShift(unsigned int x, unsigned int n);

    void main()

    {

    unsigned int x, n, val;

    char ch;

    do

    {

    printf("\nEnter the value: ");

    scanf("%u", &x);

    printf("\nEnter the number of rotations: ");

    scanf("%u", &n);

    val=RightShift(x, n);

    printf("\nValue is %u", val);

    fflush(stdin);

    printf("\nDo you want to continue?: ");

    ch = getchar();

    }while(ch != 'n');

    }

    unsigned RightShift(unsigned int x, unsigned int n)

    {

    int i;

    for(i=0; i>1;

    else

    {x = x>>1;

    x = x+32768;

    }

    }

    return x;

    }

  • 7/25/2019 Computer Programming Lab Programs

    16/23

    Output:

    Enter the value: 4

    Enter the number of rotations: 2

    Value is 1

    Do you want to continue?: y

    Enter the value: 9

    Enter the number of rotations: 2

    Value is 16386

    Do you want to continue?: y

    Enter the value: 8

    Enter the number of rotations: 2

    Value is 2

    Do you want to continue?: y

    Enter the value: 7

    Enter the number of rotations: 1

    Value is 32771

    Do you want to continue?: n

  • 7/25/2019 Computer Programming Lab Programs

    17/23

    10 b) Design and develop a C function isprime (num) that accepts an integer argument and

    returns 1 if the argument is prime, a 0 otherwise. Write a C program that invokes this

    function to generate prime numbers between the given range.

    #include

    #include

    int isprime(int n);

    void main()

    {

    int x, y, i, flag = 0;

    printf("\nEnter the range: ");

    scanf("%d %d", &x, &y);

    for(i=x; i

  • 7/25/2019 Computer Programming Lab Programs

    18/23

    11. Draw the flowchart and write a recursive Cfunction to find the factorial of a number, n!,

    defined by fact(n)=1, if n=0. Otherwise fact (n) =n*fact (n-1). Using this function, write a C

    program to compute the binomial coefficient rnC . Tabulate the results for different values

    of n and r with suitable messages.

    #include

    int fact(int n)

    {

    if( n == 0 )

    return 1;

    else

    return (n*fact(n-1));

    }

    void main()

    {

    int n, r, res;

    printf("\nEnter value of n: ");

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

    res = fact(n)/(fact(n-r)*fact(r));

    printf("nCr is = %d", res);

    }

    Output:

    Enter value of n: 4 2

    nCr is = 6

  • 7/25/2019 Computer Programming Lab Programs

    19/23

    12. Given two university information files studentname.txt and usn.txt that contains

    students Name and USN respectively. Write a C program to create a new file called

    output.txt and copy the content of filesstudentname.txt and usn.txt into output file in

    the sequence shown below. Display the contents of output file output.txt on to the screen.

    #include

    void main()

    {

    FILE *fp1, *fp2, *fp3;

    char name[50], usn[50];

    fp1 = fopen("student.txt", "r");

    if(fp1 == NULL)

    printf("File1 Not found");

    fp2 = fopen("usn.txt", "r");

    if(fp2 == NULL)

    printf("File2 Not found");

    fp3 = fopen("output.txt", "w");

    while(!feof(fp1) && !feof(fp2))

    {

    fscanf(fp1, "%s", name);

    fscanf(fp2, "%s", usn);

    fprintf(fp3, "\n%s %s", name, usn);

    }

    fclose(fp1);

    fclose(fp2);

    fclose(fp3);

    fp3 = fopen("output.txt", "r");

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

    printf("NAME\t\tUSN\n");

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

    while(!feof(fp3)){

    fscanf(fp3, "%s", name);

    fscanf(fp3, "%s", usn);

    printf("\n%s\t\t%s", name, usn);

    }

    fclose(fp3);

    }

    Input files:

  • 7/25/2019 Computer Programming Lab Programs

    20/23

    student.txt usn.txt

    Output:

    -----------------------------

    NAME USN

    -----------------------------

    person1 414CS001

    person2 414CS002

    person3 414CS003

    output.txt

    person1

    person2

    person3

    414CS001

    414CS002

    414CS003

    person1 414CS001

    person2 414CS002

    person3 414CS003

  • 7/25/2019 Computer Programming Lab Programs

    21/23

    13. Write a C program to maintain a record of n student details using an array of structures

    with four fields (Roll number, Name, Marks, and Grade). Assume appropriate data type for

    each field. Print the marks of the student, given the student name as input.

    #include

    struct student

    {

    int rollno, marks;

    char name[20], grade;

    };

    void main()

    {

    struct student s[10];

    int n, i, found=0;

    char keyname[20];

    printf("\nEnter the number of students: ");scanf("%d", &n);

    for(i=0; i

  • 7/25/2019 Computer Programming Lab Programs

    22/23

    printf("\nMarks of student %s is: %d", s[i].name, s[i].marks);

    found = 1;

    }

    }

    if(found == 0)

    printf("\nGiven student name not found");

    }

    Output:

    Enter the number of students: 2

    Enter the details of student 1

    Enter roll num: 11

    Enter name: John

    Enter marks: 60

    Enter grade: a

    Enter the details of student 2

    Enter roll num: 22

    Enter name: Sam

    Enter marks: 50

    Enter grade: b

    Students details are:

    Roll No Name Marks Grade

    11 John 60 a

    22 Sam 50 b

    Enter the student name whose mark has to be displayed: John

    Marks of student John is: 60

  • 7/25/2019 Computer Programming Lab Programs

    23/23

    14.Write a C program using pointers to compute the sum, mean and standard deviation of

    all elements stored in an array of n real numbers.

    #include

    #include

    void main()

    {float a[40], sum=0, mean, sumvar=0, sd, var, *ptr;

    int n, i;

    printf("\nEnter the value of n:");

    scanf("%d", &n);

    printf("\nEnter the n array elements:");

    for(i=0; i