Programming Basic Questions - 114319
Programming Basic Questions - 114319
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()
scanf("%d", &num1);
scanf("%d", &num2);
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;
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &z);
product = x*y*z;
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()
scanf("%d", &num1);
scanf("%d", &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;
#include <stdio.h>
int main()
{
double radius, diameter, circumference, area;
const double PI = 3.14159; // it is good to use double for this
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);
int main()
scanf("%d", &number1);
scanf("%d", &number2);
else {
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()
printf("%d", factorial);
return 0;
int i, factorial = 1;
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 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};
#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
return 0;
}