0% found this document useful (0 votes)
5 views18 pages

L8 Assign Arith Rel Operators Example Programs

The document provides an overview of various operators in C programming, including assignment, arithmetic, relational, logical, and bitwise operators. It includes examples of how to use these operators and presents several sample programs demonstrating operations such as multiplication, ASCII value retrieval, quotient and remainder calculation, and data type size determination. Additionally, it features programs for swapping numbers using different methods and calculating the volume of a sphere.

Uploaded by

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

L8 Assign Arith Rel Operators Example Programs

The document provides an overview of various operators in C programming, including assignment, arithmetic, relational, logical, and bitwise operators. It includes examples of how to use these operators and presents several sample programs demonstrating operations such as multiplication, ASCII value retrieval, quotient and remainder calculation, and data type size determination. Additionally, it features programs for swapping numbers using different methods and calculating the volume of a sphere.

Uploaded by

ishika.ag
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment, Arithmetic

and Relational Operators


Operators
• define how the variables and literals in the
expression will be manipulated.

Assignments Arithmetic
operators operators

Relational Logical
operators operators

Bitwise
operators
Operators
• Assignment operators
• Assignment operator (=) is used to assign a
value to a variable.
Operators

• Assignment operators
• In C lots of shortcut are possible by using Shorthand
operators.
• For ex.
• a = a +b can be written as a += b
• a = a *b can be written as a *= b
• a = a /b can be written as a /= b
• a = a -b can be written as a -= b
Operators Contd…

• Arithmetic operators: C supports five major operators.


• Addition +
• Subtraction –
• Division /
• Multiplication *
• Remainder %
Operators Contd…
Relational operators

• Equal to ==
• Not equal to !=
• Less than <
• Less than equal to <=
• Greater than and greater than equal to (>, >=)
Operators contd…

• Relational Operators

• The expression
operand1 relational-operator operand2
takes a value of 1 if the relationship is true and 0 if relationship
is false.
• Example
int a = 25, b = 30, c, d;
c = a < b;
d = a > b;
value of c will be 1 and that of d will be 0.
Example programs
Program to multiply two floating point numbers

#include <stdio.h>
int main()
{ double firstNumber, secondNumber, product;
printf("Enter two numbers: ");
// Stores two floating point numbers in variable firstNumber and secondNumber respectively
scanf("%lf %lf", &firstNumber, &secondNumber);
// Performs multiplication and stores the result in variable productOfTwoNumbers
product = firstNumber * secondNumber;
// Result up to 2 decimal point is displayed using %.2lf
printf("Product = %.2lf", product);
return 0;
}
Program to print ASCII value of a character
#include <stdio.h> Sure, ASCII values are like unique
numbers assigned to each letter,
int main()
number,, or symbol on a computer.
{ char c; For example, the ASCII value for
printf("Enter a character: "); the letter ‘A’ is 65, and for the
number ‘0’ is 48. Computers use
// Reads character input from the user
these values to understand and
scanf("%c", &c); store text. So, when you type a
// %d displays the integer value of a character letter on your keyboard, the
// %c displays the actual character computer uses its ASCII value to
know which letter you mean.
printf("ASCII value of %c = %d", c, c); These values make it possible for
return 0; computers to work with text and
} characters.
Program to compute Quotient and Remainder
#include <stdio.h>
int main()
{ int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}
Program to find the size of all data types
#include <stdio.h>
int main()
{
int a; long b; long long c; double e; long double f;
printf("Size of int = %d bytes \n", sizeof(a));
printf("Size of long = %d bytes\n", sizeof(b));
printf("Size of long long = %d bytes\n", sizeof(c));
printf("Size of double = %d bytes\n", sizeof(e));
printf("Size of long double = %d bytes\n", sizeof(f));
return 0;
}
1. Program to swap two
numbers
In Class 2. Program to calculate the
Programs to do volume of sphere
Program to Swap two numbers
#include<stdio.h> // value of temp (initial value of first) is assigned
to second
int main() {
second = temp;
double first, second, temp;
// %.2lf displays number up to 2 decimal points
printf("Enter first number: ");
printf("\nAfter swapping, first number =
scanf("%lf", &first); %.2lf\n", first);
printf("Enter second number: "); printf("After swapping, second number =
scanf("%lf", &second); %.2lf", second);
//value of first is assigned to temp return 0;
temp = first; }
//value of second is assigned to first
first = second;
Program to Swap two numbers without using 3rd
variable - using addition and subtraction

#include <stdio.h>
int main()
{
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
Program to Swap two numbers without using 3rd
variable -using multiplication and division
#include <stdio.h>
int main()
{
int x = 10, y = 5;

// Code to swap 'x' and 'y'


x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5

printf("After Swapping: x = %d, y = %d", x, y);

return 0;
}
Program to find volume of a sphere

Volume= (4/3)*pie*r3

#include <stdio.h>
int main()
{
int radius=48;
float pie=3.14285714286;
double volume=(4.0/3.0)*pie*(radius*radius*radius);
printf("Volume of the sphere=%f",volume);
}
Thank You !

You might also like