0% found this document useful (0 votes)
18 views

Programming Basic Questions - 114319

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Programming Basic Questions - 114319

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

ANSWERS:

1. Modify the first program so that the program displays your name.

#include <stdio.h>

int main()
{
printf("my name is Bernard");
return 0;
}

2. Write a program that calculates and prints the sum of two integers on computer screen

#include <stdio.h>

int main()
{
int num1, num2, sum;
num1 = 3;
num2 = 5;
sum = num1 + num2;
printf("%d", sum);
return 0;
}

3. Write a program that obtains two numbers from a user, computes the sum of these
numbers and outputs the result on computer screen

#include <stdio.h>

int main()

int num1, num2, sum;

printf("\nplease enter the first number: ");

scanf("%d", &num1);

printf("\nplease enter the second number: ");

scanf("%d", &num2);

sum = num1 + num2;

printf("\nthe sum of these numbers is, %d", sum);

return 0;

}
4. Write a program that reads three integers from the keyboard, store them in the variables x,
y and z calculates the product of the three integers and prints the result on screen

#include <stdio.h>

int main()

int x,y,z,product;

printf("\nplease enter the first number: ");

scanf("%d", &x);

printf("\nplease enter the second number: ");

scanf("%d", &y);

printf("\nplease enter the third number: ");

scanf("%d", &z);

product = x*y*z;

printf("\nthe product of these numbers is, %d", product);

return 0;

5. Write a program that asks the user to enter two numbers, obtains them from the user and
prints their sum, product, difference, quotient and remainder.

#include <stdio.h>

int main()

int num1, num2,sum, product, difference, quotient, remainder;

printf("\nplease enter the first number: ");

scanf("%d", &num1);

printf("\nplease enter the second number: ");

scanf("%d", &num2);

sum = num1 + num2;

product = num1 * num2;

difference = num1 - num2;

quotient = num1 / num2;

remainder = num1 % num2; // remember remainder doesn't work with float and doubles in c

printf("\nsum = %d, product = %d, difference = %d, quotient = %d, remainder = %d ", sum,product,difference,quotient,remainder);

return 0;

}
6. Write a program that calculates and prints area and perimeter of a rectangle on computer
screen.

#include <stdio.h>

int main()
{
int w,l,area, perimeter;
printf("\nenter width of a rectangle: ");
scanf("%d", &w);
printf("\nenter length of a rectangle: ");
scanf("%d", &l);

area = w * l;
perimeter = (2*w) + (2*l);
printf("\narea = %d, perimeter = %d", area , perimeter);
return 0;
}

7. Write a program that converts the temperature reading in degrees Fahrenheit, entered by
user, into degrees Celsius.
#include <stdio.h>

int main()
{
float celcius, fahrenheit;

printf("\nplease enter the degrees Fahrenheit: ");


scanf("%f", &fahrenheit);

celcius = (fahrenheit - 32) * 5/9;


printf("\ndegrees in celcius = %.2f", celcius);
return 0;
}
8. Write a program that reads in the radius of a circle and prints the circle’s diameter,
circumference and area. Use the constant value 3.14159 for π.

#include <stdio.h>

int main()
{
double radius, diameter, circumference, area;
const double PI = 3.14159; // it is good to use double for this

printf("\nEnter the radius of a circle: ");


scanf("%lf", &radius);

diameter = radius * 2;
circumference = PI * diameter;
area = PI * pow(radius,2);
printf("diameter = %.2f, circumference = %.2f, area = %.2f", diameter, circumference, area);
return 0;
}

9. Write a program that accepts two numbers, divides the first number by the second number
and prints the result.
#include <stdio.h>

int main()
{
float num1, num2, division;
printf("Enter two numbers: \n");
scanf("%f %f", &num1, &num2);

division = num1 / num2;


printf("the division of the two number is %.2f", division);
return 0;
}
10. Write a program that asks the user to enter two integers, obtains the numbers from the
user, then prints the larger number followed by the words “is larger”. If the numbers are
equal, print the message “These numbers are equal”.
#include <stdio.h>

int main()

int number1, number2;

printf("\nEnter the first integer: ");

scanf("%d", &number1);

printf("\nEnter the second integer: ");

scanf("%d", &number2);

