Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 155

Course – PROBLEM-SOLVING

THROUGH PROGRAMMING

Course Instructor
Dr. Umadevi V
Department of CSE, BMSCE
Webpage:https://sites.google.com/site/drvumadevi/

February 25, 2024 CSE, BMSCE 1


Unit-1

Introduction to Computer Hardware and Software:


Computer generations, computer types, bits, bytes and words,
CPU, Primary memory, Secondary memory, ports and connections,
input devices, output devices, Computers in a network, Network
hardware, Software basics, software types. Problem solving,
algorithms and flowchart.
Overview of C: Basic structure of C program, executing a C
program. Constant, variable and data types, Operators and
expressions

February 25, 2024 CSE, BMSCE 2


Introduction to C
 C is a general-purpose programming language developed by
Dennis Ritchie at AT&T Bell laboratories in 1972.
Advantages/Features of C Language
C language is very popular language because of the following
features:
1. C is structured Programming Language
2. It is considered a high-level language because it allows the
programmer to solve a problem without worrying about machine
details.
3. It has wide variety of operators using which a program can be
written easily to solve a given problem.
4. C is more efficient which increases the speed of execution and
management of memory compared to low level languages. 5. C is
machine independent. The program written on one machine will
work on another machine.
6. C can be executed on many different hardware platforms.

February 25, 2024 CSE, BMSCE 3


Basic structure of C program

February 25, 2024 CSE, BMSCE 4


Basic structure of C program
Suggested
Essential Part
Optional
Optional

Essential Part

Optional

February 25, 2024 CSE, BMSCE 5


Example: Basic structure of C program

February 25, 2024 CSE, BMSCE 6


Basic structure of C program
Documentation section: The documentation section consists of a set of comment lines giving the
name of the program, the author and other details, which the programmer would like to use later.
Link section: The link section provides instructions to the compiler to link functions from the system
library such as using the #include directive.
Definition section: The definition section defines all symbolic constants such using the #define
directive.
Global declaration section: There are some variables that are used in more than one function.
Such variables are called global variables and are declared in the global declaration section that is
outside of all the functions. This section also declares all the user-defined functions.

main () function section: Every C program must have one main function section. This section
contains two parts; declaration part and executable part
Declaration part: The declaration part declares all the variables used in the executable
part.
Executable part: There is at least one statement in the executable part. These two parts
must appear between the opening and closing braces. The program execution begins at the opening
brace and ends at the closing brace. The closing brace of the main function is the logical end of the
program. All statements in the declaration and executable part end with a semicolon.

Subprogram section
If the program is a multi-function program then the subprogram section contains all the user-defined
functions that are called in the main () function. User-defined functions are generally placed
immediately after the main () function, although they may appear in any order.

All section, except the main () function section may be absent when they are not required.

February 25, 2024 CSE, BMSCE 7


Executing a C program

Executing a program written in C


involves series of steps. These are
1. Creating the program
2. Compiling the program
3. Linking the program with function
that are needed from the C library
4. Executing the program

February 25, 2024 CSE, BMSCE 8


Process of Compiling and Running C Program

February 25, 2024 CSE, BMSCE 9


Unit-1

Overview of C: Constant, Variable and Data types,


Operators and expressions

February 25, 2024 CSE, BMSCE 10


Character Set
 The Characters used to form words, numbers and
expression depends upon the computer on which
program runs.
 A C character set defines the valid characters that can
be used in a source program.
 The characters in C set are grouped into following
categories:
1. Letters: Uppercase: A,B,C,….,Z Lowercase: a,b,c,…..,z
2. Digits: 0,1,2,…..,9
3. Special characters: ! , . # $ ( ) } { etc
4. White spaces: Blank space, Horizontal tab space (\t),
carriage return, new line character (\n), form feed character.

February 25, 2024 CSE, BMSCE 11


Character Set Special Characters
, comma & ampersand
. period ^ caret
; semicolon * asterisk
: colon - minus sign
? Question mark + plus sign
‘ apostrophe < opening angle bracket ( or less than
“ quotation mark sign)
! Exclamation mark > closing angle bracket ( or greater
| vertical bar than sign)
/ slash ( left parenthesis
\ backslash ) right parenthesis
~ tilde [ left bracket
_ under score ] right bracket
$ dollar sign { left brace
% percent sign } right brace
# number sign
February 25, 2024 CSE, BMSCE 12
C Tokens
 The smallest individual unit in ‘C’ program is called as C-Token.
 These are the building blocks of ‘C’ program.
 There are 6 types of ‘C’ tokens:
1. Keywords 2. Identifiers 3 Constants
4. Strings 5. Operators 6. Special symbols

February 25, 2024 CSE, BMSCE 13


Keywords
 Keywords are also called as reserved words.
 They already have pre-defined meaning.
 Keywords should always be written in lowercase.
 The keywords meaning can’t be changed.
 Keywords cannot be used as identifiers.
 There are 32 keywords in ‘C’ program.

February 25, 2024 CSE, BMSCE 14


Identifiers
 It refers to the name of the objects.
 These are the names given to variables, functions, macros
etc.
 The rules to write an identifier are as follows:
1. It must contain only alphabets (A to Z, a to z), numbers (0 to 9)
and underscore ( _ ).
2. It must start with alphabet or underscore but not numeric
character.
3. It should not contain any special symbols apart from
underscore.
4. It should not have any space.
5. Keywords cannot be used as identifiers.
6. Identifiers are case sensitive.
7. Maximum length of an identifier is 31 characters

February 25, 2024 CSE, BMSCE 15


Identifiers
Examples
Valid Identifiers:
integer, minimum, sum_total, row1, _cpps

Invalid Identifiers:
float -> It is a keyword.
I am -> It has space
123_Abc -> It is starting with number
N1+n2 -> It contains special symbol (+)

February 25, 2024 CSE, BMSCE 16


Constants
Constants are the values that doesn’t changes during the
execution of a program

Decimal Octal Hexadecimal

Signed Unsigned

February 25, 2024 CSE, BMSCE 17


Integer Constants
1. Decimal: It is an integer constant consisting of numbers
from 0 to 9. It can be preceded by + or –
2. Octal: It is an integer constant consisting of numbers
from 0 to 7. It is preceded by 0
3. Hexadecimal: It is an integer constant consisting of
numbers from 0 to 9, A to F (A=10, B=11, C=12, D=13,
E=14, F=15). It is preceded by 0x or 0x

February 25, 2024 CSE, BMSCE 18


Decimal, Octal, Hexadecimal Number System

