Subject Code: ESC-105-COM Fundamentals of Programming Languages
Subject Code: ESC-105-COM Fundamentals of Programming Languages
Example 2 :
Addition of two numbers.
Algorithm:
• Step1: Accept two numbers
• Step2: Perform addition of two numbers
• Step3: Display result.
Program Design Tools: Flowchart
Circle Connector
START
TAKE TWO
NUMBERS FROM
USER
DISPLAY RESULT
STOP
Write a Algorithm and draw the flow
chart for that
Algorithm: Flow chart:
Step1: Accept two numbers START
Print
Addition
STOP
History and importance C
C is a programming language developed at AT &
T’s Bell Laboratories of USA in 1972.
It was designed and written by a man named
Dennis Ritchie.
In the late seventies C began to replace the more
familiar languages of that time like PL/I, ALGOL,
etc.
ANSI C standard emerged in the early 1980s, this
book was split into two titles:
The original was still called Programming in C, and
the title that covered ANSI C was called
Programming in ANSI C
History and importance C
C Tokens
Character set, Identifiers, Keywords, constants,
Data types, type qualifiers, Declaration and
Initialization of variables.
C Tokens Token: The smallest individual units in
a program are called tokens.
The ‘C’ tokens are classified as:
1. Character set
2. Keywords
3. Identifiers
4. Constants
5. Operators
Character Set
Character set consists of
i) alphabet from A-Z or a-z
ii) digits from 0-9
iii) Special characters like (,), {,}, [,], , &, $, #,
%, ^, !, ?, :, ;, ",', .
iv) White space character: blank space v)
Escape sequences:
Character Set
\b : backspace • .
return 0;
}
Output Hello World
C Program to Find the Size of int, float, double
and char
#include <stdio.h> // Calculate and Print
// the size of floatType
int main() printf("\nSize of float is:
{
%ld", sizeof(floatType));
int integerType;
char charType;
float floatType; // Calculate and Print
double doubleType; // the size of doubleType
printf("\nSize of double is:
// Calculate and Print
%ld", sizeof(doubleType));
// the size of integer type
printf("Size of int is: %ld",
sizeof(integerType)); return 0;
}
// Calculate and Print OutputSize of int is: 4 Size of
// the size of charType
char is: 1 Size of float is: 4 Size
printf("\nSize of char is: %ld",
sizeof(charType)); of double is: 8
Program to Convert Fahrenheit to
Celcius in C
// Driver code
// Fahrenheit to Celsius int main()
#include <stdio.h> {
float f = 40;
Thank
You
Unit II
Operators
and
Expressions
Content
Operators and Expressions:
Arithmetic Operators, Relational Operators,
Logical Operators, Assignment Operators,
Increment and Decrement Operators, Conditional
Operators, Bitwise Operators,
Special Operators. Arithmetic Expressions,
Evaluation of Expressions, Precedence of Arithmetic
Operators,
Operator Precedence and Associativity, Mathematical
Functions
Operators
Operators are symbols which take one or more operands or
expressions and perform arithmetic or logical computations.
Operands are variables or expressions which are used in
conjunction with operators to evaluate the expression.
Combination of operands and operators form an expression.
Expressions are sequences of operators, operands, and
punctuators that specify a computation.
Evaluation of expressions is based on the operators that the
expressions contain and the context in which they are used.
Expression can result in a value and can produce side effects.
A side effect is a change in the state of the execution
environment.
Arithmetic Operators
Example
#include<stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %dn", add);
printf("Subtraction of a, b is : %dn", sub);
printf("Multiplication of a, b is : %dn", mul);
printf("Division of a, b is : %dn", div);
printf("Modulus of a, b is : %dn", mod);
return 0;
}
Relational Operators
Example
#include<stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
Else
{
printf("m and n are not equal");
}
return 0;
}
Logical Operators
#include <stdio.h> .
int main()
{
int m=40,n=20;
int a=20,p=30;
if (m>n && m !=0)
{ printf("&& Operator : Both conditions are truen"); }
if (a>p || p!=20)
{ printf("|| Operator : Only one condition is truen"); }
if (!(m>n && m !=0))
{ printf("! Operator : Both conditions are truen"); }
else
{ printf("! Operator : Both conditions are true. " "But, status is
inverted as falsen"); }
return 0;
}
Assignment Operators
Example
# include<stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Total+i
}
printf("Total = %d", Total);
return 0;
}
Increment and Decrement Operators
0 parenthesis ()
Result= (4*5)+(9+6)-8
Result= 20+15-8
Result= 35-8
Result= 27
Program for Arithmetical Expression
#include <stdio.h>
int main()
{
int x = 5, y = 10;
if (x == y)
{ printf("x is equal to y\n"); }
Else
{ printf("x is not equal to y\n"); }
return 0;
}
Program for Arithmetical Expression
#include <stdio.h>
int main()
{
int a = 5;
printf("%d", -++a*2);
return 0;
}
Output= -12
Mathematical Functions
1. Add: sum = a + b
This function calculates the sum of two numbers.
Note: This function does not require the <math.h> header.
Example:
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
printf("Addition: %d + %d = %d\n", num1, num2, sum);
return 0;
}
Output:
Addition: 10 + 5 = 15
2. Subtract: subtraction = a - b
.
This function subtracts one number from another. Note: This function
does not require the <math.h> header.
Example:
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 5;
int difference = num1 - num2;
printf("Subtraction: %d - %d = %d\n", num1, num2, difference);
return 0;
}
Output:
Subtraction: 10 - 5 = 5
EXAMPALE :
1) Write a program for Multiplication using math function
2) Write a program for Division using math function
Power: pow()
This function raises a number to a specified power.
Example:
#include <stdio.h>
#include <math.h>
int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
print(power:%2lf^%.2lf=%.2lf/n”, base, exponent, results ):
return 0;
}
Output:
Power: 2.00^3.00 = 8.00
Round Up: ceil()
This function rounds a floating-point number up to the
nearest integer.
Example:
#include <stdio.h>
#include <math.h>
int main() {
double num = 4.3;
double ceilResult = ceil(num);
printf("Ceiling: %.2lf rounded up to the nearest
integer is %.2lf\n", num, ceilResult);
return 0;
}
Output:
Ceiling: 4.30 rounded up to the nearest integer is 5.00
• Some other math function
.
1. Round Down: floor(): This function rounds a floating-point
number down to the nearest integer
2. Tangent: tan():This function calculates the tangent of an
angle.
3. Tangent: tan(): This function calculates the tangent of an
angle
4. Square Root: To find the square root of a number, use
the sqrt() function: Example printf("%f", sqrt(16));
5. ceil(number) rounds up the given number. It returns the
integer value which is greater than or equal to given
number.
6. floor(number)rounds down the given number. It returns the
integer value which is less than or equal to given number.
7. abs(number)returns the absolute value of given number.
Program
1) Write a program for integer arithmetic to convert a given number
of days into month and days.
2) C Program to Check Whether a Number is Positive, Negative, or
Zero
3) C Program to Check Whether Number is Even or Odd
4) C Program to Check Whether a Character is Vowel or
Consonant
5) C Program to Find Largest Number Among Three Numbers
6) C Program to Calculate Sum of Natural Numbers
7) C Program to Check Leap Year
8) C Program to Find Factorial of a Number
9) C Program to Print Fibonacci Series
10) C Program to Check Armstrong Number
11) C Program to Reverse a Number
12) C Program to Check Whether a Number is a Palindrome or Not
13) C Program to Check Whether a Number is Prime or Not
Thank
You