Here's what I'm trying to do, and the errors that I'm getting.
it's a decent amount of code. Would like help
1) Write a loop that will print all the whole numbers from 1 and 100. - works
2) Write a loop that will add all the whole numbers from 1 and 100 and print the total. - works
3) Write a loop that will add all the ODD whole numbers from 7 and 313 and print the total.
getting 29690 as the answer
4) Write a loop that will add all the EVEN whole numbers from -2 and -146 and print the total.
getting 29690 as the answer (the same as the previous one)
5) Write a loop that will add every 3rd number from 2000 and -60 and print the total.
getting 695393 as the answer
6) Write a loop that will prompt the user to enter a number from 1 to 100. The program will continue to loop until a number within that range is entered. After the loop, print out the square root of the number. Be sure to test the loop by entering numbers outside the range. - answer is correct
7) Write a loop that will prompt the user to enter test scores from 0 to 100. The program will continue to loop until a -1 is entered. Sum all test scores entered that are in the range 0 to 100 (inclusive). After the loop, calculate the average and print out the letter grade. Assume a 10-point grading scale.
- not returning the correct average or letter grade
8) If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write the code that will calculate and print the sum of all the multiples of 3 or 5 below 1000.
- not returning the correct answer
9) Write a loop that will prompt the user to enter an uppercase letter. The code should continue to loop until an uppercase letter is entered. After an uppercase letter is entered print out the letter in both uppercase and lowercase. You can't use the built-in tolower function.
- not returning the correct answer (unsure of how to convert the uppercase to lowercase)
Hangman. Make a constant in your code with a value from 1 to 100. Prompt the user to guess the number. If the user guesses correctly end the game and display a "Congratulation. You won." message. If the guess is too high print "Too high." If the guess is too low print "Too low." Give the user at most eight guesses. If the user hasn't guessed the number after eight guesses print a "You lose." message.
- not going through all of the steps and just returning the first step regardless, also not stopping loop after 8 guesses or after guessing correctly
Code:
// ------------------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
// ------------------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------------------
const long lngARRAY_SIZE = 100;
// ------------------------------------------------------------------------------------------
// Prototypes
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Name: main
// Abstract: This is where the program starts
// ------------------------------------------------------------------------------------------
void main()
{
int intIndex = 0;
int intTotal = 0;
int intAverage = 0;
const int constGradeRunningTotal = 0;
char chrLetter = ' ';
int intSquareRoot = 0;
int intNumberChosen = 45;
int intNumberEntered = 0;
int intCount = 0;
// --------------------------------------------------------------------------------
// Problem #1 - Print all the whole numbers from 1 to 100.
// --------------------------------------------------------------------------------
printf("Problem #1 - Print all the whole numbers from 1 to 100.\n");
for (intIndex = 1; intIndex <= 100; intIndex += 1)
{
printf("%d, ", intIndex);
//move to a new line every 10 numbers
if (intIndex % 10 == 0)
{
printf("\n");
}
}
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #2 - Print Total of the sum of all numbers 1 to 100
// --------------------------------------------------------------------------------
printf("Problem #2 - Print the sum of all the whole numbers from 1 to 100.\n");
for (intIndex = 1; intIndex <= 100; intIndex += 1)
{
intTotal += intIndex;
}
printf("The sum of all whole numbers 1 to 100 is %d, ", intTotal);
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #3 - Print the sum of all ODD numbers 7 to 313
// --------------------------------------------------------------------------------
printf("Problem #3 - Print all the sum of all odd numbers from 7 to 313.\n");
for (intIndex = 7; intIndex <= 313; intIndex += 2)
{
if (intIndex % 2 != 0)
{
intTotal += intIndex;
}
}
printf("The sum of all whole odd numbers 7 to 313 is %i, ", intTotal);
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #4 - Print the sum of all EVEN whole numbers -2 to -146
// --------------------------------------------------------------------------------
printf("Problem #4 - Print the sum of all even whole numbers from -2 to -146.\n");
for (intIndex = -2; intIndex <= -146; intIndex += 2)
{
if (intIndex % 2 == 0)
{
intTotal += intIndex;
}
}
printf("The sum of all whole even numbers -146 to -2 is %i, ", intTotal);
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #5 - Print the sum of every 3rd number from -60 to 2000
// --------------------------------------------------------------------------------
printf("Problem #5 - Print the sum of every 3rd whole number from -60 to 2000.\n");
for (intIndex = -60; intIndex <= 2000; intIndex += 3)
{
intTotal += intIndex;
}
printf("The sum of sum of every 3rd whole number from -60 to 2000 is %d, ", intTotal);
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #6 - Print the square root of a number within the range of 1 to 100
// --------------------------------------------------------------------------------
printf("Problem #6 - Enter a number from 1 to 100:\n");
scanf_s("%i", &intIndex);
//is number between 1 and 100?
if (intIndex >= 1 & intIndex <= 100)
{
intSquareRoot = intIndex * intIndex;
}
else
{
printf("The number mus be between 1 and 100:\n");
scanf_s("%i", &intIndex);
}
printf("The square root of %i is %i\n", intIndex, intSquareRoot);
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #7 - Print Average test Score and Letter Grade
// --------------------------------------------------------------------------------
do
{
printf("Problem #7 - Enter a test score between 0 and 100 (-1 to end):\n");
scanf_s("%d", &intIndex);
intCount = intCount + 1;
intTotal = intTotal + intIndex;
intAverage = intTotal / intCount;
} while (intIndex != -1);
if (intAverage <= 100 & intAverage >= 90)
{
chrLetter = 'A';
}
if (intAverage <= 89 & intAverage >= 80)
{
chrLetter = 'B';
}
if (intAverage <= 79 & intAverage >= 60)
{
chrLetter = 'C';
}
if (intAverage <= 79 & intAverage >= 70)
{
chrLetter = 'D';
}
else
{
chrLetter = 'F';
}
printf("The average test score was %d and the letter grade is a %c", intAverage, chrLetter);
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #8 - Print the sum of all multiples of 3 or 5 below 1000
// --------------------------------------------------------------------------------
printf("Problem #8 - Print the sum of all multiples of 3 or 5 below 1000.\n");
for (intIndex = 0; intIndex < 1000; intIndex ++)
{
if (intIndex % 3 == 0 || intIndex % 5 == 0)
{
intTotal += intIndex;
}
}
printf("The sum of all multiples of 3 or 5 below 1000 is %i, ", intTotal);
printf("\n");
printf("\n");
// --------------------------------------------------------------------------------
// Problem #9 - Print the uppercase and lowercase letter of an UpperCase letter entered.
// --------------------------------------------------------------------------------
printf("Problem #9 - Enter an uppercase letter: \n");
scanf_s("%c", &chrLetter);
if (chrLetter < 'a' || chrLetter > 'z')
{
printf("Letter must be uppercase: ");
}
else if (chrLetter < 'A' || chrLetter > 'Z')
{
chrLetter = chrLetter;
}
printf("Here is the letter in uppercase %C and lowercase %c", &chrLetter, &chrLetter);
printf("\n");
printf("\n");
//is the letter uppercase?
//no, enter another letter
//yes, print both the uppercase and lowercase letter
// --------------------------------------------------------------------------------
// Problem #10 - Have user guess number between 1 and 100
// --------------------------------------------------------------------------------
do
{
printf("Problem #10 - Enter the chosen number between 1 and 100 (you have 8 guesses)\n");
scanf_s("%d", &intNumberEntered);
if (intNumberEntered < intNumberChosen)
{
printf("Too low!\n");
}
else if (intNumberEntered > intNumberChosen)
{
printf("Too high!\n");
}
//update count
intCount + 1;
if (intCount == 8)
{
printf("You Lose!!!\n");
}
} while (intNumberEntered != intNumberChosen);
printf("Congratulations, you won!!\n");
system("pause");
}