February 25, 2024 CSE, BMSCE 19


Decimal Integer Constants
 A Decimal integer constants consist of any combination
of digits from 0 to 9. A Decimal integer constants can
contain one or more digits, but first digit should not be
0. Base value of decimal integer is 10.
 Valid Decimal integer Constants

0 12 856 456844

 Invalid Decimal Integer Constants


45,565 invalid character (,)
25.0 invalid character(.)
123 invalid character(blank space)
12-23-34-456 invalid character(-)
051 first digit cannot be a zero
February 25, 2024 CSE, BMSCE 20
Rules for constructing Integer constants

i. An integer constant must have at


least one digit.
ii. It must not have a decimal point.
iii. It can be either positive or negative.
iv. If no sign precedes an integer
constant, it is assumed to be positive.
v. Commas or blanks are not allowed
within an integer constant.

February 25, 2024 CSE, BMSCE 21


Octal Integer Constants
 An Octal integer should starts with 0 in order to identify the
constant as an octal integer number. An Octal integer constants
consist of any combination of numbers between 0 to 7. Base
value of Octal integer constants is 8.
 Valid Octal Integer Constants

0 012 0756 0456744

 Invalid Octal Integer Constants

4725 does not starts with 0


01491 invalid digit(9)
012.23 invalid character(.)
051-43 invalid character(-)

February 25, 2024 CSE, BMSCE 22


Hexadecimal Integer Constants
 A Hexadecimal integer constants can be a combination of any
numbers in the range of 0 to 9 and a to f or A to F. Here A to F
denotes 0 to 15. A Hexadecimal integer constants should starts
with either 0x or 0X. Base value of Hexadecimal integer
constants is 16.
 Valid Hexadecimal Integer in C Programming

0x 0X12 0x8A6 0x4a84b4

 Invalid Hexa Integer Constants in C Programming

4725 does not starts with 0x


0x1H9 invalid charcter(H)
0X12.23 invalid character (.)
0X51-43 invalid character(-)

February 25, 2024 CSE, BMSCE 23


Unsigned or Long Integer Constants
 Unsigned or Long Integer Constants may
exceed the range of Integer. Unsigned or Long
Integer Constants are identified by the suffix u
or l. Here
 U represents unsigned int.
 L represents long int.
 Example of unsigned and long integer
constants

4541U Unsigned integer


4558L Long integer
9876UL Unsigned long integer

February 25, 2024 CSE, BMSCE 24


Real or Floating point constant
 Real constant is combination of a whole number followed by
a decimal point and the fractional part.
 Example: 0.0083 -0.75 .95 215.
 The Real or Floating-point constants can be written in two forms:
1. Fractional form
2. Exponential form

1. Real constant in fractional form


A real constant consists for a series of digits representing the whole part of
the number, followed by a decimal point, followed by a series of
representing the fractional part. The whole part or the fractional part can be
omitted, but both cannot be omitted.
2. Real constant in Exponential form: Represented in two parts.
Mantissa: The part appearing before e, the mantissa is either a real
number expressed in decimal notation or an integer.
Exponent: The part following e, the exponent is an integer with an
optional plus or minus sign followed by a series of digits. The letter e
separating the mantissa and the exponent can be written in either
lowercase or uppercase.

February 25, 2024 CSE, BMSCE 25


Real or Floating point constant
Valid and Invalid Floating Point Constants

Valid Floating Point Constants Invalid Floating Point Constants

47.00 47,00
0.4 05

Valid and Invalid Exponential notation


Valid Exponential notation Invalid Exponential notation
542.254E6 7.5.3E4
542.254e-13 7.3e4.5

February 25, 2024 CSE, BMSCE 26


Real or Floating point constant
Rules for Constructing Real Constants in Fractional Form
1. A real constant must have at least one digit.
2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive.
5. Commas or blanks are not allowed within a real constant.

Rules for Constructing Real Constants in Exponential Form


1. The mantissa part and the exponential part should be separated
by letter e or E in exponential form
2. The mantissa part may have a positive or negative sign.
3. Default sign of mantissa part is positive.
4. The exponent part must have at least one digit, which must be a
positive or negative integer. Default sign is positive.

February 25, 2024 CSE, BMSCE 27


Character Constants
1. Single Character Constant: These are the
values enclosed within single quotes.
 Each character have an integer value called as
ASCII
 Example: ‘A’: 65, ‘a’: 97
 Printable Character: All characters are printable
except backslash(\) character or escape
sequence.
 Non-Printable Character: The characters with
backslash (\) are non printable. Ex: \n, \t

February 25, 2024 CSE, BMSCE 28


American Standard Code for Information
Interchange(ASCII) Table

February 25, 2024 CSE, BMSCE 29


Character Constants

2. String Constant:
 A group of characters together form
string.
 Strings are enclosed within double
quotes.
 They end with NULL character (\0).
Ex: “BMS”
B M S \0

February 25, 2024 CSE, BMSCE 30


Backslash Character Constants

February 25, 2024 CSE, BMSCE 31


Example Program

#include <stdio.h>
main()
{
printf("BMS College of Engineering\n");
printf("BMS \"College\" of Engineering\n");
printf("BMS College of Engineering\?\n");
printf("\\BMS College of Engineering\\");
}

Output
BMS College of Engineering
BMS "College" of Engineering
BMS College of Engineering?
\BMS College of Engineering\

February 25, 2024 CSE, BMSCE 32


Symbolic Constants/ Defined Constant

 These are created with the help of define


preprocessor directive.

 Ex: #define PI 3.142

 During processing the PI in the program will be


replaced with 3.142.

 The name of constant is written in uppercase


just to differentiate from the variables.

February 25, 2024 CSE, BMSCE 33


Variables in C
 A variable is a data name used to store a data value.
 It may take different values at different time during
execution.
 Variables are also called identifier, because it is used to
identify value.
 Examples: Average
height
Counter_1
class_strength

Syntax of variable declaration:


data-type variable-name;
Example: int a, b, Sum;
char bms, c;

February 25, 2024 CSE, BMSCE 34


Variables in C

February 25, 2024 CSE, BMSCE 35


Example of valid and invalid variable names

Examples: Valid variable names


Jeevan
Value
T_raise
Bangalore
x1marks

Examples: Invalid variable names


123
(area)
%marks
25th

February 25, 2024 CSE, BMSCE 36


DATA TYPES

Data type defines the types of data that is


stored in a variable.
There are 3 data types:
i. Primary/ Built-in/ Fundamental data type
int, float, char, void
ii. Derived data type
array, structure
iii. User defined data type
enum, typedef

