C Language 1. Please save the program with the name ‘cal.c’ Write a program to i
ID: 3907602 • Letter: C
Question
C Language
1. Please save the program with the name ‘cal.c’
Write a program to input two integer values and out their sum and their difference. A sample
run off the program follows. The user input is represented in boldface in the sample run.
(page. 56, C by Discovery 4th edition, by Foster & Foster)
Example: Enter an integer: 45
Enter another integer: 83
45 + 83 = 128
45 – 83 = -38
2. Please save the program with the name ‘guess.c’
Write a C program that plays a number guessing game with the user. A sample run for the game
follows. User input is shown in boldface in the sample run. (C by discovery)
Welcome to the game of Guess It!
I will choose a number between 1 and 200.
You will try to guess that number. If you guess wrong, I will
tell you if you guessed too high or too low.
You have 5 tries to get the number.
OK, I am thinking of a number. Try to guess it.
Your guess? 50
Too high!
Your guess? 12
Too low!
Your guess? 112
Too high!
Your guess? 250
Illegal guess. Your guess must be between 1 and 200.
Try again. Your guess? 30
**** CORRECT ****
Want to play again? y
Ok, I am thinking of a number. Try to guess it.
Your guess? 58
**** CORRECT ****
Want to play again? n
Goodbye, It was fun. Play again soon.
The following code will print out a random generator between 1-100 ten times.
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, n;
printf("Ten random numbers in [1,100] ");
for (c = 1; c <= 10; c++)
{
n = rand() % 100 + 1;
printf("%d ", n);
}
return 0;
}
Explanation / Answer
Coding explation given in comments throughout the program.
1. calc.c
2.guess.c
Program 1 calc.c
//Program to print Sum and Difference of given integers.
//using codeblocks on windows system, program has been tested.
#include <stdio.h>
#include <stdlib.h>
int main()
{
// declaring 4 variables for storing integer values provided by user and sum and diff to store the result.
int number1, number2, sum, diff;
//printing title of the program
printf("Program to print Sum and Difference ");
//Prompting user to enter 1st number
printf("Enter an integer :");
//Storing the entered integer to variable number1.
scanf("%d",&number1);
//promting user to enter 2nd number
printf("Enter another integer :");
//storing the second number to variable number2
scanf("%d",&number2);
//calculating the given numbers and storing it in variable sum and diff
//addition code
sum = number1 + number2;
//subtraction code
diff = number1 - number2;
//printing the output of all variables
printf("%d + %d = %d ", number1, number2, sum);
printf("%d - %d = %d ", number1, number2, diff);
return 0;
}
Program 2 guess.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
// variable declartion for userGuess number and random Number and ch for user to ask continue game or not.
int i, userGuess=0, randNumber=0;
char ch='n';
//print initial message before start of program.
printf("Welcome to the game of Guess It! ");
printf("I will choose a number between 1 and 200. ");
printf("You will try to guess that number. If you guess wrong, I will ");
printf("tell you if you guessed too high or too low. ");
printf("You have 5 tries to get the number. ");
// do while loop used to run program till user says no.
do{
printf("OK, I am thinking of a number. Try to guess it. ");
//generate random number with rand function. Only rand() function generates same number repeatedly so srand() is used
srand ( time(NULL) );
randNumber = rand() % 200 + 1;
//For testing below line is provided it will be commented when production version compiled
//uncomment below line to see what random number generated by system.
printf("Rand Number : %d ",randNumber);
//FOR loop for asking user guess for 5 times.
for(i = 0 ; i< 5; i++){
printf("Your guess?");
//get user guess number.
scanf("%d",&userGuess);
//check user number with random number or validate with number which is beyond the limit of 200.
if(userGuess > 200){
printf("Illegal guess. Your guess must be between 1 and 200. ");
printf("Try again. ");
}else if(userGuess > randNumber){
printf("Too High! ");
}else if(userGuess < randNumber){
printf("Too Low ");
}else if(userGuess == randNumber){
printf("===CORRECT=== ");
//break is used to stop iteration of for loop and prompt user to continue with new game.
break;
}
}
printf(" Want to play again?");
//get user input to continue or not.
ch = getche();
//start to new program cycle in new line.
printf(" ");
//compare the user input to continue or not.
}while(ch == 'y' || ch == 'Y');
// on user select to no display message and stop program
printf(" Goodbye, It was fun. Play again soon.");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.