if(number1 > number2){

printf("\n%d, is larger", number1);

else if (number2 > number1) {

printf("\n%d, is larger", number2);

else {

printf("\nThese numbers are equal");

return 0;

11. Write a program that inputs three different integers from the keyboard, then prints the sum,
the average, the product, the smallest and the largest of these numbers.
#include <stdio.h>

int main()
{
float number1, number2, number3, sum, average, product;
printf("please enter the first number: ");
scanf("%f", &number1);
printf("please enter the second number: ");
scanf("%f", &number2);
printf("please enter the third number: ");
scanf("%f", &number3);
sum = number1 + number2 + number3;
printf("the sum of these numbers is, %.1f \n", sum);
average = (sum)/3;
printf("the average of these numbers is, %.1f \n", average);
product = number1 * number2 * number3;
printf("the product of these numbers is, %.1f \n", product);
if(number1 > number2{
if(number1 > number3){
printf("%.1f is the largest \n", number1);
}
else {
printf("%.1f is the largest \n", number3);
}
else if(number2 > number1){
if(number2 > number3) {
printf("%.1f is largest \n", number2);
}
else{
printf("%.1f is largest \n", number3);
}
return 0;
}

12. Write a program that displays even numbers between 1 and 100 inclusive.

#include <stdio.h>

int main()
{
int i;
for(i = 1; i <= 100; i++)
{
if(i%2 == 0)
printf("%d \n", i);
}
return 0;
}
13. Write a program that calculates the sum of the first 200 counting integers

#include <stdio.h>

int main()
{
int i , sum = 0;
for(i = 1; i <= 200; i++){
sum += i; // equals to (sum = sum + i)
}
printf("the sum of the first 200 counting integers is, %d", sum);

return 0;
}

14. Write a program that calculates the squares and cubes of the numbers from 0 to 10 and use
tabs to print the following table of values

#include <stdio.h>

int main()
{
int i , square, cube;
printf("number\tsquare\tcube \n");
for(i = 0; i <= 10; i++)
{
square = pow(i,2);
cube = i*i*i;
printf("%d\t%d\t%d \n", i,square,cube);
}
return 0;
}

15. Write a function that receives 5 integers, calculate the sum and average of these numbers.
Call this function from main() and print the results.

#include <stdio.h>

void calculation(double num1, double num2, double num3, double num4, double num5);

int main()
{
calculation(10,10,10,10,10);
return 0;
}

void calculation(double num1, double num2, double num3, double num4, double num5)
{
double sum, average;
sum = num1+num2+num3+num4+num5;
average = sum /5;
printf("sum = %.1f \n", sum);
printf("average = %.1f", average);
}

16. Write a C program that uses function which performs multiplication of 3 times 5, returns the
product and prints out the return value form the function on computer screen.

#include <stdio.h>

int multiplication();

int main()
{
int result = multiplication();
printf("%d", result);
return 0;
}

int multiplication()
{
return 3*5;
}

17. Write a C function to calculate the factorial value of any integer entered through the
keyboard
#include <stdio.h>

int factorialOfNumber(int num); // remember this line tells the main function that factorialOfNumber exists below it

// if you put factorialOfNumber function before main then you don't need this line

int main()

int factorial = factorialOfNumber(3);

printf("%d", factorial);

return 0;

int factorialOfNumber(int num)

int i, factorial = 1;

for(i = num; i >=1; i--) {

factorial= factorial*i;

return factorial;

}
18. Write C Program that accepts input of two numbers. The program contains function
addition for performing addition of two numbers; function subtraction for performing
subtraction of the second number form the first number; function multiplication for
multiplication of the two numbers and function division to divide two numbers.
The program should also give the user an option of performing one of the operations above
and display the result on the screen.

#include <stdio.h>

int addition(int num1, int num2)


{
return num1 + num2;
}

int subtraction(int num1, int num2)


{
return num1 - num2;
}

int multiplication(int num1, int num2)


{
return num1 * num2;
}

double division(int num1, int num2)


{
return (double)num1 / num2; // explicit casting
}

int main()
{
int number1, number2;
char operation;
printf("\nplease choose the operation, a - addition, s - subtraction, m - multiplication, d - division: ");
scanf("%c", &operation);
printf("\nEnter the first number: ");
scanf("%d", &number1)
printf("\nEnter the second number: ");
scanf("%d", &number2);
if(operation == 'a' || operation == 'A' )
{
printf("\nAddition = %d", addition(number1, number2));
}
else if(operation == 's' || operation == 'S')
{
printf("\nSubtraction = %d", subtraction(number1, number2));
}
else if(operation == 'm' || operation == 'M')
{
printf("\nMultiplication = %d", multiplication(number1, number2));
}
else if(operation == 'd' || operation == 'D')
{
printf("\nDivision = %.2f", division(number1, number2));
}
else
{
printf("\ninvalid operation");
}
return 0;
}
19. Write a C program that initializes all 10 array elements to 1.

#include <stdio.h>

int main()
{
int i, arr[10];
for(i = 0; i < 10; i++)
{
arr[i] = 1;
printf("%d \n", arr[i]);
}
return 0;
}

20. Write a C program to read scores of five students using array and print the array elements

#include <stdio.h>

int main()
{
int i, score, studentScores[5];
printf("Enter scores of five students\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &score);
studentScores[i] = score;
}
printf("\nThe array elements are: \n");
for(i = 0; i < 5; i++)
{
printf("%d \n", studentScores[i]);
}
return 0;
}

21. Write C program that displays all 10 array elements on computer screen.

#include <stdio.h>

int main()
{
int i, numbers[10] = {1,2,3,4,5,6,7,8,9,10};

for(i = 0; i < 10; i++)


{
printf("%d \n", numbers[i]);
}
return 0;
}
22. Write a C program that calculates and displays the sum and average of all elments of an
array,
Float numbers[6] = {50,10,20,60,120,90};

#include <stdio.h>

int main()
{
float sum = 0 , average, numbers[6] = {50, 10, 20, 60, 120, 90};
int i; // remember in c, an array subscript must be an integer

for(i = 0; i < 6; i++)


{
sum = sum + numbers[i];
average = sum / 6;
}

printf("sum = %.2f \naverage = %.2f", sum, average);

return 0;
}

You might also like