0% found this document useful (0 votes)
7 views12 pages

TP1 Answers

Uploaded by

fareslaadjal2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

TP1 Answers

Uploaded by

fareslaadjal2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Larbi Tebessi University- Tebessa

Department of mathematics and computer science, L1

Algorithms and data structure

Lab1: Getting Started with C Programming


Identifiers

1. Which of the following identifiers are accepted by the C language to name variables? Explain why or why
not.

1) age 2) var1 3) _MOYENNE_du_BAC_ 4) N"tel 5) lim_supérieure 6) speed-max 7) 3numbers 8) Age 9)


time average 10) année 11) prix.TTC 12) double.

Identifier Valid Invalid Explanation

age X only letters, and no special characters.

var1 X Contains letters and digits, and doesn't start with a digit.

_MOYENNE_du_BAC_ X Contains underscores, it's valid because underscores are allowed.

N"tel X The " (double quote) is not allowed in C identifiers.

lim_supérieure X The é (accented character) is not allowed in identifiers.

speed-max X The - (hyphen) is not allowed in C identifiers.

3numbers X Identifiers cannot start with a digit.

Age X Note that C is case-sensitive, Age is different from age.

time average X Identifiers cannot contain spaces.

année X The accented character é is not allowed in C identifiers.

prix.TTC X The . (dot) is not allowed in identifiers.

double X double is a reserved keyword in C and cannot be an identifier.


2. Compare your answer with that of the Code::Blocks compiler. Declare integer variables (int
variable_name;) using the above identifiers, then compile the program, and analyze and correct any
errors if necessary.

3. Give an initial value to each of these variables (int variable_name = value;).

4. Add a printf ("% d", variable_name); statement.

5. Compile and run the program.

#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 = 20; // Change age to 20
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

// Output each modified variable's value:


printf("age = %d\n", age);
printf("var1 = %d\n", var1);
printf("_MOYENNE_du_BAC_ = %d\n", _MOYENNE_du_BAC_);
printf("Age = %d\n", Age);
printf("NTel = %d\n", NTel);
printf("lim_superieure = %d\n", lim_superieure);
printf("speed_max = %d\n", speed_max);
printf("numbers3 = %d\n", numbers3);
printf("time_average = %d\n", time_average);
printf("annee = %d\n", annee);
printf("prix_TTC = %d\n", prix_TTC);
printf("double_value = %d\n", double_value);

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;

// Output each variable's value to check initialization


printf("age = %d\n", age);
printf("var1 = %d\n", var1);
printf("_MOYENNE_du_BAC_ = %d\n", _MOYENNE_du_BAC_);
printf("Age = %d\n", Age);
printf("NTel = %d\n", NTel);
printf("lim_superieure = %d\n", lim_superieure);
printf("speed_max = %d\n", speed_max);
printf("numbers3 = %d\n", numbers3);
printf("time_average = %d\n", time_average);
printf("annee = %d\n", annee);
printf("prix_TTC = %d\n", prix_TTC);
printf("double_value = %d\n", double_value);

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

// Output each modified variable's value:


printf("age = %d\n", age);
printf("var1 = %d\n", var1);
printf("_MOYENNE_du_BAC_ = %d\n", _MOYENNE_du_BAC_);
printf("Age = %d\n", Age);
printf("NTel = %d\n", NTel);
printf("lim_superieure = %d\n", lim_superieure);
printf("speed_max = %d\n", speed_max);
printf("numbers3 = %d\n", numbers3);
printf("time_average = %d\n", time_average);
printf("annee = %d\n", annee);
printf("prix_TTC = %d\n", prix_TTC);
printf("double_value = %d\n", double_value);

return 0;
}

Compile and run. At the end, your program should look like this:

# include < stdio .h >


int main ( )
{
int age = 18;
// ... to be completed
age = 21;
// ... to be completed
printf ("%d", age ) ; // ... to be completed

return 0;
}
Format specifiers
1. Create a new project.
2. Copy this program into the main.c file.

# include < stdio .h >


int main ( )
{
int num = 42;
double pi =
3.14159265;
char letter = ’A’;

printf (" Integer : %d\n", num ) ;


printf (" Floating - Point : %f\n",
pi) ;
printf (" Character : %c\n", letter ) ;

printf (" Hello world \n") ;


printf (" Hello %s\n", "
world ") ;
printf ("%s\n", " Hello world ") ;

printf ("num : %5d\n", num ) ;


printf ("num : %05d\n", num ) ;

printf ("pi: %2.2f\n", pi) ;


printf ("pi: %1.4f\n", pi) ;
printf ("pi: %3.3f\n", pi) ;

return 0;
}

3. Compile and run your project.


4. Analyze and explain the result.
Various data types and scanf() function usage

1. Create a new project.

2. Copy the following program into the main.c file.

#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);

printf("Please enter a decimal (float) value:\n");


scanf("%f", &fvalue);

printf("Please enter a character value:\n");


scanf(" %c", &cvalue);

printf("Please enter a double value:\n");


scanf("%lf", &dvalue);

// Displaying the entered values


printf("\nEntered Integer value is: %d\n", ivalue);
printf("\nEntered Float value is: %f\n", fvalue);
printf("\nEntered Character value is: %c\n", cvalue);
printf("\nEntered Double value is: %lf\n", dvalue);

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;

// Prompt the user to enter two numbers


printf("Enter any two numbers separated by a comma: ");
scanf("%f,%f", &num1, &num2);

// Perform and display arithmetic operations


printf("The sum of the given numbers is: %.2f\n", num1 + num2);
printf("The difference of the given numbers is: %.2f\n", num1 - num2);
printf("The product of the given numbers is: %.2f\n", num1 * num2);

// Handle division carefully to avoid division by zero


if (num2 != 0) {
printf("The quotient of the given numbers is: %.6f\n", num1 / num2);
} else {
printf("Division by zero is not allowed.\n");
}

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() {

int angle1, angle2, angle3;

// Prompt the user to enter the first two angles

printf("Enter the first angle of the triangle: ");

scanf("%d", &angle1);

printf("Enter the second angle of the triangle: ");

scanf("%d", &angle2);

// Calculate the third angle

angle3 = 180 - (angle1 + angle2);

// Display the third angle

printf("The third angle of the triangle is: %d degrees\n", angle3);

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;

// Prompt the user to enter the number of days


printf("Enter the number of days: ");
scanf("%d", &total_days);

// Convert days to years, weeks, and remaining days


years = total_days / 365; // 1 year = 365 days
total_days = total_days % 365; // Remaining days after calculating years

weeks = total_days / 7; // 1 week = 7 days


days = total_days % 7; // Remaining days after calculating weeks

// Display the result


printf("%d years, %d weeks, and %d days\n", years, weeks, days);

return 0;
}

You might also like