Input and Variable problem solutions

STOP! Before you look here, make sure you have completed your attempt at the problem

Question

  1. The number of people living in Canada: int

  2. pi to 2 decimal places: float

  3. The answer to a multiple choice question where the choices are A,B,C or D: char

  4. pi to 10 decimal places: double

  5. Number of muffins sold: int

  6. Taxes due on a bottle of pop: float

Solution:

  1. Number of people living in Canada is under 2 billion and its a whole number. So, int works.

  2. pi to 2 decimal is 3.14. A float is enough to store this. A double can also store it but it is more storage than needed for this amount of precision

  3. A, B, C and D are all characters. so char works here

  4. pi to 10 decimal places need more storage than a float can handle, need double for more precision

  5. Unless you are selling more than 2 billion muffins, int is enough.

  6. The amount of taxes due on a bottle of pop should not have more than 3 to 4 significant digits so a float is good enough.

Question

Write a program that will ask the user to enter the length and width dimensions of a room in meters. Program will then calculate the area of the room (assuming it is rectangular). Remember area is calculated as length*width

Solution

#include <stdio.h>
int main(void){
    double length;
    double width;
    printf("please enter the length of the room: ");
    scanf("%lf",&length);
    printf("please enter the width of the room: ");
    scanf("%lf",&width);
    printf("The area of a room with dimensions %.2lf X %.2lf is %.2lf\n",length,width,length*width);
    return 0;
}

Question

Write a program that will ask the user to enter a decimal number. It will then print out the part of the number before the decimal point and after the decimal point separately.

For example, suppose the user enters 123.456. The program will print out:

the part before the decimal is 123 the part after the decimal is 456

Sample run:

Please enter a decimal number: 123.456
the part before the decimal is 123
the part after the decimal is 456

*Note - A sample run is used to provide inside on what you should see when your program runs. You should use it as a guide. It may also serve to clarify the problem.

Solution

#include <stdio.h>
int main(void){
    int beforeDecimal;
    int afterDecimal;
    printf("Please enter a decimal number: ");
    scanf("%d.%d",&beforeDecimal, &afterDecimal);
    printf("the part before the decimal is %d\n",beforeDecimal);
    printf("the part after the decimal is %d\n",afterDecimal);

}

Question

Write a program that will ask the user to enter an lower case alphabetic character. The program will then tell them what letter of the alphabet it is.

For example, if the user enters the letter a, the program will say that it is letter number 1. Thus, b would be letter number 2, c would be letter number 3 and so on.

Sample run:

Please enter a lower case alphabetic character: d
d is letter number 4 in the alphabet.

Solution

#include <stdio.h>
int main(void){
    char letter;
    int order;
    printf("Please enter a lower case alphabetic character: ");
    scanf("%c",&letter);

    /*characters are represented by an encoding system. In ascii, 
    the encoding for 'a' to 'z' are numbers that follow each other. 
    That is 'a's value is one less than 'b' which is one less than
    'c'.  Thus, we can add or subtract to the character to reach a
    different number:

    if letter was 'a' then 'a'-'a' results in 0.  if letter was 
    b, then 'b'-'a' would be 1 and so on.*/
    order = letter - 'a' + 1;

    printf("%c is letter number %d in the alphabet",letter,order);
}

Last updated