Created
August 10, 2022 16:18
-
-
Save trashvin/15469d7db3cb4cab5f57ecd9681b4523 to your computer and use it in GitHub Desktop.
Sample C++ code for loops and conditional expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example program | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
int getAction() | |
{ | |
int action = 0; | |
cout << "Please select an action \n"; | |
cout << "1 - Get sum of all numbers from 1-n \n"; | |
cout << "2 - Get sum of all even numbers from 1-n \n"; | |
cout << "3 - Get sum of all odd numbers from 1-n \n"; | |
cout << "4 - Get sum of all fibonacci numbers from 1-n \n"; | |
cout << "5 - Get sum of all numbers divisible by 3,4 and 5 from 1-n \n"; | |
cout << "Enter your choice : "; | |
cin >> action; | |
return action; | |
} | |
int getSumOfAllNumbers(int number) | |
{ | |
// your code to compute sum goes here... | |
int result = 0; | |
return result; | |
} | |
int getSumOfAllEvenNumbers(int number) | |
{ | |
// your code to compute sum goes here... | |
int result = 0; | |
return result; | |
} | |
int getSumOfAllOddNumbers(int number) | |
{ | |
// your code to compute sum goes here... | |
int result = 0; | |
return result; | |
} | |
int getSumOfAllFibonacciNumbers(int number) | |
{ | |
// your code to compute sum goes here... | |
int result = 0; | |
return result; | |
} | |
int getSumOfAllDivisibleNumbers(int number) | |
{ | |
// your code to compute sum goes here... | |
int result = 0; | |
return result; | |
} | |
int main() | |
{ | |
// deaclare the needed variables | |
int number = 0; | |
char response; | |
bool repeat = true; | |
int action = 0; | |
int answer = 0; | |
bool validAction; | |
// repeat the process while repeat is TRUE | |
while( repeat == true) | |
{ | |
cout << "Please enter a number : "; | |
cin >> number; | |
// show menu and get action | |
action = getAction(); | |
// set it initially to true | |
validAction = true; | |
switch ( action ) | |
{ | |
case 1: | |
answer = getSumOfAllNumbers(number); | |
break; | |
case 2: | |
answer = getSumOfAllEvenNumbers(number); | |
break; | |
case 3: | |
answer = getSumOfAllOddNumbers(number); | |
break; | |
case 4: | |
answer = getSumOfAllFibonacciNumbers(number); | |
break; | |
case 5: | |
answer = getSumOfAllDivisibleNumbers(number); | |
break; | |
default: | |
cout << "ERROR : Invalid Choice \n"; | |
// invalid action, set it to false | |
validAction = false; | |
} | |
// only display the answer for valid actions | |
if (validAction == true) | |
{ | |
cout << "The answer is : " << answer << "\n"; | |
} | |
cout << "Repeat the process (Y/N) ? :"; | |
cin >> response; | |
// test if user entered Y (for yes), if Y then set repeat = TRUE | |
// otherwise, set repeat to FALSE | |
if ( toupper(response) == 'Y' ) repeat = true; | |
else repeat = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment