0% found this document useful (0 votes)
17 views4 pages

Chapter 2 LQ

Uploaded by

faiq3239
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Chapter 2 LQ

Uploaded by

faiq3239
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER: 02

USER INTERACTION
1. What are logical operators?
Definition:
“The operators that are used to combine two or more conditions (relational expressions) are
called logical operators.”
They result in true or false.
The true is represented by 1 while false is represented by 0.
There are three types of logical operators in C.
Description Operators
AND &&
OR ‖
NOT !
(i) Logical AND operator:
“The operator that takes two operands as input and produces a result true if both operands are true is
called AND operator.”
Representation:
It is represented by “&” symbols.
Truth Table:
Following is a truth table of AND operator:
Expression Result
False && False False
False && True False
True && False False
True && True True
(ii) Logical OR operator:
“The operator that takes two operands as input and produces a result true if any of the operands is
true and returns false if both operands are false is called OR operator.”
Representation:
It is represented by ‖ symbols.
Truth table:
Following is a truth table for the OR operator:
Expression Result
False ‖ False False
False ‖ True True
True ‖ False True
(iii) Logical NOT operator:
“The operator that indicates or reverses the result of a condition is called NOT operator.”
Representation:
The operator is represented by the “!” sign.
Truth Table:
The truth table of logical NOT operator is as follows:
Expression Result
False True
True False
2. What is the scanf ( ) function? Explain with the help of examples.
This function is used to take input from the user into the variable. It is a built-in standard input function of
the C language. The standard input means the input from the keyboard. This function is pronounced as
scan-eff.
Elements:
There are two main parts of the scanf function.
 The first part inside the double quotes is a list of format specifiers.
 The second part is the list of optional variables with & sign at their left.
Ampersand & sign:
The ampersand & sign is called the address of operator. If this sign is missed then the value cannot be
stored in the given variable.
While reading input from the keyboard it waits for the enter key after each character. it also allows us
to take multiple inputs in a single scanf statement. This function is declared in <stdio.h> header file.
Syntax:
scanf ("format specifier", &variable list);
scanf ("%d, &X);

Format specifier Variable name


In the above example, scanf( ) takes integer type input in the variable X. the scanf( ) will not store
value in the variable If the data type is not matched with the format specifier.
Examples Output
#include <stdio.h> Enter a number between 0-10: 4
void main ( ) The number you entered is: 4
{
int number;
printf(“Enter a number between 0-10:”);
scanf(“%d”, &number);
printf(“The number you entered is: %d”, number;
}
#include <stdio.h> Enter the value in a: 5
int main ( ) The value you entered is 5
{
int a, b, sum;
printf(“Enter the value in a and b:”);
scanf(“%d %d”, &a, &b);
sum = a + b
printf(“the sum of a and b is %d”,sum);
}
3. What is the printf ( ) function? Explain with the help of an example.
The printf ( ) is a built-in output function in C language. It is used to display the output on the computer
screen in a specified format. The output can be constants, texts, or the value of a variable.
Terminology:
Its name comes from “print formatted” which means it is used to print the formatted output on the
screen. This function is pronounced as print- eff.
This function is declared in <stdio.h> header file. It takes a formatted string and an optional list of
variables as arguments to output. Whatever we write inside the double quotes “and” in the printf( )
function, gets displayed on screen.
Syntax:
printf(format string); OR printf(format string, var1, var2, var3, ...);
Examples Output
#include<stdio.h> Hello world
void main ( )
{
printf(“Hello world”);
}
In the above example, printf function is used to display Hello world on screen.
4. What is a format specifier? Discuss different format specifiers in C language.
The format specifiers are used in C for input and output purposes. The format specifiers are
used to specify the format in which data is displayed on the computer screen.
These are used to display the result of any expression involving variables, constants or both.
Format specifiers start with a % sign followed by a special character for identifying the type of data.
Different format specifiers are used for different types of variables.
Some format specifiers are as follows:
Data type Format specifier
int %d or %i
float %f
char %c
Example:
#include<stdio.h>
void main( )
{
int a = 3, b = 4, sum;
sum = a+b;
printf(“%d + %d = %d”, a, b, sum);
}
Output:
3+4=7
In the above example, 1st %d is used for variable “a” 2nd %d is used for variable “b”, and last %d is used
for the variable sum.
5. What are escape sequences? Discuss different escape sequences in C.
Escape sequences are used in printf function inside the “and ”. They force printf to change its normal
behavior of showing output.
Backslash(\) is called escape character which is associated with each escape sequence to notify
about escape.
Escape sequences consist of two characters.
 The first character will be “\” i.e., backslash.
 The second character varies according to the functionality that we want to achieve.
Example:
printf("My name is \"Ali\"" );
Output:
My name is "Ali".
In the above example, \ is an escape sequence. it causes printf to display on the computer screen.
Following are some commonly used escape sequences:
Sequence Purpose
\’ It displays the single quote.
\\ It displays back slash.
\a It generates an alert sound.
\b It removes previous char.
\n It specifies the movement of the cursor to start of the next line.
\t It specifies the I/O function of moving to the next tab stop horizontally.
6. What are arithmetic operators? Explain different operators in C.
Definition:
“The operators that are used to perform arithmetic operations on data are called arithmetic
operators.”
These operators belong to the binary operator category because two operands are required to
complete their operation.
C language provides the following arithmetic operators:
(i) Addition:
Addition operator (+) calculates the sum of two operands.
Example:
int add = 10 + 10;
Output:
The resultant value in variable add is 20.
(ii) Subtraction:
Subtraction operator (-) subtracts right operand from the left operand value.
Example:
int result = 20 – 15;
Output:
After performing subtraction, value 5 is assigned to the variable result.
(iii) Multiplication:
Multiplication operator (*) is a binary operator which performs then product of two numbers.
Example:
int multiply = 5 * 5;
Output:
After the execution of statement, variable multiply contains value 25.
(iv) Division:
Division operator (-) divides the value of left operand by the value of right operand.
Example:
float result = 3.0/2.0;
Output:
After this statement, the variable result contains the value 1.5.
(v) Modulus:
Modulus operator (%) performs division of left operand by the right operand and returns the remainder
value after division. Modulus operator works on integer data types.
Example:
int remaining = 14 % 3;
Output:
As, when we divide 14 by 3, we get a remainder of 2. So, the value stored in variable remaining is 2.

You might also like