Вы находитесь на странице: 1из 4

C Programming Lab

Universiti Tenaga Nasional




2 Basic Input Output

Question 1

What is the output of the programs below, and do you understand the concept of printf?

/* this is Question 1(a)*/
#include<stdio.h>

int main ()
{
printf("Welcome to ");
printf("my house.");
printf("I live in a ");
printf("small house ");
printf("with 2 cats and 3 hamsters");

return 0;
}

/* this is question 1(b) */
#include <stdio.h>

void main (void)
{
printf("Welcome to\n my house.\nI live in\na cottage.\n");
}






Question 2

What are the errors of these programs? Try to repair the error (yourself) and try to execute the
programs.

/* this is question 2(a) */
#inlude<studio.h>
void main (void)
{
printf("Hom sweet home");
}

/* this is question 2(b) */
#include<stdio.h>
void main (void)
{
printf("Home sweet home\n\n")
}

/* this is question 2(c) */
#include<stdio.h>
void main (int)
{
print("Home sweet home\t");
return 0;
}
NOTES: As long as you do not put \n in your printf, you output will be printed out in one
line.
C Programming Lab
Universiti Tenaga Nasional

Question 3

There are 4 basic data types: integer (int), float (float), double (double), character
(char).

Printing numerical output: integer (int)

What is the output of the programs below? Can you differentiate the way of writing integer
using printf?

/* this is question 3(a) */
#include<stdio.h>
void main (void)
{
printf("When you add 2 to 3, the answer is 5\n");
}

/* this is question 3(b) */
#include<stdio.h>
void main (void)
{
printf("When you add 2 to 3, the answer is %d\n", 2+3);
}

/* this is question 3(c) */
#include<stdio.h>
void main (void)
{
printf("When you add %d to %d, the answer is %d\n", 2, 3, 2+3);
}

According to your understanding, explain what is the purpose of %d?

NOTES:
Conversion character Type of output
%c Character
%s String
%d Integer
%e Floating point number /exponent
%f Floating point number
%lf Double
%g Floating point number
%o Octal
%s String (group character)
%u Unsigned integer
%x Hexadecimal
%% to print symbol %



Try changing the data type from integer to float. What happen to the output?

C Programming Lab
Universiti Tenaga Nasional

Question 4

scanf is the statement that gets the input from the user. The format of scanf is




When you get a value from a user, you should store it in some memory address. Try running
this program, what did you get?

/* This is example 4(a) */
#include<stdio.h>
void main (void)
{
int x;
printf("Please enter a value");
scanf("%d", &x);
printf("The value that you have entered is %d", x);
}

Now you have understood the concept of scanf (if you still dont, PLEASE ask!), try to solve
this problem.

Each egg cost RM0.25. Calculate the amount that should be paid by a user based on the
number of eggs bought. Show your problem analysis, algorithm and your source code.

Question 5

Write a program that calculates the area of a square after obtaining from the user the length
of its sides.

Program Analysis

Input: Square length
Output: Area of a square
Constraint: All values should be positive
Formula: Area = length * length

Question 6

Write a program, Population.cpp, to compute the rate of growth, expressed as a
percentage, of an insect population. Take as input the initial size of the population and its size
one week later. Then predict the size of the population in yet another week, assuming that
growth continues at the same rate. At the beginning of the program, please write your
program analysis in comments.

Rate of growth = (size (week 2) size (week 1))/size (week 1) * 100

scanf (ctrl char 1, ctrl char 2, .., ctrl char n, argument 1, argument 2, .., argument n);

C Programming Lab
Universiti Tenaga Nasional

Question 7

Find errors on the source codes:

/* This is question 7 */
#include <stdio.h>
int main (void)
{
integer 123star = 3+2;
float xyz;

print(Please enter number")
scanf("%d", &xyz);
}

Question 8

Write, compile and execute the following code:

/*This is example for question 8 (a)*/
#include <stdio.h>
void main ()
{
printf("The number is %d\n", 15);
printf("The number is %i\n", 15);
printf("The number is %x\n", 15);
printf("The number is %o\n", 15);
printf("The number is %e\n", 15.0);
printf("The number is %E\n", 15.0);
printf("The number is %c\n", 'A');
printf("The number is %f\n", 15.0);
printf("The number is %s\n", "Hi");
}

Note the differences of the output formats %d, %i, %x, %o, %e, %E, %c, %f, and %s.

/*This is example for question 8 (b)*/
#include<stdio.h>
void main ()
{
printf("Hi \n"); /* use \n for next line */
printf("\" Hi \" \n"); /* use \ for */
printf("\' Hi \' \n"); /* use \ for */
printf("H \t I \n"); /* use \t for tab */
printf("\a\a Hi \n"); /* use \a for beep */
printf(" :-) \n"); /* print a smiley face */
}

Вам также может понравиться