February 25, 2024 CSE, BMSCE 37


Primary (or Basic) Data Types

Signed Unsigned char or

Void

February 25, 2024 CSE, BMSCE 38


Primary Data types

Integer type:
 It is used to store whole numbers.
 The keyword int is used to declare variable of integer
type.

February 25, 2024 CSE, BMSCE 39


Primary Data types

Floating point data type:


 It is used to store decimal numbers.
 The keyword float is used to declare variable of
floating point data type

Note:
Double: It is used to store double precision floating point numbers.

February 25, 2024 CSE, BMSCE 40


Primary Data Types
Char:
 It is used to store character type of data.
 The keyword char is used to declare variable of
character type.
 Char type takes 1B of storage

Void:
 It is a special data type that has no value.
 It doesn’t have size.
 It is used for returning the result of function
that returns nothing.

February 25, 2024 CSE, BMSCE 41


Primary Data Types

February 25, 2024 CSE, BMSCE 42


Declaration of Variables

February 25, 2024 CSE, BMSCE 43


Initialization of variables

February 25, 2024 CSE, BMSCE 44


Example Programs

Integertype.c
#include <stdio.h>
main()
{
int a = 8;
printf("The value of a = %d ",a);
}

The value of a = 8

February 25, 2024 CSE, BMSCE 45


45
Example Programs

