29
TUANKU SULTANAH BAHIYAH POLYTECHNIC ELECTRICAL ENGINEERING DEPARTMENT EC201 – FUNDAMENTAL PROGRAMMING 1 SESION: JUN 2012 NAME : MATRIX NUM : COMPUTER NUMBER : START TIME : END TIME : PRACTICAL SKILL MARK : / 100 EXPERIMENT 2 TITLE : DATA INPUT AND OUTPUT Objectives At the end of this lesson, students will be able to understand: Input Output Statements Function of input output statements. a. printf() and puts() b. scanf() and gets() Use format specified in programs Modify input output file in programs Create input output operations. 1

Muzzammilrashid

  • View
    233

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Muzzammilrashid

TUANKU SULTANAH BAHIYAH POLYTECHNIC

ELECTRICAL ENGINEERING DEPARTMENT

EC201 – FUNDAMENTAL PROGRAMMING 1

SESION: JUN 2012

NAME :

MATRIX NUM :

COMPUTER NUMBER

:

START TIME :

END TIME :

PRACTICAL SKILL MARK

: / 100

EXPERIMENT 2

TITLE : DATA INPUT AND OUTPUT

Objectives

At the end of this lesson, students will be able to understand:

Input Output Statements Function of input output statements.

a. printf() and puts()b. scanf() and gets()

Use format specified in programs Modify input output file in programs Create input output operations.

1

Page 2: Muzzammilrashid

Please read the instructions / questions carefully.

EXERCISE 1 (3 HOURS)

Exercise 1A

Write a program that uses four output statements to print the pattern of asterisks (*) shown below:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * ** * * * * * * * * * * * *

Source Code//Exercise 1A#include <stdio.h>main(){ printf("****** *** *** ******"); printf("\n****** ** * ** ******"); printf("\n ** ** ** **"); printf("\n ** ** ** **"); printf("\n****** **** ******"); printf("\n****** * ******");}

2

Page 3: Muzzammilrashid

Exercise 1B

Use printf() function to write a program that prints your name, registration number, email address and telephone number.

Source Code#include<stdio.h>main(){ printf("Muhammad Muzzammil Bin Md Rasid \n"); printf("16DET13F1112 \n"); printf("[email protected] \n"); printf("017-4408184 \n"); printf("Muhammad Shahrizal Bin abdullah \n"); printf("16DET13F1082 \n"); printf("[email protected] \n"); printf("017-5613319 \n"); printf("Aliff Marican Bin Ibrahim Marican \n"); printf("16DET13F1106 \n"); printf("012-4428248 \n"); printf("[email protected] \n"); printf("Muhamad Husaini bin Hilmi \n"); printf("16DET13F1085\n"); printf("[email protected]\n"); printf("017-4236686 \n"); return 0;}

3

Page 4: Muzzammilrashid

Exercise 1C

Use puts() function to write a program that prints your name, registration number, email address and telephone number.

Source Code#include<stdio.h>main(){ puts("Muhammad Muzzammil Bin Md Rasid \n"); puts("16DET13F1112 \n"); puts("[email protected] \n"); puts("017-4408184 \n"); puts("Muhammad Shahrizal Bin abdullah \n"); puts("16DET13F1082 \n"); puts("[email protected] \n"); puts("017-5613319 \n"); puts("Aliff Marican Bin Ibrahim Marican \n"); puts("16DET13F1106\n"); puts("012-4428248 \n"); puts("[email protected] \n"); puts("Muhamad Husaini bin Hilmi \n"); puts("16DET13F1085 \n"); puts("[email protected] \n"); puts("017-4236686 \n"); return 0;}

4

Page 5: Muzzammilrashid

Exercise 1D

Fix the errors of the program below:

#include <stdio.h>

main(){ char addresS [200]; puts("Insert your full address: "); getchs (Address); printf ("Your address is: %k \n",address); return ();}

The output after the errors fixed is shown as below:

5

Page 6: Muzzammilrashid

Source Code//Exercise 1D#include<stdio.h>main(){ char address[200]; puts("Insert your full address"); gets(address); printf("Your address is :\%s\n",address); return 0;}

6

Page 7: Muzzammilrashid

Exercise 1E:

A C program contains the following statements:

#include <stdio.h>

Int I,j,k;

Write an appropriate scanf function to enter numerical values for I,j and k, assuming

(a) The values for I,j and k will be decimal integers.

7

#include<stdio.h>

main()

{ int i,j,k; printf("Insert value of i:\n"); scanf("%d",&i); printf("Insert value of j:\n"); scanf("%d",&j); printf("Insert value of k:\n"); scanf("%d",&k); return 0;

}

Page 8: Muzzammilrashid

(b) The values I will be decimal integer, j an octal integer and k a hexadecimal integer.

8

#include<stdio.h>main(){ int i,j,k; printf("Insert value of i:\n"); scanf("%d",&i); printf("Insert value of j:\n"); scanf("%f",&j); printf("Insert value of k:\n"); scanf("%f",&k); return 0;}

Page 9: Muzzammilrashid

(c) The values for I and j will be hexadecimal integers and k will be an octal integer.

9

#include<stdio.h>main(){ int i,j,k; printf("Insert value of i:\n"); scanf("%f",&i); printf("Insert value of j:\n"); scanf("%f",&j); printf("Insert value of k:\n"); scanf("%f",&k); return 0;}

Page 10: Muzzammilrashid

EXERCISE 2 ( 3 HOURS)

Exercise 2A:

Write the program and get the output.

10

//Exercise 2A#include <stdio.h>

