TP1 Answers
TP1 Answers
1. Which of the following identifiers are accepted by the C language to name variables? Explain why or why
not.
var1 X Contains letters and digits, and doesn't start with a digit.
#include <stdio.h>
int main() {
// Valid declarations with initial values:
int age = 18;
int var1 = 34;
int _MOYENNE_du_BAC_ = 10;
int Age = 42;
int NTel = 551234567;
int lim_superieure = 5000;
int speed_max = 100;
int numbers3 = 33;
int time_average = 30;
int annee = 2024;
int prix_TTC = 150;
int double_value = 16;
return 0;
}
6. Modify the program to display each value on a new line.
#include <stdio.h>
int main() {
// Valid declarations with initial values:
int age = 18;
int var1 = 34;
int _MOYENNE_du_BAC_ = 10;
int Age = 42;
int NTel = 551234567;
int lim_superieure = 5000;
int speed_max = 100;
int numbers3 = 33;
int time_average = 30;
int annee = 2024;
int prix_TTC = 150;
int double_value = 16;
return 0;
}
7. Then, modify the values of these variables through assignments in the program (before the printf
functions).
(variable_name = value;)
#include <stdio.h>
int main() {
// Valid declarations with initial values:
int age = 18;
int var1 = 34;
int _MOYENNE_du_BAC_ = 10;
int Age = 42;
int NTel = 551234567;
int lim_superieure = 5000;
int speed_max = 100;
int numbers3 = 33;
int time_average = 30;
int annee = 2024;
int prix_TTC = 150;
int double_value = 16;
// Modify the values of the variables through assignments:
age = 21; // Change age to 21
var1 = 50; // Change var1 to 50
_MOYENNE_du_BAC_ = 12; // Change _MOYENNE_du_BAC_ to 12
Age = 45; // Change Age to 45
NTel = 551234899; // Change NTel to a different value
lim_superieure = 6000; // Change lim_superieure to 6000
speed_max = 150; // Change speed_max to 150
numbers3 = 100; // Change numbers3 to 100
time_average = 45; // Change time_average to 45
annee = 2025; // Change annee to 2025
prix_TTC = 200; // Change prix_TTC to 200
double_value = 32; // Change double_value to 32
return 0;
}
Compile and run. At the end, your program should look like this:
return 0;
}
Format specifiers
1. Create a new project.
2. Copy this program into the main.c file.
return 0;
}
#include <stdio.h>
int main()
{
int ivalue;
float fvalue;
char cvalue;
double dvalue;
// Taking inputs
printf("Please enter an integer value:\n");
scanf("%d", &ivalue);
return 0;
}
Basic Arithmetic Operations
Write a C program that prompts the user to enter two numbers and then performs four basic arithmetic
operations on those numbers: addition, subtraction, multiplication, and division. The program should
then
display the results of these operations to the user.
Example Output:
Enter any two numbers separated by comma : 10 ,5
The sum of the given numbers is : 15
The difference of the given numbers is : 5
The product of the given numbers is : 50
The quotient of the given numbers is : 2.000000
Answer :
#include <stdio.h>
int main() {
float num1, num2;
return 0;
}
Finding the Third Angle of a Triangle
Based on the fact that the sum of the angles in any triangle is always equal to 180 degrees, write a C
program that calculates and displays the third angle of a triangle when two angles are given as input
by the user. Example Output:
Enter the first angle of the triangle : 60
Enter the second angle of the triangle : 80
The third angle of the triangle is : 40 degrees
Answer
The C program :
#include <stdio.h>
int main() {
scanf("%d", &angle1);
scanf("%d", &angle2);
return 0;
}
Days Conversion
Write a C program that prompts the user to enter a number of days. The program should then convert
the number of days to years, weeks, and remaining days, ignoring leap years. The program should then
display the
converted time to the user.
Example Output:
Enter the number of days : 1234
3 years, 24 weeks and 2 days .
#include <stdio.h>
int main() {
int total_days, years, weeks, days;
return 0;
}