floatrtype.c
#include <stdio.h>
main()
{

float a = 8.976;
printf("The value of a = %f",a);

The value of a = 8.976

February 25, 2024 CSE, BMSCE 46


46
Example Programs

program.c
#include <stdio.h>
main()
{

char bms = 'b';


printf("The character of bms = %c ",bms);

The character of bms = b

February 25, 2024 CSE, BMSCE 47


47
Example Programs

sum.c
#include <stdio.h>
main()
{
int a = 8;
int b = 10
printf("The sum = %d ",a+b);

The sum = 18

February 25, 2024 CSE, BMSCE 48


Example Programs

sum.c
#include <stdio.h>
main()
{
int a = 8;
int b = 10
int c;

c=a+b;
printf("The sum= %d ",c);

The sum = 18

February 25, 2024 CSE, BMSCE 49


Question
Write a C program to declare three floating point variables
with name of variables as Num1, Num2 and Sum.
Initialize Num1 to the value 2.5 and Num2 to the value to
3.8. Initialize the variable Sum to total of the two variable
values of Num1 and Num2. The program should output the
Sum variable value.

February 25, 2024 CSE, BMSCE 50


Primary Data Types
Data Type Format Specifier Typical Bytes

unsigned char %c 1
char %c 1
signed char %c 1
int %d, %i 2
unsigned int %u 2
signed int %d, %i 2
short int %hd 1
unsigned short int %hu 1

signed short int %hd 1


long int %ld, %li 4
signed long int %ld, %li 4
unsigned long int %lu 4

float %f 4

double %lf 8

long double %Lf 10

February 25, 2024 CSE, BMSCE 51


Find the output of following Program
#include <stdio.h>
main() {
int a,b=20;
int c,d;
int x, y=10;
char z='a';
a=0;
c=d=10;
printf("%d %d",a,b);
printf("\n");
printf("%d %d",c,d);
printf("\n");
printf("%d %c",z,z);
printf("\n");
x=y+z;
printf("%d",x);
}

February 25, 2024 CSE, BMSCE 52


Find the output of following Program
#include <stdio.h>
main() {
int a,b=20;
int c,d;
int x, y=10;
char z='a';
Output
a=0;
--------
c=d=10;
0 20
printf("%d %d",a,b); 10 10
printf("\n"); 97 a
printf("%d %d",c,d); 107
printf("\n");
printf("%d %c",z,z);
printf("\n");
x=y+z;
printf("%d",x);
}

February 25, 2024 CSE, BMSCE 53


Reading Data from Keyboard
In C programming, scanf() is one of the commonly used function to
take input from the user. The scanf() function reads formatted
input from the standard input such as keyboards.

Syntax of scanf()
scanf("format specifier" , &variable1, &variable2,…..);

Note:
How to get the address of a variable ?
Using ampersand (&) operator, we can get the address of a variable. In C,
we will refer & as address of operator.

February 25, 2024 CSE, BMSCE 54


Example Program: Integer Input/Output
#include <stdio.h>
main()
{
int num;

printf("Enter an integer: ");


scanf("%d", &num);
printf("Number = %d",num);
}

February 25, 2024 CSE, BMSCE 55


Example Program: Float and Double Input/Output
#include <stdio.h>
main()
{
float num1;
double num2;

printf("Enter a number: ");


scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);

printf("num1 = %f\n", num1);


printf("num2 = %lf", num2);

February 25, 2024 CSE, BMSCE 56


Example Program: Character Input/Output
#include <stdio.h>
main()
{
char chr;

printf("Enter a character: ");


scanf("%c",&chr);
printf("You entered %c", chr);
}

February 25, 2024 CSE, BMSCE 57


Example Program: Taking multiple inputs from the user and display them.

#include <stdio.h>
main()
{
int a;
float b;

printf("Enter integer and then a float: ");

// Taking multiple inputs


scanf("%d%f", &a, &b);

printf("You entered %d and %f", a, b);

}
February 25, 2024 CSE, BMSCE 58
Derived Data Types
 Data types that are derived from fundamental data types are called derived data
types.
 Derived data types do not create new data types. Instead, they add some
functionality to the existing data types.
 Derived data types are derived from the primitive data types by adding some
extra relationships with the various elements of the primary data types. The
derived data type can be used to represent a single value or multiple values.

Given below are the various derived data types used in C:


 Arrays: An array is an ordered sequence of finite data items of the same data
type that share a common name.
 Pointers: A pointer is a special type of variable used to hold the address of
another variable.
 Functions: A function is a self-contained block of one or more statements with a
name.
 Structures: A structure is a collection of different data type items stored in a
contiguous memory allocation.
 Unions: A union is similar to a structure where the memory allocated to the
largest data type is reused for other types in the group.

February 25, 2024 CSE, BMSCE 59


User-Defined Type Declaration

 typdef
 enum

February 25, 2024 CSE, BMSCE 60


User-Defined Type Declaration
typedef: The keyword typedef is used to create a new name (alias) for an
existing data type. It does not create a new data type.
The syntax of using typedef is as follows:
typedef existing_type new_data_type_name;

Example Program
#include <stdio.h>
void main() {
typedef int Tutorials; //statement-1
Tutorials a = 17;
printf("Given value =%d\n", a);
}

In statement – 1, the keyword typedef is used to create Tutorials as the alias for the
int data type. From this statement onwards, Tutorials will be the new name for int in
this program and the variables declared as Tutorials type will also behave like int
variables for all practical purposes.

February 25, 2024 CSE, BMSCE 61


User-Defined Type Declaration
enum: The enum refers to a keyword that we use for creating an enumerated data
type. The enum is basically a special data type (enumerated data type) that consists
of a set of various named values – known as members or elements. We mainly use
it for assigning different names to the integral constants. This way, the program
becomes much more readable.
Format that we use for creating the enum type:
enum identifier {value_a, value_b, …. , value_z};

The enumerated data types basically allow a user to create symbolic names of their
own for a list of all the constants that are related to each other.
For instance, you can create the enumerated data type for the false and true
conditions this way:
enum Boolean {false, true};

In case we don’t assign values explicitly to the enum names; the compiler, by default,
would assign the values that start from 0.
In this case, false and true would be automatically assigned to 0 and 1 respectively. In
simpler words, the first field of the enum value here will be replaced by 0, and then
the next field will be replaced with 1, and so on.

February 25, 2024 CSE, BMSCE 62


User-Defined Type Declaration
Let us take a look at an example to display how to use the enum
data value.

#include <stdio.h>
main() {
enum week {MON = 1, TUE, WED, THURS, FRI, SAT, SUN};
enum week day = FRI;
printf(“Week Day = %d”, day);
}

Here, the field name MON will be assigned with the value 1. Thus, the next
field name TUE will be assigned with the value 2 automatically, and so on.
The program mentioned above will print the following output:
Week Day = 5

February 25, 2024 CSE, BMSCE 63


Question
What is the output generated by the following program ?

#include <stdio.h>
main() {
enum months {January = 11, February, March, April, May, June, July,
August, September, October, November, December};
enum months birthday = October;
printf(“Surprise Birthday = %d”, birthday);
}

A. Surprise Birthday = 15
B. Surprise Birthday = 12
C. Surprise Birthday = 20
D. Error

February 25, 2024 CSE, BMSCE 64


Question
What is the output generated by the following program ?

#include <stdio.h>
main() {
enum months {January = 11, February, March, April, May, June, July,
August, September, October, November, December};
enum months birthday = October;
printf(“Surprise Birthday = %d”, birthday);
}

A. Surprise Birthday = 15
B. Surprise Birthday = 12
C. Surprise Birthday = 20
D. Error

Answer – C. Surprise Birthday = 20


Here, the field name January will be assigned with the value 11. The next field name
February will be assigned with the value 12 automatically, and so on.

February 25, 2024 CSE, BMSCE 65


Declare variable as Constant
Variables can be declared as constants by using the “const” keyword before the
datatype of the variable. The constant variables can be initialized once only. The
default value of constant variables are zero.

Example Program
#include <stdio.h>
main()
{
const int a;
const int b = 12;
printf("The default value of variable a : %d", a);
printf("\nThe value of variable b : %d", b);
}

February 25, 2024 CSE, BMSCE 66


Declare variable as Constant
Variables can be declared as constants by using the “const” keyword before the
datatype of the variable. The constant variables can be initialized once only. The
default value of constant variables are zero.

Example Program
#include <stdio.h>
main()
{
const int a;
const int b = 12;
printf("The default value of variable a : %d", a);
printf("\nThe value of variable b : %d", b);
}
Output
The default value of variable a : 0
The value of variable b : 12

February 25, 2024 CSE, BMSCE 67


Question
Whats is the output of this C code?
#include <stdio.h>
main() {
const int b = 12;

printf("\nThe value of variable b : %d", b);


b=b+1;
printf("\nThe value of variable b : %d", b);
}

A. The value of variable b : 12


The value of variable b : 13
B. Error because const and int are used together
C. Garbage value
D. Error, because a constant variable cannot be changed

Answer: Option D
Explanation: Constant variable has to be declared and defined at the same time.
Trying to change it later results in error.

February 25, 2024 CSE, BMSCE 68


Overflow and Underflow of Data
Overflow occurs when we attempt to store a value that is
greater than the largest value of the data type.
Similarly, Underflow occurs when we attempt to store a
value that is less than the least value of the data type.

February 25, 2024 CSE, BMSCE 69


Unit-1

Overview of C: Operators and expressions

February 25, 2024 CSE, BMSCE 70


Operators
An operator is a symbol that tells the compiler to perform specific
mathematical and logical functions.
The different operators supported in ‘C’ are:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Unary Operators : Increment and Decrement
7. Ternary/ Conditional Operator
8. Special Operators

February 25, 2024 CSE, BMSCE 71


Arithmetic Operators

The different arithmetic operators are:

February 25, 2024 CSE, BMSCE 72


Example Program
Write a C program that accepts principle, rate
of interest, time and compute the simple
interest.

February 25, 2024 CSE, BMSCE 73


Example Program
Write a C program that accepts principle, rate of interest,
time and compute the simple interest.

#include<stdio.h>
main()
{
int p,r,t, si;

printf(“Enter Principle, Rate of interest & Time to find simple interest: \n");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("Simple interest = %d",si);
}

February 25, 2024 CSE, BMSCE 74


Question
Write a C program to declare two floating point variables
with name of variables as Num1, Num2 and Sum. Accept
the values to variables Num1 and Num2 using scanf()
function. Initialize the variable Sum to total of the two
variable values of Num1 and Num2. The program should
output the Sum variable value.

February 25, 2024 CSE, BMSCE 75


Question
Write a C program to declare two floating point variables
with name of variables as Num1, Num2 and Sum. Accept
the values to variables Num1 and Num2 using scanf()
function . Initialize the variable Sum to total of the two
variable values of Num1 and Num2. The program should
output the Sum variable value.

#include <stdio.h>
main() {
float Num1, Num2, Sum;
printf("Enter two floating point values: ");
scanf("%f%f", &Num1, &Num2);
Sum=Num1+Num2;
printf("Sum = %f", Sum);
}

February 25, 2024 CSE, BMSCE 76


Question
Identify syntax error in the following program. After correction,
what output would you expect when you execute it ?
#include <stdio.h>
#define PI 3.14159
Main()
{
float R, C
Float perimeter
float area;
C = PI
R = 5.0;
Perimeter = 2.0*C*R;
area = C*R*R;
Printf("%f" "%f", perimeter, area);
}

February 25, 2024 CSE, BMSCE 77


Answer

#include <stdio.h> #include <stdio.h>


#define PI 3.14159 #define PI 3.14159
Main() main()
{ {
float R, C float R, C;
Float perimeter float perimeter;
float area; float area;
C = PI C = PI;
R = 5.0; R = 5.0;
Perimeter = 2.0*C*R; perimeter = 2.0*C*R;
area = C*R*R; area = C*R*R;
Printf("%f" "%f", perimeter, area); printf("%f %f", perimeter, area);
} }

Output:

31.40 78.50

February 25, 2024 CSE, BMSCE 78


Arithmetic expressions are of three types:

1. Integer Arithmetic
2. Real Arithmetic
3. Mixed mode Arithmetic
Integer Arithmetic: It is an expression evaluation where
all the operands are of integer data type. Let us take an
example:
int a=10,b=5,c;
c=a/b;
printf(“%d”, c);

c will contain quotient value 2

February 25, 2024 CSE, BMSCE 79


Arithmetic expressions are of three types:

Real Arithmetic: It is an expression evaluation where all the operands are


of float data type.
Here is an example:
float x=2.5, y=10.0,z;
z=y/x;
printf(“%f”, z);
z will contain value 4.00
Mixed Mode Arithmetic: In an expression if some operands are of integer
type and others are of float type it is called mixed mode arithmetic
expression. Here the data type of small size gets automatically converted to
data type of bigger size.
Here is an example for such expression:
int a=25;
float x=4,z;
z=a/x;
printf(“%f”,z);
Here ‘a’ is of integer data type and ‘x’ is of float type, therefore ‘a’
automatically gets converted to float type and output is: 6.25

February 25, 2024 CSE, BMSCE 80


Relational Operators
These are used to compare two quantities. The output will
be either 0 (False) or 1 (True).
The different relational operators are:

February 25, 2024 CSE, BMSCE 81


Examples on Relational operators:
Example-1:
int A=5, B=10, X=20, Y=5;
What is the truth value of the following expression:
(A+B) == (X-Y)

(5+10) == (20-5) answer is TRUE

Example-2:
int A=5, B=10, X=20, Y=10;
What is the truth value of the following expression:
(A+B) >= (X-Y)

(5+10)> = (20-10) answer is FALSE

February 25, 2024 CSE, BMSCE 82


Conditional Operator or Ternary Operator (?:)

It takes three arguments.

Expression1 ? Expression2 : Expresseion3

Where, Expression1: Condition


Expression2: Statement followed if condition is true
Expression3: Statement followed if condition is false

Example:
large = (4 > 2) ? 4: 2; large = 4

February 25, 2024 CSE, BMSCE 83


Conditional Operator or Ternary Operator (?:)

Example Program

#include <stdio.h>
main() {
int age;
// take input from users
printf("Enter your age: ");
scanf("%d", &age);

// ternary operator to find if a person can vote or not


(age >= 18) ? printf("You can vote") : printf("You cannot vote");
}

February 25, 2024 CSE, BMSCE 84


Question
Write a program to find largest of two integer numbers. Accept the
two integer numbers from user and print the largest value.
Note: You should use Conditional or Ternary operator

February 25, 2024 CSE, BMSCE 85


Question
Write a program to find largest of two integer numbers. Accept the
two integer numbers from user and print the largest value.
Note: You should use Conditional or Ternary operator

#include <stdio.h>
main() {
int a,b;
int large;

printf("Enter two integer numbers:");


scanf("%d%d",&a, &b);
large=(a>b)?a:b;
printf("Largest= %d", large);
}

February 25, 2024 CSE, BMSCE 86


Question
Write a program to find largest of three integer numbers. Accept the three
integer numbers from user and print the largest value.
Note: You should use Conditional or Ternary operator

February 25, 2024 CSE, BMSCE 87


Question
Write a program to find largest of three integer numbers. Accept the three
integer numbers from user and print the largest value.
Note: You should use Conditional or Ternary operator

#include <stdio.h>
main() {
int a,b,c;
int large, final_large;

printf("Enter three integer numbers:");


scanf("%d%d%d",&a,&b,&c);

large=(a>=b)?a:b;
final_large=(large>=c)?large:c;

printf("Largest= %d", final_large);


}

February 25, 2024 CSE, BMSCE 88


Question
Write a program to determine whether the given integer number is
an Even or Odd number. Accept the an integer number from user
and print the whether it is Even or odd.
Note: You should use Conditional or Ternary operator

February 25, 2024 CSE, BMSCE 89


Question
Write a program to determine whether the given integer number is
an Even or Odd number. Accept the an integer number from user
and print the whether it is Even or odd.
Note: You should use Conditional or Ternary operator

#include <stdio.h>
main() {
int a, remainder;
printf("Enter an integer number: ");
scanf("%d", &a);

remainder=a%2;
(remainder == 0) ? printf("Even") : printf("Odd");
}

February 25, 2024 CSE, BMSCE 90


Arithmetic Operators Precedence and Associativity

Operator Precedence: Operator precedence determines which operator is


evaluated first when an expression has more than one operators.
For example 100-2*30 would yield 40,
because it is evaluated as 100 – (2*30) and not (100-2)*30. The reason is
that multiplication * has higher precedence than subtraction(-).

Associativity in C: Associativity is used when there are two or more


operators of same precedence is present in an expression.
For example multiplication and division arithmetic operators have same
precedence, lets say we have an expression 5*2/10, this expression would
be evaluated as (5*2)/10 because the associativity is left to right for these
operators. Similarly 20/2*5 would be calculated as (20*2)/5.

Operator Precedence Associativity


* / % Highest Left to Right
+ - Next Left to Right

February 25, 2024 CSE, BMSCE 91


Logical Operators
Logical Operators are used to test more than one condition
and make decision.
The different logical operators are: NOT, AND, OR
Operator Description Example Return Value
&& Logical AND 7 > 3 && 8 > 5 1
|| Logical OR 7 > 3 || 8<5 1
! Logical NOT 5 != 5 0

Logical NOT (!) Logical AND (&&) Logical OR (||)

February 25, 2024 CSE, BMSCE 92


Question
Write a program to find largest of three integer numbers. Accept the three
integer numbers from user and print the largest value.
Note:
i. You should use Conditional or Ternary operator
ii. You should use Logical AND operator

February 25, 2024 CSE, BMSCE 93


Question
Write a program to find largest of three integer numbers. Accept the three
integer numbers from user and print the largest value.
Note:
i. You should use Conditional or Ternary operator
ii. You should use Logical AND operator

#include <stdio.h>
main() {
int N1, N2, N3, large;

printf("Enter three interg numbers:");


scanf("%d%d%d",&N1,&N2,&N3);

large=(N1>=N2 && N1>=N3)?N1:((N2>=N3)?N2:N3);

printf("Largest=%d",large);
}

February 25, 2024 CSE, BMSCE 94


Assignment Operators
Assignment operators are used to assign the result of an expression to a variable.
Oper Description Example
ator
= Simple assignment operator. Assigns values from right C = A + B will assign the
side operands to left side operand value of A + B to C
+= Add and assignment operator. It adds the right operand
C += A is equivalent to
to the left operand and assign the result to the left
C=C+A
operand.
-= Subtract and assignment operator. It subtracts the right
C -= A is equivalent to
operand from the left operand and assigns the result to
C=C-A
the left operand.
*= Multiply and assignment operator. It multiplies the right
C *= A is equivalent to
operand with the left operand and assigns the result to
C=C*A
the left operand.
/= Divide and assignment operator. It divides the left
C /= A is equivalent to
operand with the right operand and assigns the result
C=C/A
to the left operand.
%= Modulus and assignment operator. It takes modulus
C %= A is equivalent to
using two operands and assigns the result to the left
C=C%A
operand.
Note: +=, -=, *=, /=, %= are also referred as shorthand assignment operators.
February 25, 2024 CSE, BMSCE 95
Example Program
#include<stdio.h>
main()
{
int a=20;

a+=4;
printf("\na=%d",a);

a-=13;
printf("\na=%d",a);

a*=6;
printf("\na=%d",a);

a/=5;
printf("\na=%d",a);

a%=5;
printf("\na=%d",a);
}

February 25, 2024 CSE, BMSCE 96


Example Program
#include<stdio.h>
main()
{
int a=20;

a+=4;
printf("\na=%d",a);

a-=13;
Output
printf("\na=%d",a); a=24
a=11
a*=6; a=66
printf("\na=%d",a); a=13
a=3
a/=5;
printf("\na=%d",a);

a%=5;
printf("\na=%d",a);
}

February 25, 2024 CSE, BMSCE 97


Unary Operators

There are 4 types:


 Unary Plus Operator (+)
Determines the Sign
 Unary Minus Operator (-)
 Increment (++)
 Decrement (--)

February 25, 2024 CSE, BMSCE 98


Increment (++)
Increment (++): It adds one to the operand.

Pre-increment
First value of the operand
is incremented (added) by 1
then, it is used for
evaluation.
Example: ++a
++a is equivalent to a=a+1

February 25, 2024 CSE, BMSCE 99


Increment (++)
Increment (++): It adds one to the operand.

Pre-increment Post-increment
First value of the operand First value of the operand
is incremented (added) by 1 is used for evaluation then, it
then, it is used for is incremented (added) by 1.
evaluation.

Example: ++a Example: a++

++a is equivalent to a=a+1 a++ is equivalent to a=a+1

February 25, 2024 CSE, BMSCE 100


Example of Increment (++) operator

Pre-increment
or
Prefix increment
#include <stdio.h>
main()
{
int a=0, b=5;

printf("Value of a= %d b=%d ",a,b);


a = ++b;
printf("\nValue of a= %d b=%d ",a,b);
}

February 25, 2024 CSE, BMSCE 101


Example of Increment (++) operator

Pre-increment
or
Prefix increment
#include <stdio.h>
main()
{
int a=0, b=5;

printf("Value of a= %d b=%d ",a,b);


a = ++b;
printf("\nValue of a= %d b=%d ",a,b);
}

Value of a= 0 b=5
Value of a= 6 b=6

February 25, 2024 CSE, BMSCE 102


Example of Increment (++) operator

Pre-increment Post-increment
or or
Prefix increment Postfix increment
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int a=0, b=5; int a=0, b=5;

printf("Value of a= %d b=%d ",a,b); printf("Value of a= %d b=%d ",a,b);


a = ++b; a = b++;
printf("\nValue of a= %d b=%d ",a,b); printf("\nValue of a= %d b=%d ",a,b);
} }

Value of a= 0 b=5
Value of a= 6 b=6

February 25, 2024 CSE, BMSCE 103


Example of Increment (++) operator

Pre-increment Post-increment
or or
Prefix increment Postfix increment
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int a=0, b=5; int a=0, b=5;

printf("Value of a= %d b=%d ",a,b); printf("Value of a= %d b=%d ",a,b);


a = ++b; a = b++;
printf("\nValue of a= %d b=%d ",a,b); printf("\nValue of a= %d b=%d ",a,b);
} }

Value of a= 0 b=5 Value of a= 0 b=5


Value of a= 6 b=6 Value of a= 5 b=6

February 25, 2024 CSE, BMSCE 104


Question
Find the output of the following program
#include<stdio.h>
main()
{
int p, q;
q=10;
p=++q;
printf("p: %d\n",p);
printf("q: %d",q);
}

February 25, 2024 CSE, BMSCE 105


Question
Find the output of the following program
#include<stdio.h>
main()
{
int p, q;
q=10;
p=++q;
printf("p: %d\n",p);
printf("q: %d",q);
}
Answer:
p: 11
q: 11

February 25, 2024 CSE, BMSCE 106


Question
Find the output of the following program
#include<stdio.h>
main()
{
int p, q;
q=10;
p=q++;
printf("p: %d\n",p);
printf("q: %d",q);
}

February 25, 2024 CSE, BMSCE 107


Question
Find the output of the following program
#include<stdio.h>
main()
{
int p, q;
q=10;
p=q++;
printf("p: %d\n",p);
printf("q: %d",q);
}
Answer:
p: 10
q: 11

February 25, 2024 CSE, BMSCE 108


Question
Find the output of the following program
#include <stdio.h>
main() {
int var1 = 5, var2 = 5;

printf("%d\n", var1++);

printf("%d\n", ++var2);
}

February 25, 2024 CSE, BMSCE 109


Question
Find the output of the following program
#include <stdio.h>
main() {
int var1 = 5, var2 = 5;

// 5 is displayed
// Then, var1 is increased to 6.
printf("%d\n", var1++);

// var2 is increased to 6
// Then, it is displayed.
printf("%d\n", ++var2);
}

Answer:
5
6

February 25, 2024 CSE, BMSCE 110


Decrement (--)
Decrement (--): It subtracts one from the operand.

Pre-increment Post-increment
First value of the operand First value of the operand
is decremented (subtracted) is used for evaluation then, it
by 1 then, it is used for is decremented (subtracted)
evaluation. by 1.

Example: --a Example: a--

--a is equivalent to a=a-1 a-- is equivalent to a=a-1

February 25, 2024 CSE, BMSCE 111


Example of Decrement (--) operator

Pre-decrement
or
Prefix decrement
#include <stdio.h>
main()
{
int a=0, b=5;

printf("Value of a= %d b=%d ",a,b);


a = --b;
printf("\nValue of a= %d b=%d ",a,b);
}

February 25, 2024 CSE, BMSCE 112


Example of Decrement (--) operator

Pre-decrement Post-decrement
or or
Prefix decrement Postfix decrement
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int a=0, b=5; int a=0, b=5;

printf("Value of a= %d b=%d ",a,b); printf("Value of a= %d b=%d ",a,b);


a = --b; a = b--;
printf("\nValue of a= %d b=%d ",a,b); printf("\nValue of a= %d b=%d ",a,b);
} }

