0% found this document useful (0 votes)
51 views

LAB 5: Arithmetic Operators, Compound Assignments & Escape Sequence

This lab covers arithmetic operators, compound assignments, and escape sequences. The objectives are for students to understand how to use these tools. The lab contains 5 tasks that have students: 1) Calculate ages 5 years in the past and future. 2) Print shapes using escape sequences. 3) Determine if one number is a multiple of another. 4) Calculate and optionally decrement BMI based on inputs. 5) Reverse a 3-digit number.

Uploaded by

Neena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

LAB 5: Arithmetic Operators, Compound Assignments & Escape Sequence

This lab covers arithmetic operators, compound assignments, and escape sequences. The objectives are for students to understand how to use these tools. The lab contains 5 tasks that have students: 1) Calculate ages 5 years in the past and future. 2) Print shapes using escape sequences. 3) Determine if one number is a multiple of another. 4) Calculate and optionally decrement BMI based on inputs. 5) Reverse a 3-digit number.

Uploaded by

Neena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB 5: Arithmetic operators, compound assignments & Escape

Sequence
Introduction:
In this lab experiment, the students will learn how to use arithmetic operators and
compound assignments and will learn how to use different escape sequences for
formatting and printing different shapes.

Objective:
After completing this lab, the students will be able to have better understanding of
arithmetic operators, compound assignments and escape sequence.

TASK 1:
Write a program that asks the user for their age. Then display what their age would be
five years from now. Then display what their age was five years ago.
#include<stdio.h>

int main()

{
int yourAge = 0;

int ageAfter5Years, age5YearsAgo;

printf("How old are you?", yourAge);

scanf_s("%d", &yourAge); //asks the user to enter age

ageAfter5Years = yourAge + 5; //calculates age after 5 years

printf("In five years, your age will be %d years old.\n", ageAfter5Years);

age5YearsAgo= yourAge - 5; //calculates age 5 years ago

printf("And five years ago, you were %d!", age5YearsAgo);

return 0;

}
TASK 2:
Write a program that prints the following shapes with asterisks. (Use escape
sequences)

/*prints different shapes using escape sequences*/


#include<stdio.h>

int main()
{
printf("*********\t***\t\t*\t\t*\n");

printf("*\t* * *\t ***\t * *\n");

printf("*\t* * \t *\t\t\b\b*****\t\t\b\b\b*\t\t\b\b\b\b\b*\n");

printf("*\t*\t\b\b\b*\t\t\b\b\b*\t\t*\t\t\b\b\b\b*\t\t\b\b\b\b*\n");

printf("*\t*\t\b\b\b*\t\t\b\b\b*\t\t*\t\t\b\b\b\b\b*\t\t\b\b\b*\n");

printf("*\t*\t\b\b\b*\t\t\b\b\b*\t\t*\t\t\b\b\b\b*\t\t\b\b\b\b*\n");

printf("*\t*\t\b\b\b*\t\t\b\b\b*\t\t*\t\t\b\b\b*\t\t\b\b\b\b\b*\n");

printf("*\t* * *\t\t*\t\t\b\b*\t *\n");


printf("*********\t***\t\t*\t\t*");

return 0;

TASK 3:
Write a program that reads in two integers and determines and prints whether the first is
a multiple of the second. [Hint: Use the remainder operator.]
//determines whether a number is a multiple of the other
#include<stdio.h>

int main()
{
int x, y, z;

printf("Enter the value of x:");

scanf_s("%d", &x); //asks the user to enter x

printf("Enter the value of y:");

scanf_s("%d", &y); //asks user to enter x

z = x % y;

if(z==0)

printf("x is a multiple of y");

else
printf("x is not a multiple of y");

return 0;

For x=5,y=7

For x=8,y=2
TASK 4:
Write a program that calculates BMI. You are supposed to take the input weight in kgs
and height in feet and inches. Convert the height in feet and inches entered by user in
meters and calculate the BMI. If your BMI is greater than 25 decrement it by 1. (Use the
decrement operator)
//prints and calculates the BMI
#include<stdio.h>

int main()

float weightInkg, heightInInches, heightInFeet, heightInMeters, BMI;

printf("Enter the value of weight:");


scanf_s("%f", &weightInkg);
printf("Enter the value of height in inches:");
scanf_s("%f", &heightInInches);

printf("Enter the value of height in feet:");


scanf_s("%f", &heightInFeet);

heightInMeters = (heightInInches + (heightInFeet / 12.0)) / 3.281;

printf("Enter the value of height in meters:%f\n", heightInMeters);


BMI = weightInkg / (heightInMeters * heightInMeters);
if (BMI > 25) {
printf("BMI is %f", --BMI); //if BMI>25, this statement will reduce it
by 1
}
else
{
printf("BMI is %f", BMI);
}
return 0;
}

For BMI>25

For BMI<25
TASK 5:
Write a program that asks the user to enter to enter a three-digit number, then prints the
number with its digits reversed. A session with the program should have the following
appearance:
Enter a three-digit number: 698
The reversal is: 896
Hint: Use the reminder and division operators to obtain individual digits.

//prints a number as well as its reversal


#include<stdio.h>

int main()

int num = 0;
int x, y, z;

printf("Enter a three-digit number: ");

scanf_s("%d", &num);

x = num / 100;

y = (num % 100) / 10;

z = (num % 10) % 10;


printf("The reversal is: %d%d%d", z, y, x); //prints the reversal

return 0;
}

You might also like