TP_2_Answers (1)
TP_2_Answers (1)
TP_2_Answers (1)
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number > 0)
printf("The number is positive\n");
else if (number < 0)
printf("The number is negative\n");
else
printf("The number is zero\n")
}
1. Solution :
2. Parity of Product
Write a program that prompts the user for two numbers and then informs whether their product is negative or
positive. Remark: You should not calculate the product of the two numbers.
2. Solution :
Description:
• This program utilizes the printf function to exhibit messages. The scanf function to gather
user input.
• It verifies whether either of the numbers is zero, as, in that scenario the product would also be
zero.
• If both numbers are either positive or negative then their product will be positive.
• However if one number is positive and the other is negative their product will be negative.
#include <stdio.h>
int main() {
int nbr1, nbr2;
// Ask the user to input two numbers
printf("Enter the first number: ");
scanf("%d", &nbr1);
printf("Enter the second number: ");
scanf("%d", &nbr2);
// Determine the sign of the product
if (nbr1 == 0 || nbr2 == 0) {
printf("The product is zero.\n");
} else if ((nbr1 > 0 && nbr2 > 0) || (nbr1 < 0 && nbr2 < 0)) {
printf("The product is positive.\n");
} else {
printf("The product is negative.\n");
}
return 0;
}
2. Checking if the two variables have different signs
#include <stdio.h>
int main() {
int nbr1, nbr2;
// Ask the user to input two numbers
printf("Enter the first number: ");
scanf("%d", &nbr1);
printf("Enter the second number: ");
scanf("%d", &nbr2);
return 0;
}
3. Solution
Description:
• This program prompts the user to enter the number of photocopies they need.
• The program then calculates the cost of the ten copies, the twenty copies and any
additional copies based on the provided rates (5 DA, 4 DA and 3 DA respectively). The
program presents a breakdown of the cost, for each category ( ten, next twenty additional)
as well as the total price.
• To clearly outline each part of the cost calculation variables such, as first_ten_copies,
next_twenty_copies, additional_copies, first_ten_price, next_twenty_price and
additional_price are utilized.
#include <stdio.h>
int main() {
int copies;
float total_price = 0.0;
float first_ten_price, next_twenty_price, additional_price;
int first_ten_copies, next_twenty_copies, additional_copies;
first_ten_price = first_ten_copies * 5;
next_twenty_price = 0;
additional_price = 0;
} else if (copies <= 30) {
first_ten_copies = 10;
next_twenty_copies = copies - 10;
additional_copies = 0;
first_ten_price = 10 * 5;
next_twenty_price = next_twenty_copies * 4;
additional_price = 0;
} else {
first_ten_copies = 10;
next_twenty_copies = 20;
additional_copies = copies - 30;
first_ten_price = 10 * 5;
next_twenty_price = 20 * 4;
additional_price = additional_copies * 3;
}
return 0;
}
4. Quadratic Equation Solver
Write a program to solve the equation ax2 + bx + c = 0, considering all special cases.
4. Solution
Description :
• The comment block at the beginning explains the overall purpose of the program.
• Each section of the code is preceded by comments that explain what that part of the code
is doing.
• The code is structured to be straightforward and easy to follow, with clear variable names
and step-by-step logic.
• The program handles different cases:
- When 'a' is 0, the equation is linear, not quadratic.
- For quadratic equations, it calculates the discriminant (Delta).
- Based on the discriminant value, the program determines:
* Two real and distinct roots,
* One real and repeated root,
* Or two complex roots.
The user is prompted to input the values of a, b, and c, and the program outputs the roots of
the equation.
/*
Quadratic Equation Solver
This program solves equations of the form ax^2 + bx + c = 0.
*/
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, Delta, x1, x2, R_part, i_part;
// Check if 'a' is zero which means the equation is linear, not quadratic
if (a == 0) {
printf("The equation is not quadratic, but linear.\n");
// If 'b' is also zero, the equation has no solution or infinite solutions
if (b == 0) {
if (c == 0) {
printf("Infinite solutions.\n");
} else {
printf("No solution.\n");
}
} else {
// Calculate and display the root for linear equation
x1 = -c / b;
printf("The solution is: %f\n", x1);
}
} else {
// Calculate the discriminant (Delta)
Delta = b*b - 4*a*c;
return 0;
}
5. Which Day?
Write a program that allows displaying the day corresponding to a number from 1 to 7 entered by the
user. Solve this problem using two methods: nested if statements and a switch statement.
5. Solution
Description :
The program using conditional statements performs the following operations:
1. Takes User Input: It prompts you to enter a number between 1 and 7. This number
represents a day of the week, where 1 is for Monday, 2 for Tuesday, and so on up to 7 for
Sunday.
2. Processes the Input:
• Using Nested if Statements: The program first uses a series of if and else if
statements to determine which day of the week corresponds to the number you
entered. For example, if you enter 1, it identifies it as "Monday".
• Using a switch Statement: After the if statements, it does the same task using a
switch statement. This is a different method to handle multiple conditions. The
program matches your input with a case in the switch (like case 1: for Monday) and
then executes the code associated with that case.
1. Outputs the Result: For both methods (nested if and switch), the program prints out a
message telling you which day of the week corresponds to the number you entered. For
example, if you enter 3, it prints "The date equivalent to the number 3 is: Wednesday".
#include <stdio.h>
int main() {
int day_number;
.
6. Calculator
Write a program that simulates a calculator. The program takes two real numbers entered by the user.
Depending on the choice made from a menu displayed on the screen, the program calculates the
sum, product, average, minimum, or maximum of these two numbers. The menu should be presented
to the user as follows:
**************** MENU *******************
1: ----------> Sum ------------------
2: ----------> Product ----------------
3: ----------> Average ----------------
4: ----------> Minimum ----------------
5: ----------> Maximum ----------------
-----------------------------------------
Enter your choice?
6. Solution
Description :
A. With simple answer to the user
• The program starts by including the stdio.h header file, which is necessary for input and
output functions.
• It then declares float variables real1 and real2 to store the real numbers entered by the
user, and result to store the outcome of the chosen operation.
• The user is prompted to input two real numbers, which are read and stored in real1 and
real2.
• A menu is displayed, showing the available operations. The user is asked to choose one
by entering a corresponding number.
• A switch statement is used to perform the operation based on the user's choice. Each
case in the switch corresponds to an arithmetic operation.
/*
Calculator Program _ Simple Message
- This program performs basic arithmetic operations on two real numbers.
- The user can choose to calculate the sum, product, average, minimum, or maximum.
*/
#include <stdio.h> // Standard Input-Output header for printf and scanf functions
int main() {
float real1, real2, result; // Variables to store the two real numbers and the result
int choice; // Variable to store the user's choice
printf("\n\n");
• The program now includes a character array operation to store the name of the chosen
operation.
• Based on the user's choice in the switch statement, not only is the result calculated, but
the operation string is also updated with the corresponding operation name using
strcpy().
• Finally, the result is displayed with a message that specifies the operation, like "The result
of the Product operation is: ...".
int main() {
float real1, real2, result; // Variables to store the real numbers and the result
int choice; // Variable to store the user's choice
char operation[10]; // String to store the name of the operation
printf("\n\n");