Value of a= 0 b=5 Value of a= 0 b=5


Value of a= 4 b=4 Value of a= 5 b=4

February 25, 2024 CSE, BMSCE 113


Bitwise Operators
These works on bits and performs bit by bit
operations.
The different types of bitwise operators are:
i. Bitwise AND (&)
ii. Bitwise OR (|)
iii. Bitwise XOR (^)
iv. Bitwise left shift (<<)
v. Bitwise right shift (>>)

February 25, 2024 CSE, BMSCE 114


Example: Bitwise Operators

Bitwise AND, OR and XOR:

February 25, 2024 CSE, BMSCE 115


Example: Bitwise Operators
Example for Bitwise Left shift:

Note: Meaning is to left shift bits by 2 positions

Example for Bitwise Right shift:

Note: Meaning is to right shift bits by 2 positions

February 25, 2024 CSE, BMSCE 116


Example Program: Bitwise Operator
Output
#include <stdio.h>
Value of a=9 b=5 c=1
main()
Value of a=9 b=5 c=13
{
Value of a=9 b=5 c=12
unsigned short int a=9,b=5, c=0;
Value of a=9 b=5 c=36
Value of a=9 b=5 c=2
c=a&b;
printf("Value of a=%hu b=%hu c=%hu\n",a,b,c);
c=a|b;
printf("Value of a=%hu b=%hu c=%hu\n",a,b,c);
c=a^b;
printf("Value of a=%hu b=%hu c=%hu\n",a,b,c);
c=a<<2;
printf("Value of a=%hu b=%hu c=%hu\n",a,b,c);
c=a>>2;
printf("Value of a=%hu b=%hu c=%hu\n",a,b,c);

February 25, 2024 CSE, BMSCE 117


Special Operators

a) Comma Operator
b) sizeof operator

