Note: 1. The requirements stated \"Declare a double variable with a value set to
ID: 3913345 • Letter: N
Question
Note:
1. The requirements stated "Declare a double variable with a value set to positive infinity" [declared only a double]
2. To read a character, scanf requires %c, [not %ch1 or %ch2]
Create a program called dataExercise.c using Visual Studio 2017 that declares the following variables and displays their values:
1. Declare a character variable with value 'a', and display the character using printf with a %c format. Then add a second printf that displays the character with a %d format.
2. Declare a short int variable with a value set to the maximum value of a short int (the maximum value for whatever machine I happen to use to grade your assignment - so use the correct limits.h constant). Display the value using printf with a %d format.
3. Declare a double variable with a value set to positive infinity. Display the value using printf with a %f format.
4. Prompt the user and accept the following 4 types of values from a single input line: char int char float
5. Display the values that were read in (4)
6. Prompt the user and accept the following types of values from a single input line: char float int char
7. Display the values that were read in (6)
8. Prompt the user and accept an integer value
9. Display the value read in (8) in a right-justified field of width 15, with leading zeroes
10. Prompt the user and accept a float value
11. Display the value read in (10) in a right-justified field of width 15, with 2 decimal points of precision, and leading spaces
Your program must adhere to the class coding standards.
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <limits.h>
int main()
{
char ch = 'a';
printf("%c ", ch);
printf("%d ", ch);
short int shInt = SHRT_MAX;
printf("%d ", shInt);
double dbl = +INFINITY;
printf("%f ", dbl);
printf("Enter char int char float: ");
char ch1, ch2;
int num;
float fnum;
scanf("%c%i %c%f", &ch1, &num, &ch2, &fnum);
printf("You entered: '%c' %i '%c' %f ", ch1, num, ch2, fnum);
printf("Enter char float int char: ");
scanf(" %c%f%i %c", &ch1, &fnum, &num, &ch2);
printf("You entered: '%c' %f %i '%c' ", ch1, fnum, num, ch2);
printf("Enter an integer value: ");
scanf("%i", &num);
printf("You entered: %015i ", num);
printf("Enter a float value: ");
scanf("%f", &fnum);
printf("You entered: %15.2f ", fnum);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.