ECGR 2103 Spring 2016 Lab 1 20 Points Due by 1159pm on 10131
ECGR 2103 Spring 2016 Lab #1 20 Points Due by 11:59pm on 10/13/16
I. For each of the following programs: a. Write, compile, and run the code i. Comment your program where appropriate (1 point/ program) b. Take a screenshot of your code and the output c. Paste your code and output into a single document d. Save your document as a PDF file (1 point) e. Submit your assignment on Moodle
1. Write a program that asks the user to enter two numbers. The program should use the conditional operator/expression to determine which number is the smaller and which is the larger of the two. [2 points]
2. Write a program that asks the user to enter a number within the range of numbers equivalent to the lowercase alphabet. Use a switch statement to display the alphabet version of that number. [5 points]
3. Create a change-counting game that asks the user to enter what coins to use to make exactly one dollar. The program should ask the user to enter the number of pennies, nickels, dimes, and quarters. If the total value of the coins inserted is equal to one dollar the program should congratulate the user for winning the game. Otherwise, the program should display a message indicating whether the amount entered was more or less than one dollar. Use constant variables to hold the coin values. [5 points]
4. Write code that lets the user enter a number and a letter. Both should be increased until the end of the alphabet is reached and printed. Use a while loop. [3 points]
5. Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number that can only be divided by one and itself (i.e. 3, 5, 7, 11, 13...) [5 points]
Hint: One way to solve this problem is to use a doubly nested loop. The outer loop can iterate from 3 to 100, while the inner loop checks to see whether the counter value for the outer loop is prime. One way to decide whether the number n is prime is to loop from 2 to n-1; if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n-1 evenly divide n, then n must be prime.
Solution
1.
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
int large=a>b?a:b;
int small=a<b?a:b;
cout<<\"large \"<<large<<\" small \"<<small;
return 0;
}
2.
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<\"Enter number between 97 & 122\ \";
cin>>num;
cout<<\"Character Equivalent : \";
switch(num)
{
case 97: cout<<\"a\";break;
case 98: cout<<\"b\";break;
case 99: cout<<\"c\";break;
case 100: cout<<\"d\";break;
case 101: cout<<\"e\";break;
case 102: cout<<\"f\";break;
case 103: cout<<\"g\";break;
case 104: cout<<\"h\";break;
case 105: cout<<\"i\";break;
case 106: cout<<\"j\";break;
case 107: cout<<\"k\";break;
case 108: cout<<\"l\";break;
case 109: cout<<\"m\";break;
case 110: cout<<\"n\";break;
case 111: cout<<\"o\";break;
case 112: cout<<\"p\";break;
case 113: cout<<\"q\";break;
case 114: cout<<\"r\";break;
case 115: cout<<\"s\";break;
case 116: cout<<\"t\";break;
case 117: cout<<\"u\";break;
case 118: cout<<\"v\";break;
case 119: cout<<\"w\";break;
case 120: cout<<\"x\";break;
case 121: cout<<\"y\";break;
case 122: cout<<\"z\";break;
}
return 0;
}
4.
#include<iostream>
using namespace std;
int main()
{
int num;
char ch;
cin>>num>>ch;
if(ch>=65 && ch<=90)
{
while(ch<=90)
{
num++;
ch++;
}
cout<<ch<<\" \"<<num;
}
else
if(ch>=97 && ch<=122)
{
while(ch<=122)
{
num++;
ch++;
}
cout<<ch<<\" \"<<num;
}
return 0;
}