February 25, 2024 CSE, BMSCE 118


Comma Operator
1. Comma operator has lowest precedence i.e., evaluated at last.
2. Comma operator returns the value of the rightmost operand.
3. Comma operator can acts as
Operator: In the Expression
Separator: Variable Declarations, Function Calls,
Function Definition, Parameter List,
Enum declarations

February 25, 2024 CSE, BMSCE 119


Example Program
#include <stdio.h>
int main()
{
int x,y;

x = 40,50,60;
y = (40,50,60);
printf(“x= %d, y= %d\n”,x,y);
}

Output:
x= 40, y= 60

February 25, 2024 CSE, BMSCE 120


Example Program
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int x,y; int x= 70,80,90;
int y;
x = 40,50,60;
y = (40,50,60); y= (70,80,90);
printf(“x= %d, y= %d\n”,x,y); printf(“x= %d, y= %d\n”,x,y);
} }

Output: Compilation Error


x= 40, y= 60
int x= 70,80,90;
This statement is basically a declaration that also
contains an initialisation statement. The comma is used
in the form of a separator here, and thus, we cannot
use such values in a program.
The actual output of the code would be equal to a
compile-time error since the comma is invalid in such a
given circumstance.

February 25, 2024 CSE, BMSCE 121


sizeof operator
sizeof( ) operator: It is a compile time operator and when
used with an operand it returns the number of bytes the
operand occupies the operand may be variable or constant
or data type qualifier.
Example:
int sum;
M=sizeof(sum); //M has 2
N=sizeof(long int); //N has 4

