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 [Link] The conditional statement - more conditional
Possible solution
#include <stdio.h>
int main(void)
{
int a = 10;
if (a > 9)
puts("First condition is true");
else
puts("First condition is false");
if (a < 9)
puts("Second condition is true");
else
puts("Second condition is false");
if (a == 9 + 1)
puts("Third condition is true");
else
puts("Third condition is false");
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Big numbers
Possible solution
#include <stdio.h>
int main(void)
{
int ipAddressPart1, ipAddressPart2, ipAddressPart3, ipAddressPart4;
scanf("%d", &ipAddressPart1);
scanf("%d", &ipAddressPart2);
scanf("%d", &ipAddressPart3);
scanf("%d", &ipAddressPart4);
if (ipAddressPart1 >= 0 && ipAddressPart1 <= 255 &&
ipAddressPart2 >= 0 && ipAddressPart2 <= 255 &&
ipAddressPart3 >= 0 && ipAddressPart3 <= 255 &&
ipAddressPart4 >= 0 && ipAddressPart4 <= 255)
{
unsigned long int ipAddress32Bit = 0;
ipAddress32Bit += ipAddressPart1 * 256 * 256 * 256;
ipAddress32Bit += ipAddressPart2 * 256 * 256;
ipAddress32Bit += ipAddressPart3 * 256;
ipAddress32Bit += ipAddressPart4;
printf("Human readable IP address is: %d.%d.%d.%d\n", ipAddressPart1, ipAddressPart2, ipAddressPart3, ipAddr
printf("IP address as a 32 - bit number : %lu\n", ipAddress32Bit);
}
else
puts("Incorect IP Address.");
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Type conversions: part 1
Possible solution
#include <stdio.h>
int main(void)
{
float gradeAverage;
int finalGrade;
scanf("%f", &gradeAverage);
finalGrade = (int)gradeAverage;
if (finalGrade == 1)
puts("Very bad.");
if (finalGrade == 2)
puts("Bad.");
if (finalGrade == 3)
puts("Neutral.");
if (finalGrade == 4)
puts("Good.");
if (finalGrade == 5)
puts("Very good.");
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Type conversions: part 2
Possible solution
#include <stdio.h>
int main(void)
{
float notExeactFive = 5.4;
float notExeactNumber = 6.7;
int exactFive;
int roundedNumber;
if (notExeactNumber - (int)notExeactNumber > 0.5)
{
roundedNumber = (int)notExeactNumber + 1;
}
else
{
roundedNumber = (int)notExeactNumber;
}
exactFive = (int)notExeactFive;
printf("Five is: %d\n", exactFive);
printf("Rounded to seven: %d\n", roundedNumber);
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Loops: while
Possible solution
#include <stdio.h>
int main(void)
{
int input1;
int input2 = -1;
while(input2 != 0)
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
}
if(input1==99)
puts("Finish.");
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Loops: do - while
Possible solution
#include <stdio.h>
int main(void)
{
int input;
int counter = 0;
scanf("%d", &input);
do
{
int innerCounter=-1;
do
{
printf("*#");
innerCounter++;
} while (counter>innerCounter);
printf("\n");
counter++;
} while (input > counter && counter<20);
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Loops: for
Possible solution
#include <stdio.h>
int main(void)
{
int input;
int i, j;
scanf("%d", &input);
for (i = 0; i < input && i < 20; i++)
{
printf("*");
for (j = 0; j < i; j++)
printf(" ");
printf("*\n");
}
if (input > 20)
input = 20;
for (i = input-1; i >= 0; i--)
{
printf("*");
for (j = 0; j < i; j++)
printf(" ");
printf("*\n");
}
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Logical expressions
Possible solution
#include <stdio.h>
int main(void)
{
int year;
scanf("%d", &year);
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] 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 [Link] Single bits
Possible solution
#include <stdio.h>
int main(void)
{
int input;
int nibbleH, nibbleL;
scanf("%d", &input);
nibbleH = (input >> 4) & 15;
nibbleL = input & 15;
printf("H nibble: %d\n", nibbleH);
printf("L nibble: %d\n", nibbleL);
return 0;
}
© 2016 C++ Institute. All rights reserved. Last updated: September 27, 2016 | [Link] Page 1 of 1 (IV)