0% found this document useful (0 votes)
12 views35 pages

LAB1 Students

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

LAB1 Students

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

• Program : Set of Instructions

• Desktop Machine: understands binary language.


• As a programmer, writing complex instructions in Binary
is very difficult.
• So, Developers uses High Level Programming Language
like C, Java.
• Machine does not understand High Level Programming
Language.
• Job of Compiler is to convert code written in High Level
Language to Machine understandable code.
• A C program can only be executed on OS where it has
been compiled.
• C is Platform dependent Programming Language.
Execution Steps:

For checking gcc version and verify if gcc is installed cd .. [for getting out from
properly on the machine existing folder]
gcc --version
cd Desktop/ [for getting
If gcc is not installed, into Desktop folder]
sudo apt update
ll or ls [list all the files &
sudo apt install build-essential folders in the existing
folder]
For compiling .c file to an executable
gcc sample.c -o sample

For executing the executable file


./sample
Problem Statement:
To accept two integers from user & display
their sum.

Input:
Let the input be 2 integers stored as A and B

Output:
Display C

Operations:
Let C be an integer C such that
C =A +B
Specify the Including Standard Input output header file
preprocessor directive
Main function main() is the entry point of every C program. No other code will
Return type execute before and after main()

Curly braces specifies Identifiers


the scope of main
function Data type To display on terminal screen

Scan the input stream, you


will find 2 decimal values,
extract them and assign the
first decimal value to address
of num1 and second
Type Specifiers & or ampersand (address)
decimal value to address of
num2

Signifies address Value present at Value present at


of result1 address of num1 address of num2

Next line Specifies value of result1


• Data Types
• Identifiers
• Return types
• Type Specifiers
• Operators
Data Type
Consider the statement: int X;
Data Type gives :
1. Size of memory
size = 2bytes
2. Types of values that can be stored in the memory
X will store only integer type values
3. Types of operations that can be performed on the value stored.
Operations could be: + , -, *, /, %
Identifiers
Consider the statement: int X;
When assigning identifiers in C, you must follow these rules:
• The first character must be a letter or an underscore (_)
• The remaining characters can be letters, digits, or underscores
• Identifiers are case-sensitive, so "myVar" and "myvar" are different
identifiers
• Reserved words: Identifiers cannot be C keywords, such as "int" or
"double"
• Only the first 31 characters are significant
• Identifiers cannot contain white spaces
• Identifiers cannot contain special characters, except for underscores
• Each identifier must be unique within the scope it is defined in
• An identifier is a name given to a variable, function, constant, class, type,
sub, or property.
Various Types of Identifiers
Types of Identifiers Example
Variables Here the content of memory identified by identifier can int X=10;
change during program execution. So memory is of type …
variable. X=5;
….
X=8;
Constant Here the content of memory identified by identifier cannot const X=10;
change during program execution. So memory is of type ..
constant. X=5; //This is not allowed

Macro If set of instructions are repeated at many places in the #define X5


same program then we can define it by a MacroName //X=macroname
which can be used in the program instead of the set of //5 macro expression
Instructions. This is defining Macro.
Type Specifiers or Format Specifiers
Operators
• Assignment Operator (=)
int X=10;  Value 10 is assigned to a memory identified as X

LHS_Value= RHS_Value
LHS_Value signifies address
RHS_Value signifies value.
 Value at RHS is assigned to Address of LHS
int X= 10;
Value assigned
10
Address of X
X

RAM
int Y = X+2;
12
Value of X Y
Address of Y
Arithmetic Operators(Binary Operators, since two operands)
•+
• -
• *
• / (Quotient of division)
5/2 = 2 Integer division, since both operands are integers
5.4/2 =5.4/2.0 =2.7 Result is float division since one of the operand is float.

• %(modulus)
If a> b then a%b gives remainder of a/b
If a <b then a%b gives a
If a=b then a%b gives 0
Type Conversion
Implicit Type Conversion When lower data type gets int X= 3;
operated with higher data float Y=X;
type then lower data type //output: Y=3.0
gets implicitly converted to
higher data type.

No loss of precision.
Explicit Type Conversion When we need to convert float X=3.2;
higher data type to lower data int Y= (int) X;
type then explicit type //output: Y=3
conversion/ type casting is
required. General format:
(target_type) identifier
Loss of precision.
• Relational Operators • ShortHand Assignment Operators
< +=  i.e a+=b a=a+b
<= -=  i.e a-=b  a=a-b
> *=  i.e a*=b a=a*b
>= /=  i.e a/=b a=a/b
Returns Boolean value(1 bit) %=  i.e a%=b a=a%b
i.e either True or False

• Equality Operators
== Equal to
!= Not equal to
Returns Boolean value(1 bit)
i.e either True or False
Lab1: Use the formatted input/output statements,
operators and expressions of C language
A: Write a C program to input 2 numbers. Perform addition, subtraction,
multiplication, division and modulus and display output.
B: WAP to Convert Celsius to Fahrenheit. Answer should be rounded upto 2
decimal places
fahrenheit=(celsius×9/5)+32
C: WAP to Convert Foot to Meter. Answer should be rounded upto 3 decimal
places
meter=foot×0.3048
D: Write a C program to convert days into year, month and days.
double a function that returns the smallest integer value
⌈x⌉
ceil(double x) greater than or equal to x in type double
double a function that returns the largest integer value less
⌊x⌋
floor(double x) than or equal to x in type double
double a function that evaluates the absolute value of x in
IxI
fabs(double) type double

x​^(1/2) double a function that evaluates the square root of x


sqrt(double x)
double
x^y pow(double x, a function that evaluates x raised to the power y
double y)
Lab1B: F-Division
E. Create a program that asks the user for a number, calculates its square and
cube, and then prints the results. Write the solution both with and without using
the math library.

F: Write a C program to calculate the CGPI based on subject marks (each subject
out of 100) and attendance for each subject. The program should take input for
the marks of each subject and whether attendance for that subject is above or
below 75%:
•Input 0 if attendance is less than 75% for a subject.
•Input 1 if attendance is greater than or equal to 75% for a subject.
•If the attendance is below 75% for a subject (i.e., input is 0), the program should
ignore the marks for that subject and consider them as 0 when calculating the
CGPI.
Lab1B: H-Division
E: Write a C program to calculate the Euclidean distance between two points in a
2D plane. The user provides the coordinates of two points (x1,y1) and (x2,y2).

F. Write a C program that accepts two numbers from the user and swaps their
values. The program should demonstrate two methods for swapping:
1.Using a third variable.
2.Without using a third variable.( use shorthand operators if possible)
Lab1B: E-Division
E: Write a C program that calculates the surface area and volume of a
cylinder. The program should demonstrate two methods for obtaining
the value of π (Pi):
• Using a macro for Pi.
• Using the existing M_PI constant from the math.h library.

Surface Area (SA) of a cylinder: SA=2πr(r+h)


Volume (V) of a cylinder: V=π(r^2)h
Where:
r is the radius of the base of the cylinder.
h is the height of the cylinder.
Lab1B: E-Division
F: Calculate the gross salary.
Get input of basic salary from the user.
Calculate HRA ,TA, DA and then print the breakup of same.

TA=15%of basic
DA=90% of basic
HRA=20% of basic
Gross=Basic+ TA+HRA+DA
• https://www.scaler.com/topics/c/math-h-functions-in-c/

You might also like