February 25, 2024 CSE, BMSCE 122


Operator Precedence
and Associativity

February 25, 2024 CSE, BMSCE 123


Operator Precedence and Associativity

February 25, 2024 CSE, BMSCE 124


Operator Precedence and Associativity (Contd…)

February 25, 2024 CSE, BMSCE 125


Operator Precedence

In previous section we came across expressions and various operators used


in an expression. For instance if an expression consists of different category
of operators such expression is evaluated using Precedence (priority) of
operators and Associativity of operators. Precedence: The order in which
the operators in a complex expression are evaluated is determined by set of
priorities known as precedence.
If an expression contains Arithmetic operators ‘+’ , ‘*’ and ‘/ ‘operators as
shown below:
A=B+C*D/F In this expression RHS of expression contains
B+C*D/F, here first preference is given to ‘*’ and ‘/ ‘and then to ‘+’.
This is decided by operator precedence given in C Language.

Let us say: B=5, C=7, D=9 and F=3, then value of A is: A= 5+7*9/3
This expression is evaluated in following steps:
A = 5 + (7*9)/3
A= 5+(63/3)
A=5+21
A=26

February 25, 2024 CSE, BMSCE 126


Operator Associativity
If two operators with same precedence occur in a complex expression,
another attribute of an operator called associativity takes control.
Associativity is the parsing direction used to evaluate an expression. It can
be either left to right or right to left .
For example: X= Y/Z*P%Q Here ‘/’, ‘*’, and ‘%’ are operators at same level.
But we evaluate this expression from LEFT to RIGHT (i.e. Associativity is
from Left to Right).

February 25, 2024 CSE, BMSCE 127


Evaluation of Expressions
Based on the Precedence and Associativity of operators here
we discuss evaluating various types of expressions.

Note: In this example 7/8 gives 0 as answer as it is


integer division the decimal portion is truncated.

February 25, 2024 CSE, BMSCE 128


Question

Evaluate the following Expression

int a=10, b=7, c=8, d=2, e;


e=a+b && c-d;

February 25, 2024 CSE, BMSCE 129


Answer

February 25, 2024 CSE, BMSCE 130


