practical 6 10

Upload: keshav-ramdonee

Post on 08-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Practical 6 10

    1/27

    PRACTICAL 6

    Exercise1.1a

    The program will not work. Because int x,y is found outside the main function.

    Exercise 1.2

    #include

    int main()

    {

    int x,y;

    printf("\nEnter two numbers");

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

    printf("\n\n%d is bigger",(x>y)?x:y);

    return 0;}

    Exercise 1.3

    if (x >=1 && x

  • 8/7/2019 Practical 6 10

    2/27

    Exercise 1.7

    if (age>=21 && age=65)

    printf(The person is a senior citizen);

    Exercise 1.8

    #include

    int main()

    {

    int x= 1;

    if( x = 1)

    printf(" x equals 1" );

    else

    printf(" x does not equal 1");

    return 0;

    }

    Exercise 1.9

    DECLARATION OF VARIABLES:

    int i,j;

    long ix ;

    short s;

    float x;

    double dx;char c;

    EXPRESSIONS:

    (a) i + c int

    (b) x + c float

    (c) dx + x float

    (d) ((int)dx) + ix int

    (e) i + x float

    (f) s + j int

    (g) ix + j int(h) s + c int

    (i) ix + c int

    2

  • 8/7/2019 Practical 6 10

    3/27

    Exercise 1.10

    a) k = (i + j ) = 13

    b) z = (x + y) = -0.005

    c) i = j = 5

    d) k = (x + y) = 0

    e) k = c = garbage value

    f) z = i / j = 1.600

    g) a = b = d = d

    h) i= j = 1.1 =1

    i) z = k = x = 0.000

    j) k = z = x = 0k) i += 2 = 10

    l) y -= x = -0.015

    m) x *= 2 =0.01

    n) i /= j = 1

    o) i %= j = 3

    p) i += ( j - 2) = 11

    Exercise 1.11

    (a) scanf(%d%d%f%f,&i,&j.&x,&dx);

    (b) scanf(%d%L%d%f%u,&i,&ix,&j,&x,&u);

    (c) scanf(%d%u%c,&i,&u,&c);

    (d) scanf(%c%f%f%f,&c,&x,&dx,&s);

    Exercise 1.12

    #include

    Float x,y,z;

    (a) Printf(x, y and z are %f, %f and %f, x, y, z);

    (b) Printf(x+y is equal to %f, and x-z is equal to %f,(x+y), (x-y));(c) Printf(The squareroot of (x+y) is %f and the absolute value of (x-z) is %f,

    sqrt(x+y), Fabs(x-z));

    Exercise 1.13

    #include

    int main()

    {

    int n=0, sum=0, i=1;

    printf("Enter a number. : ");

    scanf("%d", &n);while(n>0)

    {

    3

  • 8/7/2019 Practical 6 10

    4/27

    i = n%10;

    printf("%d", i);

    sum+= i; n = n/10;

    }

    printf("\nSum of the number is %d",sum);

    return 0;}

    Exercise 1.14

    #include

    int main()

    {

    int x,y;

    printf("Enter a number: ");

    scanf("%d", &x);

    printf("Now enter a second number: ");

    scanf("%d", &y);

    if(x%y==0 && x>y)printf("x is evenly divisible by y");

    else

    printf("x is not evenly divisible by y");

    return 0;

    }

    Exercise 1.15

    #include

    int main()

    {

    int x,y;

    float div;div = x/y;

    printf("Enter first number: ");

    4

  • 8/7/2019 Practical 6 10

    5/27

    scanf("%d", &x);

    printf("Enter second number: ");

    scanf("%d", &y);

    if (y==0)

    printf("NO SOLUTION!");

    elseprintf("The answer is %5.3f\n", div);

    return 0;

    }

    Exercise 1.16

    #include

    int main (int argc, const char * argv[])

    {

    int number, digit1, digit2, digit3, digit4, digit5;

    printf("Enter a number (max 5 digits): ");

    scanf("%i", &number);

    if (number < 100000){

    digit1 = number % 100000 / 10000;switch (digit1)

    {

    case 0 : printf("Zero "); break;

    case 1 : printf("One "); break;

    case 2 : printf("Two "); break;

    case 3 : printf("Three "); break;

    case 4 : printf("Four "); break;

    case 5 : printf("Five "); break;

    case 6 : printf("Six "); break;

    case 7 : printf("Seven "); break;

    case 8 : printf("Eight "); break;case 9 : printf("Nine "); break;

    default : printf("Unknown Interger "); break;

    }

    digit2 = number % 10000 / 1000;

    switch (digit2)

    {

    case 0 : printf("Zero "); break;

    case 1 : printf("One "); break;

    case 2 : printf("Two "); break;

    case 3 : printf("Three "); break;

    5

  • 8/7/2019 Practical 6 10

    6/27

    case 4 : printf("Four "); break;

    case 5 : printf("Five "); break;

    case 6 : printf("Six "); break;

    case 7 : printf("Seven "); break;

    case 8 : printf("Eight "); break;

    case 9 : printf("Nine "); break;default : printf("Unknown Interger "); break;

    }

    digit3 = number % 1000 / 100;

    switch (digit3)

    {

    case 0 : printf("Zero "); break;

    case 1 : printf("One "); break;

    case 2 : printf("Two "); break;

    case 3 : printf("Three "); break;case 4 : printf("Four "); break;

    case 5 : printf("Five "); break;

    case 6 : printf("Six "); break;

    case 7 : printf("Seven "); break;

    case 8 : printf("Eight "); break;

    case 9 : printf("Nine "); break;

    default : printf("Unknown Interger "); break;

    }

    digit4 = number % 100 / 10;

    switch (digit4)

    {

    case 0 : printf("Zero "); break;

    case 1 : printf("One "); break;

    case 2 : printf("Two "); break;

    case 3 : printf("Three "); break;

    case 4 : printf("Four "); break;

    case 5 : printf("Five "); break;

    case 6 : printf("Six "); break;

    case 7 : printf("Seven "); break;case 8 : printf("Eight "); break;

    case 9 : printf("Nine "); break;

    default : printf("Unknown Interger "); break;

    }

    digit5 = number % 10;

    switch (digit5)

    {

    case 0 : printf("Zero "); break;

    case 1 : printf("One "); break;case 2 : printf("Two "); break;

    case 3 : printf("Three "); break;

    6

  • 8/7/2019 Practical 6 10

    7/27

    case 4 : printf("Four "); break;

    case 5 : printf("Five "); break;

    case 6 : printf("Six "); break;

    case 7 : printf("Seven "); break;

    case 8 : printf("Eight "); break;

    case 9 : printf("Nine "); break;default : printf("Unknown Interger "); break;

    }

    }

    else

    {

    printf("Entered Number is too long.");

    }

    return 0;

    }

    7

  • 8/7/2019 Practical 6 10

    8/27

    PRACTICAL TUTORIAL 7

    NO 1.

    #include

    #include

    int main()

    {

    int usenum;

    printf("Type in your number: ");

    scanf("%d", &usenum);

    if(usenum < 0)

    printf("The absolute value is %d \n", abs(usenum));

    else

    printf("The absolute value is %d \n", usenum);

    return 0;

    }

    NO 2.

    #include

    int main()

    {

    int x;

    printf("Type in a number: ");

    scanf("%d", &x);

    if(x%2 == 0)

    printf("The number is even");

    else

    printf("The number is odd");

    return 0;

    }

    8

  • 8/7/2019 Practical 6 10

    9/27

    NO 3.

    #include

    int main()

    {

    int year;

    printf("Enter a year: ");

    scanf("%d", &year);

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

    printf("The year is a leap one");

    else

    printf("The year is not a leap one");

    return 0;

    }

    NO 4.

    #include

    int main()

    {int firstnum, secondnum;

    printf("Enter first number:");

    scanf("%d",&firstnum);

    printf("\nEnter Second number:");

    scanf("%d",&secondnum);

    if (firstnum%secondnum==0)

    if((firstnum/secondnum)%2==0)

    printf("\nThe Frist number is evenly divisible by Second number\n");

    else

    printf("\nThe Frist number is NOT evenly divisible by Second number\n");

    else

    printf("\nThe Frist number is evenly divisible by Second number\n");

    9

  • 8/7/2019 Practical 6 10

    10/27

    return 0;

    }

    NO 5.

    #include

    int main()

    {

    int num1,num2;

    float division;printf ("Enter First number:");

    scanf ("%d",&num1);

    while (num1==0)

    {

    printf ("Number Cannot be Zero\nEnter another number:");

    scanf ("%d",&num1);

    }

    printf ("Enter Second number:");

    scanf ("%d",&num2);

    while (num2==0)

    {

    printf ("Number Cannot be Zero\nEnter another number:");

    scanf ("%d",&num2);

    }

    division=(num1/num2);

    printf ("Division of First and Second number=%.3f",division);

    return 0 ;

    }

    NO 6.

    #include

    int main (void)

    {

    float accumulator, value;

    char op;

    printf ("Begin Calculations\n");

    do

    {

    10

  • 8/7/2019 Practical 6 10

    11/27

    scanf("%f %c", &value, &op);

    switch (op)

    {

    case 'S':

    accumulator = value;printf("= %f\n",accumulator);

    break;

    case '+':

    accumulator = accumulator + value;

    printf("= %f\n",accumulator);

    break;

    case '-':

    accumulator = accumulator - value;

    printf("= %f\n",accumulator);break;

    case '*':

    accumulator = accumulator * value;

    printf("= %f\n",accumulator);

    break;

    default:

    printf ("Error! Unknown operator.\n");

    }

    }

    while (op != 'E' );

    printf("= %f\n",accumulator);

    printf("End of Caculations\n");

    return 0;

    }

    11

  • 8/7/2019 Practical 6 10

    12/27

    NO 7.

    (a).

    #include

    int main()

    {

    int i, total;i = 2;

    total = 0.0;

    while(i

  • 8/7/2019 Practical 6 10

    13/27

    NO 8.

    (a).

    #include

    void main ()

    {

    int i, total, testnum;

    total = 0;

    for(i=2; i

  • 8/7/2019 Practical 6 10

    14/27

    14

  • 8/7/2019 Practical 6 10

    15/27

    PRACTICAL TUTORIAL 8

    NO 1.

    float do_it(char a, char b, char c)

    NO 2.

    void print_a_number(int num)

    NO 3.

    (a). int

    (b). long

    NO 4.

    function does not take 1 arguments

    no value to return

    NO 5.

    There should be no semicolon after "int twice(int y)".

    NO 6.

    #include

    int main()

    {

    int product(int, int);

    int x,y,xy;

    printf("Enter first number: ");

    scanf("%d", &x);

    printf("Enter second number: ");

    scanf("%d", &y);

    xy = product(x,y);

    printf("The product is %d\n", xy);

    return 0;

    }

    int product(int a, int b)

    {

    int prod;prod = a*b;

    return (prod);

    }

    NO 7.#include

    float division(float, float);

    15

  • 8/7/2019 Practical 6 10

    16/27

    void main (void)

    {

    float firstnum, secnum;

    float result;

    printf ("Enter a value for first number:\n");scanf ("%f", &firstnum);

    printf ("Enter a value for second number:\n");

    scanf ("%f", &secnum);

    division (firstnum, secnum);

    result = firstnum/secnum;

    if (secnum!=0)

    printf ("The division of the 1st number by the 2nd number is %1.3f.\n",

    result);

    }

    float division(float firstnum, float secnum)

    {

    if (secnum==0)

    printf ("Error. A value cannot be divided by zero.\n");

    else

    {

    float result = firstnum/secnum;

    return (result);

    }

    }

    NO 8.

    #include

    int main()

    {

    float average(float, float,float,float,float);

    float a,b,c,d,e,ave;

    printf("Enter first number: ");

    scanf("%f", &a);printf("Enter second number: ");

    scanf("%f", &b);

    16

  • 8/7/2019 Practical 6 10

    17/27

    printf("Enter third number: ");

    scanf("%f", &c);

    printf("Enter fourth number: ");

    scanf("%f", &d);

    printf("Enter fifth number: ");

    scanf("%f", &e);

    ave = average(a,b,c,d,e);

    printf("The average is %f\n", ave);

    return 0;

    }

    float average(float p, float q,float r,float s,float t)

    {

    float aver;

    aver = (p+q+r+s+t)/5;

    return (aver);

    }

    NO 8.

    #include

    #include

    int power (int);

    void main (void)

    {

    int x;

    printf ("Enter a value:\n");

    scanf ("%i", &x);

    printf ("3 raised to the power of %i is %i.\n", x, power(x));

    }

    int power(int x)

    {

    if(x==0)

    return 1;

    else

    return power(x-1)*3;

    }

    17

  • 8/7/2019 Practical 6 10

    18/27

    18

  • 8/7/2019 Practical 6 10

    19/27

    PRACTICAL TUTORIAL 9

    NO 1.

    Semicolon after function header must not be included

    NO 2.The output would be: 4 16

    The function show() has already returned the square of x to the main function. Hence

    the second printf function and the return of cube x to the main are ignored.

    NO 3.

    The function will return the value of 1 to the main only if a=b AND a=c. Else if a!=b

    and a!=c, or if a!=b but a=c, or if a=b but a!=c, the function will return the value of

    zero to the main.

    NO 4.

    #include

    int main(){

    int maxnum(int, int);

    int x,y,max;

    printf("Enter first number: ");

    scanf("%d", &x);

    printf("Enter second number: ");

    scanf("%d", &y);

    max = maxnum(x,y);

    return 0;

    }

    int maxnum(int a, int b)

    {

    int maxi;

    if (a>=b)

    maxi = a;

    else

    maxi = b;

    printf("The larger number is %d\n", maxi);

    return (maxi);

    }

    NO 5.

    #include

    int main()

    {

    19

  • 8/7/2019 Practical 6 10

    20/27

    int maxnum(int, int, int);

    int x,y,z, max;

    printf("Enter first number: ");

    scanf("%d", &x);

    printf("Enter second number: ");

    scanf("%d", &y);printf("Enter third number: ");

    scanf("%d", &z);

    max = maxnum(x,y,z);

    return 0;

    }

    int maxnum(int a, int b, int c)

    {

    int maxi;

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

    maxi = a;else if (b>=a && b>=c)

    maxi = b;

    else

    maxi = c;

    printf("The largest number is %d\n", maxi);

    return (maxi);

    }

    NO 6.

    #include

    #include

    int main ()

    {

    int absolute(int);

    int x, absol;

    printf("Enter a value: ");

    scanf("%d", &x);

    absol = absolute(x);

    printf("The absolute value of %d is %d\n", x, absol);

    return 0;

    }

    absolute(int a)

    { int abst;

    abst = abs(a);

    20

  • 8/7/2019 Practical 6 10

    21/27

    return (abst);

    }

    NO 7.

    int f(int n)

    {

    int i = 1;

    while (i < n)

    i += i;

    return i;

    }

    NO 8.

    #include

    int prime (int);

    void main()

    {

    int n;

    printf ("Enter a positive integer number you want to test: ");

    scanf("%i", &n);

    prime(n);

    printf ("If '1' is displayed, then it is a prime.\n");printf ("If '0' is displayed, then it is not a prime.\n");

    printf ("%i.\n", prime(n));

    }

    int prime (int n)

    {

    int i, c=0;

    for(i=1; i

  • 8/7/2019 Practical 6 10

    22/27

    NO 9.

    #include

    int prime (int);

    void main()

    {

    int n;

    printf ("Enter a positive integer number you want to test: ");

    scanf("%i", &n);

    prime(n);printf ("If '0' is displayed, then it is a prime.\n");

    printf ("If '1' is displayed, then it is not a prime.\n");

    printf ("%i.\n", prime(n));

    }

    int prime (int n)

    {

    int i, c=0;

    for(i=1; i

  • 8/7/2019 Practical 6 10

    23/27

    NO 13.

    '{' : missing function header (old-style formal list?)

    syntax error : missing ';' before identifier 'b'

    syntax error : missing ';' before 'else'

    PRACTICAL TUTORIAL 10

    NO 1.

    NO 2.

    NO 3.

    23

  • 8/7/2019 Practical 6 10

    24/27

  • 8/7/2019 Practical 6 10

    25/27

    int P[150], i, j;

    for(i=0; i

  • 8/7/2019 Practical 6 10

    26/27

    {

    int Table1[r][c],Table2[r][c];

    int sum[r][c];

    readinput(Table1,Table2);

    computesums(Table1, Table2, sum);writeoutput(sum);

    }

    void readinput(int Table1[r][c],int Table2[r][c])

    {

    int i,j;

    printf("For Table 1\n");

    for (i=0; i

  • 8/7/2019 Practical 6 10

    27/27