Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views28 pages

Chapter 2

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)
3 views28 pages

Chapter 2

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/ 28

CLA: Programming

Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.1.10.1 Floating point: part 1
Possible solution

#include <stdio.h>

int main()
{
printf("The value of seven is: %f\n", 7.0);
printf("The value of eight and a half is: %f\n", 8.5);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.1.10.2 Floating point: part 2
Possible solution

#include <stdio.h>

int main()
{
float tenValue = 10;
printf("The value of nine is: %f\n", 9.0);
printf("The value of ten is: %f\n", tenValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.1.10.3 Floating point: part 3
Possible solution

#include <stdio.h>

int main()
{
float halfValue = 0.5;
float piValue = 3.14159265;
printf("The value of half is: %f/2\n", halfValue);
printf("The value of Pi is: %f\n", piValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.10.1 Operators: part 1
Possible solution

#include <stdio.h>

int main()
{
float halfValue = 0.6 - 0.1;
float piValue = 0.14159265 + 3;
printf("The value of half is: %f\n", halfValue);
printf("The value of Pi is: %f\n", piValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.10.2 Operators: part 2
Possible solution

#include <stdio.h>

int main()
{
int fourValue = 2 * 2 * 1;
int fiveValue = 2 + 3;
printf("The value of four is: %d\n", fourValue);
printf("The value of five is: %d\n", fiveValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.10.3 Operators: part 3
Possible solution

#include <stdio.h>

int main()
{
float tenValue = 2 * 3 + 4;
float twelveValue = 2 * 2.5 + 2 * 3.5 ;
printf("The value of ten is: %f\n", tenValue);
printf("The value of twelve is: %f\n", twelveValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.10.4 Operators: part 4
Possible solution

#include <stdio.h>

int main()
{
int tenValue = 3 * 8 % 14;
int twentyValue = 2 * tenValue + 10 % 5;
printf("The value of ten is: %d\n", tenValue);
printf("The value of twenty is: %d\n", twentyValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.10.5 Operators: part 5
Possible solution

#include <stdio.h>

int main()
{
int xValue = 4 * 6 % 5;
int eightValue = 2 * xValue + 10 % 5;
printf("The value of eight is: %d\n", eightValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.10.6 Operators: part 6
Possible solution

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 9;
int result;
int bigResult;

xValue = xValue + 3;
yValue = yValue - xValue;
result = xValue * yValue;
result = result + result;
result = result - 1;
yValue = result % result;
result = result + xValue + result;
bigResult= result * result * result;
result = result + xValue * yValue;

printf("result: %d\n", result);


printf("big result: %d\n", bigResult);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.25.1 Priorities and parentheses: part 1
Possible solution

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 9;
int result = (xValue + yValue) * 2;
int bigResult = xValue + yValue * 6;
printf("the result is: %d\n", result);
printf("the big result is: %d\n", bigResult);
return 0;
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.25.2 Priorities and parentheses: part 2
Possible solution

#include <stdio.h>

int main(void)
{
int xValue = 3;
int yValue = 2;
int result = (xValue + yValue) * (2 + yValue);
int smallResult = xValue + yValue * (4 - xValue);
printf("the result is: %d\n", result);
printf("the small result is: %d\n", smallResult);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.25.3 Priorities and parentheses: part 3
Possible solution

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 3;
int result = (xValue % yValue) * (14 % yValue);
int smallResult = xValue + (10 % 4 % xValue);
printf("the result is: %d\n", result);
printf("the small result is: %d\n", smallResult);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.25.4 Priorities and parentheses: part 4 - own computation
Possible solution

#include <stdio.h>

int main(void)
{
float startValue = 100;
float interestRate = 0.015;
float firstYearValue = startValue + startValue * interestRate;
float secondYearValue = firstYearValue + firstYearValue * interestRate;
float thirdYearValue = secondYearValue + firstYearValue * interestRate;
printf("After first year: %f\n", firstYearValue);
printf("After second year: %f\n", secondYearValue);
printf("After third year: %f\n", thirdYearValue);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.2.29.1 Operators and shortcuts
Possible solution

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 9;
int result;
int bigResult;

xValue += 3;
yValue -= xValue;
result = xValue * yValue;
result += result;
result--;
yValue = result % result;
result += xValue + result;
bigResult= result * result * result;
result += xValue * yValue;

printf("result: %d\n", result);


printf("big result: %d\n", bigResult);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.3.19.1 Character type and values: part 1
Possible solution

#include <stdio.h>

int main(void)
{
printf("Diff beetween '%c' and '%c' is : %d\n", 'c', 'a', 'c' - 'a');
printf("Diff beetween '%c' and '%c' is : %d\n", 'a', 'c', 'a' - 'c');
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.3.19.2 Character type and values: part 2
Possible solution

#include <stdio.h>

int main(void)
{
char firstLetter = 'A';
char firstSmallLetter = 'a';
char lastLetter = 'Z';
char lastSmallLetter = 'z';
printf("Upper case letters beetween (and with) '%c' and '%c' is : %d\n",
lastLetter, firstLetter, lastLetter - firstLetter + 1);
printf("Lower case letters beetween (and with) '%c' and '%c' is : %d\n",
lastSmallLetter, firstSmallLetter, lastSmallLetter - firstSmallLetter + 1);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.3.19.3 Character type and values: part 3
Possible solution

#include <stdio.h>

int main(void)
{
char zero = '0';
printf("'1' - '0' is: %d\n", '1' - zero);
printf("'2' - '0' is: %d\n", '2' - '0');
/*You can use both versions - const literal or variable*/
printf("'3' - '0' is: %d\n", '3' - zero);
printf("'4' - '0' is: %d\n", '4' - zero);
printf("'5' - '0' is: %d\n", '5' - zero);
printf("'6' - '0' is: %d\n", '6' - zero);
printf("'7' - '0' is: %d\n", '7' - zero);
printf("'8' - '0' is: %d\n", '8' - zero);
printf("'9' - '0' is: %d\n", '9' - zero);
printf("'0' - '0' is: %d\n", '0' - zero);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.4.17.1 Conditions and conditional executions: part 1
Possible solution

#include <stdio.h>

int main(void)
{
int a = 10;
if (a > 9)
puts("First condition is true");
if (a < 9)
puts("Second condition is true");
if (a == 9 + 1)
puts("Third condition is true");
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.4.17.2 Conditions and conditional executions: part 2
Possible solution

#include <stdio.h>

int main(void)
{
int n = -3;
if (n < 0)
{
printf("The absolute value of %d is %d\n", n, -n);
}
printf("The value of n is %d\n", n);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.4.17.3 Conditions and conditional executions: part 3
Possible solution

#include <stdio.h>

int main(void)
{
int dayOfWeek = 1;
if (dayOfWeek == 1)
puts("The day of the week is: Monday");
if (dayOfWeek == 2)
puts("The day of the week is: Tuesday" );
if (dayOfWeek == 3)
puts("The day of the week is: Wednesday");
if (dayOfWeek == 4)
puts("The day of the week is: Thursday");
if (dayOfWeek == 5)
puts("The day of the week is: Friday");
if (dayOfWeek == 6)
puts("The day of the week is: Saturday");
if (dayOfWeek == 0)
puts("The day of the week is: Sunday");
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.17.1 Printing data: part 1
Possible solution

#include <stdio.h>

int main(void)
{
int day = 20;
int month = 2;
int year = 2016;
printf("%04d-%02d-%02d - YYYY-MM-DD format - ISO 8601\n", year, month, day);
printf("%02d-%02d-%04d - MM-DD-YYYY format\n", month, day, year);
printf("%02d-%02d-%04d - DD-MM-YYYY format\n", day, month, year);
printf("%d-%d-%d - D-M-Y format\n", day, month, year);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.17.2 Printing data: part 2
Possible solution

#include <stdio.h>

int main(void)
{
float studentAYear1 = 4.2;
float studentAYear2 = 4.5;
float studentAYear3 = 4.2;

float studentBYear1 = 4.3;


float studentBYear2 = 4.4;
float studentBYear3 = 4.7;

float studentCYear1 = 4.3;


float studentCYear2 = 4.8;
float studentCYear3 = 4.9;

float studentAAvg = (studentAYear1 + studentAYear2 + studentAYear3) / 3;


float studentBAvg = (studentBYear1 + studentBYear2 + studentBYear3) / 3;
float studentCAvg = (studentCYear1 + studentCYear2 + studentCYear3) / 3;

printf("Student name: 1stYGrade 2ndYGrade 3rdYGrade Avg\n");


printf("Student A %9.2f %9.2f %9.2f %5.2f\n", studentAYear1, studentAYear2, studentAYear3, studentAAvg);
printf("Student B %9.2f %9.2f %9.2f %5.2f\n", studentBYear1, studentBYear2, studentBYear3, studentBAvg);
printf("Student C %9.2f %9.2f %9.2f %5.2f\n", studentCYear1, studentCYear2, studentCYear3, studentCAvg);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.17.3 Printing data: part 3
Possible solution

#include <stdio.h>

int main(void)
{
printf(" ^\n");
printf(" / \\\n");
printf(" / \\\n");
printf("< >\n");
printf(" \\ /\n");
printf(" \\ /\n");
printf(" v\n");
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.22.1 Getting data from the user: part 1
Possible solution

#include <stdio.h>

int main(void)
{
int daysCount;
float valuePi;
printf("How many days in the week: ");
scanf("%d", &daysCount);
printf("The value of Pi to two points: ");
scanf("%f", &valuePi);
printf("There are %d in a week.\n", daysCount);
printf("Pi value is %f.\n", valuePi);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.22.2 Getting data from the user: part 2
Possible solution

#include <stdio.h>

int main(void)
{
float valueA;
float valueB;
printf("Value A: ");
scanf("%f", &valueA);
printf("Value B: ");
scanf("%f", &valueB);
printf("%f + %f = %f.\n", valueA, valueB, valueA + valueB);
printf("%f - %f = %f.\n", valueA, valueB, valueA - valueB);
printf("%f * %f = %f.\n", valueA, valueB, valueA * valueB);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.22.3 Getting data from the user: part 3
Possible solution

#include <stdio.h>

int main(void)
{
int day, month;
puts("day:");
scanf("%d", &day);
puts("month:");
scanf("%d", &month);
int dayOfYear = 0;
if (month > 1)
dayOfYear += 31;
if (month > 2)
dayOfYear += 29;
if (month > 3)
dayOfYear += 31;
if (month > 4)
dayOfYear += 30;
if (month > 5)
dayOfYear += 31;
if (month > 6)
dayOfYear += 30;
if (month > 7)
dayOfYear += 31;
if (month > 8)
dayOfYear += 31;
if (month > 9)
dayOfYear += 30;
if (month > 10)
dayOfYear += 31;
if (month > 11)
dayOfYear += 30;
dayOfYear += day;
printf("The day of the year : %d.\n", dayOfYear);
return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.22.4 Getting data from the user: part 4
Possible solution

#include <stdio.h>

int main(void)
{
int dayOfWeek;
scanf("%d", &dayOfWeek);
if (dayOfWeek == 1)
puts("The day of the week is: Monday");
if (dayOfWeek == 2)
puts("The day of the week is: Tuesday" );
if (dayOfWeek == 3)
puts("The day of the week is: Wednesday");
if (dayOfWeek == 4)
puts("The day of the week is: Thursday");
if (dayOfWeek == 5)
puts("The day of the week is: Friday");
if (dayOfWeek == 6)
puts("The day of the week is: Saturday");
if (dayOfWeek == 0)
puts("The day of the week is: Sunday");
if (dayOfWeek>6)
printf("There is no such day : %d. Input value must be from 0 to 6.\n", dayOfWeek);
if (dayOfWeek<0) /*student may use some other form of conditional statement / statements*/
printf("There is no such day : %d. Input value must be from 0 to 6.\n", dayOfWeek);

return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)
CLA: Programming
Essentials in C C++ INSTITUTE - PROGRAM YOUR FUTURE

INSTRUCTOR VERSION
Instructor note: The instructor copy of the lab contains a possible solution to the scenario. This is only an
example of how you can do it. Sometimes, there may be multiple ways to do an exercise, and the solution
here may not necessarily be the only, or even the best, way.
Lab 2.5.22.5 Getting data from the user: part 5
Possible solution

#include <stdio.h>

int main(void)
{
int day, month, year;
puts("day:");
scanf("%d", &day);
puts("month:");
scanf("%d", &month);
puts("year:");
scanf("%d", &year);
int dayOfYear = 0;
if (month > 1)
dayOfYear += 31;
if (month > 2)
{
if (year % 400 == 0)
dayOfYear += 29;
else if (year % 100 == 0)
dayOfYear += 28;
else if (year % 4 == 0)
dayOfYear += 29;
}
if (month > 3)
dayOfYear += 31;
if (month > 4)
dayOfYear += 30;
if (month > 5)
dayOfYear += 31;
if (month > 6)
dayOfYear += 30;
if (month > 7)
dayOfYear += 31;
if (month > 8)
dayOfYear += 31;
if (month > 9)
dayOfYear += 30;
if (month > 10)
dayOfYear += 31;
if (month > 11)
dayOfYear += 30;
dayOfYear += day;
printf("The day of the year : %d.\n", dayOfYear);

return 0;
}

© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | www.cppinstitute.org Page 1 of 1 (IV)

You might also like