rahmu wroteIt's never a bad thing to do basic exercises. More complex doesn't always mean more difficult. It'd be great if you could share the exercises and the solutions you wrote (this is actually how I started the exercise trend).
OK there you go:
Q. Write a program to round an integer i to the next largest multiple of another integer j.
For example, if the user enters:
i=256 and j=7 then the output should be 259
#include <stdio.h>
void main() {
int i, remainder;
printf("Please enter the first number\n");
scanf("%d", &i);
printf("Please enter the divisor\n");
scanf("%d",&j);
remainder = i % j;
i = i - remainder;
i = i + j;
printf("The next multiple is %d", i);
getchar();
getchar();
}
Q. Write a program that reads an integer representing minutes and then outputs the number of corresponding hours, and the number of remaining minutes.
For example if the user enters 172 for the minutes then the program should output:
172 minutes contain 2 hours and 52 minutes.
#include <stdio.h>
void main() {
int i, minutes, hours;
printf("Please enter the number of minutes.\n");
scanf("%d", &i);
hours = i / 60;
minutes = i % 60;
printf("%d contains %d hours and %d minutes.", i, hours, minutes);
getchar();
getchar();
}
Q. Write a program that asks the user to enter any number and then outputs if this number is divisible by 2, 3, 5 and 9.
For example, if the user enters 1911, the output should be:
1911 is not divisible by 2
1911 is divisible by 3
1911 is not divisible by 5
1911 is not divisible by 9
#include <stdio.h>
void main() {
int i;
printf("Please enter a number.\n");
scanf("%d", &i);
if(i % 2 == 0)
printf("%d is divisible by 2\n", i);
if(i % 3 == 0)
printf("%d is divisible by 3\n", i);
if(i % 5 == 0)
printf("%d is divisible by 5\n", i);
if(i % 9 == 0)
printf("%d is divisible by 9\n", i);
getchar();
getchar();
}
Q. Write a program which allows the user to enter 2 integers. It should then give him the option of adding them, subtracting them, multiplying them or dividing them. It should then print the result.
#include <stdio.h>
void main() {
int i, j, option;
float result;
printf("Please enter the first number.\n");
scanf("%d", &i);
printf("Please enter the second number.\n");
scanf("%d", &j);
printf("Choose from the following:\n- 0 to add the numbers\n- 1 to substract them\n- 2 to multiply them\n- 3 to divide them\n");
scanf("%d", &option);
if(option == 0)
result = i + j;
else if(option == 1)
result = i - j;
else if(option == 2)
result = i * j;
else if(option == 3)
result = i / j;
else
printf("This was not a valid option");
printf("The result is %f", result);
getchar();
getchar();
}
Q. Write a program that calculates the insurance fee to pay for a pet according to the following rules:
• A dog that has been neutered costs $50.
• A cat that has been neutered costs $40.
• A bird or reptile costs nothing.
• Any other animal generates an error message.
An animal that has not been neutered costs 20.67% more.
The program should prompt the user for the appropriate information, using a code to determine the kind of animal (i.e. D or d represents a dog, C or c represents a cat, B or b represents a bird, R or r represents a reptile, and anything else represents some other kind of animal) and another code (Boolean 1/0) to know if it has been neutered, and should print the kind of animal, if it has been neutered and the cost(with Zero decimal places of accuracy).
For example, if the user enters d or D and 0
Then the output will be the following (same table):
#include <stdio.h>
void main() {
char type;
int neutered;
int cost;
printf("Enter the type of your pet\n");
scanf("%c", &type);
printf("Is your pet neutered?\nPress 1 for yes, 0 for no\n");
scanf("%d", &neutered);
//I cannot use ctype.h and string.h library functions because they are out of the scope of my cousin's exam
if(type == 'D' || type == 'd')
cost = 50;
else if(type == 'C' || type == 'c')
cost = 40;
else if(type == 'B' || type == 'b' || type == 'R' || type == 'r')
cost = 0;
else {
printf("ERROR: the entered type of animal is not valid");
return;
}
if(neutered == 0)
cost = cost + (cost * 0.2067);
printf("************************************\n");
printf("Kind of animal\t*Neutered*\tCost\t*\n");
printf("************************************\n");
if(type == 'D' || type == 'd')
printf("*Dog\t*\t%d\t*\t%d\t*", neutered, cost);
else if(type == 'C' || type == 'c')
printf("*Cat\t*\t%d\t*\t%d\t*", neutered, cost);
else if(type == 'B' || type == 'b')
printf("*Bird\t*\t%d\t*\t%d\t*", neutered, cost);
else if(type == 'R' || type == 'r')
printf("*Reptile\t*\t%d\t*\t%d\t*", neutered, cost);
getchar();
getchar();
}
Q. Write a while loop to print out the even numbers from i to j inclusive(i and j are entered by the user)
#include <stdio.h>
void main() {
int i,j;
printf("Please enter two numbers\n");
scanf("%d%d", &i, &j);
while(i <= j) {
if(i % 2 == 0)
printf("%d\n", i);
i++;
}
getchar();
getchar();
}
Q. Write a program that uses a while loop to output the multiplication table of a number on the screen.
#include <stdio.h>
void main() {
int i,j;
printf("Please enter a number\n");
scanf("%d", &j);
i = 0;
while(i <= j) {
printf("%d x %d = %d\n", i, j, i * j);
i++;
}
getchar();
getchar();
}
Q. Write a calculator which allows for + - * / = and allows the user to enter 2 numbers and
computes the result. Use a while loop to permit him to continue using the calculator until he
types # to quit.
#include <stdio.h>
void main() {
float i, j, result;
int option;
char A = 'a';
do
{
printf("Enter two numbers\n");
scanf("%f%f", &i, &j);
printf("Choose one of the following operations:\n");
printf("1. Addition\t\t2.Substraction\n3. Multiplication\t\t4. Division\n");
scanf("%d", &option);
switch(option) {
case 1:
result = i + j;
break;
case 2:
result = i - j;
break;
case 3:
result = i * j;
break;
case 4:
result = i / j;
break;
default:
printf("Error!");
return;
}
printf("The answer is: %f\n", result);
printf("Press # to quit, or anything else to continue");
scanf("%c", &A);
} while(A != '#');
}
Q. Calculate the value of Pi from the infinite series
Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
Ask the user how many terms of the series he wants to use to calculate Pi and produce the correct result for him.
#include <stdio.h>
void main() {
int i = 1, counter = 0, n;
double PI = 0;
printf("How many terms are required to calculate the value of PI?\n");
scanf("%d", &n);
while(counter < n) {
if(counter % 2 == 0)
PI = PI + 4.0/i;
else
PI = PI – 4.0/i;
i = i + 2;
counter++;
}
printf("The value of PI is: %f", PI);
getchar();
getchar();
}
P.S: I will add more exercises when he comes back, and when I finish studying for my Data Structures partial exam.
Comments on my code are more than welcome!