Question

Evaluate the following Expression

int a=10, b=7, c=8, d=2,e;


e=(a+b)*(c-d);

February 25, 2024 CSE, BMSCE 131


Answer

February 25, 2024 CSE, BMSCE 132


Question

Evaluate the following Expression

int a=10, b=7, c=8, d=2,e;


e=++a * c - d--;

February 25, 2024 CSE, BMSCE 133


Answer

(11)*8 – (2) =86

Note: Here d - - is post decrement


so its initial value (2) is used in
the expression.

February 25, 2024 CSE, BMSCE 134


Types of Conversions
Whenever mixed data occurs ‘type conversion’ comes into picture.
Two types of data type conversions are:
1. Automatic type conversions (Implicit conversions)
2. Manual type conversions (Explicit conversions)

February 25, 2024 CSE, BMSCE 135


Types of Conversions (Contd…)
Whenever mixed data occurs ‘type conversion’ comes into picture.
Two types of data type conversions are:
1. Automatic type conversions (Implicit conversions)
2. Manual type conversions (Explicit conversions)

In automatic conversion the operand/variable of smaller data type is


automatically converted to data type of larger size.
That is: char → int → long int → float → double → long double

Example:
int a=25;
float x=5,z;
z=x/a;
printf(“%f”,z);

In this example ‘x’ is float and ‘a’ is integer, a gets automatically converted to float
and answer is: z= (5.0/25.0) = 0.2

February 25, 2024 CSE, BMSCE 136


Types of Conversions (Contd…)
Side effects of automatic conversion:
int a=7, b;
float x;
b=a%x;

In the example given above we are using modulus or remainder (%) operator. Here ‘a’
is integer type and ‘x’ is float. But the catch is % can be used only with integers. So
‘x’ has to be automatically converted to float. But it is impossible as float is bigger
than integer. As a result Complier gives syntax error.
If we have to convert a variable of bigger size to smaller type we have to use manual
conversion (explicit conversion). Here is how we can use explicit conversion and
overcome the syntax error in previous example:

February 25, 2024 CSE, BMSCE 137


Types of Conversions (Contd…)
Another example of side effects in type conversions is here:
Example:
float a=25, b=4;
int x;
x=a/b;

The problem here is though 25.0/4.0 will result in 6.25, it is


stored in an integer variable ‘x’. As a result 6.25 is truncated
to 6. Therefore we have to be careful when mixed data
types are used.

February 25, 2024 CSE, BMSCE 138


Types of Conversions (Contd…)

February 25, 2024 CSE, BMSCE 139


Mathematical Functions

February 25, 2024 CSE, BMSCE 140


Arithmetic Expression

February 25, 2024 CSE, BMSCE 141


Arithmetic Expression

February 25, 2024 CSE, BMSCE 142


Mathematical Functions

February 25, 2024 CSE, BMSCE 143


Mathematical Functions

February 25, 2024 CSE, BMSCE 144


Mathematical Functions

February 25, 2024 CSE, BMSCE 145


Programming Examples and exercise

February 25, 2024 CSE, BMSCE 146


Write a C program to exchange the values of two variables
using a temporary variable and without using a temporary
variable.

February 25, 2024 CSE, BMSCE 147


Write a C program to exchange the values of two variables using
a temporary variable and without using a temporary variable.

#include<stdio.h>
main()
{
int a , b, temp;

printf("Enter the values of a and b:\n");


scanf("%d%d", &a,&b);
printf(“ a=%d and b=%d\n” , a ,b);
printf(“Exchanging Using a temporary variable\n”);
temp = a;
a = b;
b = temp;
printf(“a=%d and b=%d\n”,a,b);

printf(“Without using a temporary variable\n”);


a=a+b;
b=a-b;
a=a-b;
printf(“a=%d and b=%d\n”,a,b);
}

February 25, 2024 CSE, BMSCE 148


Write a C program to exchange the values of two variables using
a temporary variable and without using a temporary variable.

#include<stdio.h>
void main()
{
int a , b, temp;
printf("Enter the values of a and b:\n");
scanf("%d%d", &a,&b);
printf(“ a=%d and b=%d\n” , a ,b);
printf(“Exchanging Using a temporary variable\n”);
temp = a;
a = b;
b = temp;
printf(“a=%d and b=%d\n”,a,b);
printf(“Without using a temporary variable\n”);
a=a+b;
b=a-b;
a=a-b;
printf(“a=%d and b=%d\n”,a,b);
}

Output:
Enter the values of a and b:
5
10
a = 5 and b = 10
Exchanging Using a temporary variable\n”);
a = 10 and b = 5
Without using a temporary variable
a = 5 and b = 10

February 25, 2024 CSE, BMSCE 149


Write a C program to accept the temperature in Fahrenheit
and convert it into Celsius.

February 25, 2024 CSE, BMSCE 150


Write a C program to accept the temperature in Fahrenheit
and convert it into Celsius.

#include<stdio.h>
void main()
{
float ct,ft;
Printf(“Enter the temperature in Fahrenheit\n");
scanf(“%f”,&ft);

ct=(ft-32.0)/1.8;
printf(“Fahrenheit temperature = %f\n”, ft);
printf(“Celsius temperature = %f\n”, ct);
}

Output:
Enter the temperature in Fahrenheit
50
Fahrenheit temperature = 50.00
Celsius temperature = 10.00

February 25, 2024 CSE, BMSCE 151


Write a C program to find area and perimeter of rectangle

February 25, 2024 CSE, BMSCE 152


Write a C program to find area and perimeter of rectangle

#include<stdio.h>
void main()
{
int length,breadth,area,peri;
printf(“Enter the length and breadth of a rectangle\n”);
scanf(“%d%d”,&length,&breadth);
area=length*breadth;
peri=2*(length+breadth);
printf(“Area=%f\n Perimeter=%f\n”,area,peri);
}

Output:
enter the length and breadth of a rectangle
5
6
Area=30
Perimeter=22

February 25, 2024 CSE, BMSCE 153


Thanks for Listening

My contact details
Dr. Umadevi V, Associate Professor, Department of CSE, BMSCE
Staff Room: 4th floor, New Academic block

umadevi.cse@bmsce.ac.in

8762742909

February 25, 2024 CSE, BMSCE 154


Backslash Character Constants

‘\n’ New line To Take control at starting on the next


line.
‘\r’ Carriage return To take control at starting on the same
line.
‘\f’ from feed To take control at starting on the next
page.
February 25, 2024 CSE, BMSCE 155

You might also like