You are on page 1of 4

PROG 120

Applied college

Activity 8

Question 1:
Give the output from the following pieces of code:
Code Output
int x, y;
x = 20; The total is 21
1
y = x - 3;
printf ("The total is %i\n", y + 4);
int n = 0, m = 0; Less than Zero
if (n > 0)
{
if (m > 0)
2
printf("More than Zero");
}
else
printf("Less than Zero");
int i = 3; Repeat the process 3
while (i > 0) Repeat the process 2
{ Repeat the process 1
3
i--;
printf("Repeat the process %i\n", i);
}
int i = 0; Repeat the process
do Repeat the process
{ 2
i++;
4
printf("Repeat the process\n");
} while (i < 2);

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

Question 2:
Find the errors in the following pieces of code and correct them:
Code Error Correction
1. Wrong float x;
(2 errors)
modifier (%s) printf("Enter a
float x;
1 2. Missing (&) number");
printf ("Enter a number");
before variable scanf ("%f", &x);
scanf ("%s", x);
(2errors) 1.This type of int count;
int count; comment use two //Calculate the
2 /Calculate the result (/) symbol result
count = 5 + 7 - 10/2; 2. Must add count = 5 + 7 - 10/2;
printf ("The result is %i\n", count) semicolon
PROG 120

Applied college

printf ("The result


is %i\n" ,count);

Question 3:
1. Write a program calculate the area of a trapezoid.
Area = ½ (a + b) h, where 'a' and 'b' are the bases (parallel sides), and
'h' is the height

#include<stdio.h>
int main()
{
float a, b, h, area;
printf("\n Please Enter the first base of the trapezoid:");
scanf("%f", &a);
printf("\n Please Enter the second base of the trapezoid:");
scanf("%f", &b);
printf("\n Please Enter the height of the trapezoid:");
scanf("%f", &h);
area = 0.5 * (a + b) h;

printf("\n The Area of a trapezoid= %.2f\n", area);


return 0;
}

1. Write a program to check whether the input integer numbers from user are odd number
or even. The program stops when the user enters a negative number.

#include<stdio.h>
int main()
{
int number;
printf("Enter a number: \n");
PROG 120

Applied college

scanf("%d", &number);
while(number > 0){
if (number%2==0)
printf("%d is an even number \n", number);
else
printf("%d is an odd number \n", number);
printf("Enter a number: \n");
scanf("%d", &number);
}
return 0;}

Question 4:
Convert the following algorithm to C program:
Print multiplication table for the given number:
1. Start
2. Print “Enter a number”
3. Enter Number
4. Loop: Counter 1 To 10
Print Number * Counter
LoopEnd: Conter
5. End

#include<stdio.h>
int main()
{
int number;
printf("\n Enter a number ");
scanf("%i",&number);
for(i=1;i<=10;i++){
printf("%i \n",(number*i));
}
PROG 120

Applied college

return 0;
}

You might also like