main()

{

char name [20];int num1,num2,answer;

printf("Insert your name: ");scanf("%s",&name);printf("Enter your first integer:\n");scanf("%d",&num1);printf("Enter your second integer:\n");scanf("%d",&num2);

answer=num1+num2;

printf("The sum is : %d\n",answer);return 0;}

Page 11: Muzzammilrashid

Exercise 2B

Modify the add.c program;

i. Subtraction of num1 and num2, then display the answer

Source Code#include <stdio.h>

main()

{

char name [20];int num1,num2,answer;

printf("Insert your name: ");scanf("%s",&name);printf("Enter your first integer:\n");scanf("%d",&num1);printf("Enter your second integer:\n");scanf("%d",&num2);

answer=num1-num2;

printf("The sum is : %d\n",answer);return 0;}

11

Page 12: Muzzammilrashid

ii. Multiplication of num1 and num2, then display the answer

Source Code#include <stdio.h>

main()

{

char name [20];int num1,num2,answer;

printf("Insert your name: ");scanf("%s",&name);printf("Enter your first integer:\n");scanf("%d",&num1);printf("Enter your second integer:\n");scanf("%d",&num2);

answer=num1*num2;

printf("The sum is : %d\n",answer);return 0;}

12

Page 13: Muzzammilrashid

iii. Division of num1 and num2, then display the answer

Source Code

#include <stdio.h>

main()

{

char name [20];double num1,num2,answer;

printf("Insert your name: ");scanf("%s",&name);printf("Enter your first integer:\n");scanf("%d",&num1);printf("Enter your second integer:\n");scanf("%d",&num2);

answer=num1/num2;

printf("The sum is : %f\n",answer);return 0;}

13

Page 14: Muzzammilrashid

Exercise 2C

A C program contain s the following statement:

#include <stdio.h>

int a=0177,b=055,c=0xa8,d=0x1ff;

Write a printf function for each of the following groups of variables or expressions.

(a) a,b,c and d

14

#include <stdio.h>

main()

{

int a=0177 , b=055 , c=0xa8 , d=0x1ff;

printf("a=0177 \n");

printf("b=055 \n");

printf("c=0xa8 \n");

printf("d=0x1ff \n");

return 0;

}

Page 15: Muzzammilrashid

(b) (a+b), (c-d)

15

#include <stdio.h>

main()

{

int a=0177 , b=055 , c=0xa8 , d=0x1ff;

printf("a=0177 \n");

printf("b=055 \n");

printf("c=0xa8 \n");

printf("d=0x1ff \n");

printf("(a+b), (c-d) \n");

printf("(0177+055), (0xa8-0x1ff)");

return 0;

}

Page 16: Muzzammilrashid

Exercise 2D

Write a program that prompts the user to enter three numbers and then prints them vertically (each in one line), first forward and then reversed (the last one first), as shown below.

16

Page 17: Muzzammilrashid

Source Code#include <stdio.h>

main()

{

printf("Enter 3 integer : \n"); printf("12 \n"); printf("56 \n"); printf("89 \n"); printf("forward order 12 -> 56 -> 89 \n"); printf("reversed order 89 <- 56 <- 12 ");

return 0;}

17

Page 18: Muzzammilrashid

EXERCISE 3 ( 3 HOURS)

Exercise 3A

By using the six steps in C programming, please make a program that can calculate voltage value using Ohms Law.

The sample of output you should get as shown below:

*** Please make sure all the steps in problem solving are accurate and complete discussed.

18

Page 19: Muzzammilrashid

19

#include<stdio.h>

main()

{

float resistance,current,voltage;

printf("---Ohm's Law---\n");

printf("Enter resistance in Ohm\n");

scanf("%f",&resistance);

printf("Enter current in Ampere\n");

scanf("%f",&current);

printf("Enter value of the Voltage\n");

scanf("%f",&voltage);

return 0;

}

Page 20: Muzzammilrashid

Exercise 3B

By using the six steps in C programming, please make a program that can calculate the volume and area of sphere.

The sample of output you should get as shown below:

*** Please make sure all the steps in problem solving are accurate and complete discussed

20

Page 21: Muzzammilrashid

21

#include<stdio.h>

#define PI 3.142

main()

{

float radius,volume,area;

printf("Please enter the value for a radius:\n");

scanf("%f",&radius);

area = 1.33*PI*(radius*radius*radius);

printf("the volume is:%f\n",area);

volume = 4*PI*(radius*radius);

printf("the area is:%f",volume);

return 0;

}

Page 22: Muzzammilrashid

Exercise 3C

By using the six steps in C programming, please make a program that can find odd or even using conditional operator.

The sample of output you should get as shown below:

*** Please make sure all the steps in problem solving are accurate and complete discussed

22

Page 23: Muzzammilrashid

23

#include<stdio.h>

main()

{

printf("Enter an integer\n");

printf("5\n");

printf(Odd number\n\n");

return 0;

}

Page 24: Muzzammilrashid

24

#include<stdio.h>

main()

{

printf("Enter an integer\n");

printf("6\n");

printf("Even number\n\n");

return 0;

}

Page 25: Muzzammilrashid

PRACTICAL SKILL ASSESSMENT RUBRIC (CLO 4)

EXPERIMENT 2

Exercise 1.Specifications

/5

2.Readability

/5

3.Error

/5

4.Concept Understanding

/5

5.Delivery

/5

Total

/251A

1B

1C

1D

1E

2A

2B

2C

2D

3A

3B

3C

Total Marks (( Total Mark/300)*100) /300

/100

25