Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Worksheet 02 – Reading Inputs

Open your Quincy 2005 version and select C Source file to practice the given codes.
This practical sheet designed to practice the scan function scanf() and print function printf() in C
Programming.

printf( )
In C programming language, printf() function is used to print the (“character, string, float, integer, octal
and hexadecimal values”) onto the output screen.
We use printf() function with %d format specifier to display the value of an integer variable.
Similarly, %c is used to display character, %f for float variable, %s for string variable, %lf for double
and %x for hexadecimal variable.
To generate a newline, we use “\n” in C printf () statement.

scanf( )
In C programming language, scanf() function is used to read character, string, numeric data from
keyboard.

The sentences in between /* sign and */ are the comments in C Programming.

 Activity 01

01. Type the following.

#include<stdio.h>

int main ()
{
/*This line is for defining Age variable to hold the value*/
int Age;

/*This line is for print a message in your terminal*/


printf(“Enter Your Age : \n”);

/* Enter different Age’s through the key board and see the outputs*/
scanf(“%d”, &Age);

/*This line is to print the given age in your terminal*/


printf(“You are %d years old”, Age);

Page 1 of 2
return 0;
}

2. Following program is designed to ask the first name of the user, and print it in a message. Try to
program it and compile it.

#include<stdio.h>

int main(){

/*Defining a variable to store a name of the user that limits to 20 characters .*/
char name[20];

/*Print the question on terminal*/


printf("What is your first name:");

/*Read the input*/


scanf("%s",&name);

/*Print the message on the terminal*/


printf("Hi %s\nHave a nice day.",name);

return 0;
}

3. Following program is designed to enter two numbers and print the total number. Try to do it.

#include<stdio.h>

int main(){

int a,b;
int total;

printf("First number:");
scanf("%d",&a);

printf(“Second number:”);
scanf(“%d”,&b);

total=a+b; printf("Total:

%d”,total);

return 0;
}

4. Write a program to input marks of three subjects. Calculate and print the total and the
Average marks. (Hint: Create two different variables to store total and the average)

5. Write a program to input temperature in Fahrenheit and to calculate and print the equivalent
temperature in Celsius.
C = (5/9)*(F-32)
C – Celsius Value
F – Fahrenheit Value

Page 2 of 2

You might also like