List Of Programs
SESSION 1
session 1To accept two numbers and perform basic arithmetic
operations (+, - *, /, %)
Step 1: Start.
Step 2: Read two numbers.
Step 3: Compute addtion, subtraction, multiplication, division and modulo division between these numbers.
Step 4: Display results.
Step 5: Stop.
Step 2: Read two numbers.
Step 3: Compute addtion, subtraction, multiplication, division and modulo division between these numbers.
Step 4: Display results.
Step 5: Stop.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int sum = a+b;
int subtraction = a-b;
int multiplication = a*b;
float division = a/(float)b;
int moduloDivision = a%b;
printf("Sum = %d\n", sum);
printf("Subtraction = %d\n", subtraction);
printf("Multiplication = %d\n", multiplication);
printf("Division = %f\n", division);
printf("Modulo division = %d\n", moduloDivision);
return 0;
}
session 1To find the volume of a Sphere using the formula,
Volume = (4/3) π r³
Step 1: Start.
Step 2: Read radius.
Step 3: Compute volume = (4/3)*π*radius³
Step 4: Display volume.
Step 5: Stop.
Step 2: Read radius.
Step 3: Compute volume = (4/3)*π*radius³
Step 4: Display volume.
Step 5: Stop.
#include <stdio.h>
int main()
{
int r;
float volume;
printf("Enter radius: ");
scanf("%d", &r);
volume = (4/3.0)*3.14*r*r*r;
printf("Volume of sphere with radius %d is: = %f\n", r, volume);
return 0;
}
session 1To find the area of a Triangle with 3 sides given using
formula, Area = √(s(s-a) (s-b) (s-c))
Step 1: Start.
Step 2: Read three sides a, b, c.
Step 3: Compute semi parameter, s = [(a+b+c)/2] and area = √(s(s-a)(s-b)(s-c))
Step 4: Display area.
Step 5: Stop.
Step 2: Read three sides a, b, c.
Step 3: Compute semi parameter, s = [(a+b+c)/2] and area = √(s(s-a)(s-b)(s-c))
Step 4: Display area.
Step 5: Stop.
#include <stdio.h>
// To compute square root
#include <math.h>
int main()
{
int a, b, c;
float s, area;
printf("Enter a, b and c: ");
scanf("%d %d %d", &a, &b, &c);
s = (a+b+c)/2.0;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle is: %f\n", area);
return 0;
}
SESSION 2
session 2 Given the values of the variables x, y and z, write a
program to rotate their values such that x has the value
of y, y has the value of z, and z has the value of x.
Step 1: Start.
Step 2: Declare temp and read three variables x, y and z and display them.
Step 3: Swap value of temp with x, value of x with y and value of z with temp.
Step 4: Display swapped values.
Step 5: Stop.
Step 2: Declare temp and read three variables x, y and z and display them.
Step 3: Swap value of temp with x, value of x with y and value of z with temp.
Step 4: Display swapped values.
Step 5: Stop.
#include <stdio.h>
int main()
{
int x, y, z;
int temp;
printf("Enter x, y, z: ");
scanf("%d %d %d", &x, &y, &z);
printf("Entered values:\n");
printf("x=%d, y=%d and z=%d\n", x, y, z);
temp = x;
x = y;
y = z;
z = temp;
printf("Swapped values:\n");
printf("x=%d, y=%d and z=%d\n", x, y, z);
return 0;
}
session 2 To read floating-pointing number and then displays the
right-most digit of the integral part of the number.
Step 1: Start.
Step 2: Declare integral and rightmost part and read floating-pointing number.
Step 3: Compute integral part using type casting and rightmost part by modulo operator.
Step 4: Display rightmost part.
Step 5: Stop.
Step 2: Declare integral and rightmost part and read floating-pointing number.
Step 3: Compute integral part using type casting and rightmost part by modulo operator.
Step 4: Display rightmost part.
Step 5: Stop.
#include <stdio.h>
int main()
{
float number;
int integralPart, rightmostPart;
printf("Enter floating-pointing number: ");
scanf("%f", &number);
integralPart = (int)number;
rightmostPart = integralPart%10;
printf("Rightmost integral part = %d\n", rightmostPart);
return 0;
}
session 2 To read floating-pointing number, separate and displays
the integral and decimal part of the given.
Step 1: Start.
Step 2: Read floating-pointing number.
Step 3: Compute integral part using type casting. Decimal part is difference of number and integral part.
Step 4: Display decimal part and integral part.
Step 5: Stop.
Step 2: Read floating-pointing number.
Step 3: Compute integral part using type casting. Decimal part is difference of number and integral part.
Step 4: Display decimal part and integral part.
Step 5: Stop.
#include <stdio.h>
int main()
{
float number, decimalPart;
int integralPart;
printf("Enter floating-pointing number: ");
scanf("%f", &number);
integralPart = (int)number;
decimalPart = number - integralPart;
printf("Decimal part = %f and integral part = %d\n", decimalPart, integralPart);
return 0;
}
session 2 To print the size of various data types in "C"
programming language using "sizeof" operator
Step 1: Start.
Step 2: Display size of char, int, float and double datatype using sizeof() operator.
Step 3: Stop.
Step 2: Display size of char, int, float and double datatype using sizeof() operator.
Step 3: Stop.
#include <stdio.h>
int main()
{
printf("Size of char : %d bytes\n", sizeof(char));
printf("Size of int : %d bytes\n", sizeof(int));
printf("Size of float : %d bytes\n", sizeof(float));
printf("Size of double : %d bytes\n", sizeof(double));
return 0;
}
SESSION 3
session 3 Using "ternary" operator, check whether a given
number is Positive or Negative
Step 1: Start.
Step 2: Read number.
Step 3: Check whether number is > 0 (positive); else check if number is equal to zero, else negative.
Step 4: Display positive, zero or negative.
Step 5: Stop.
Step 2: Read number.
Step 3: Check whether number is > 0 (positive); else check if number is equal to zero, else negative.
Step 4: Display positive, zero or negative.
Step 5: Stop.
#include <stdio.h>
int main()
{
int number;
printf("Enter number: ");
scanf("%d", &number);
(number > 0)?
printf("Number is positive.\n"):
(number == 0)? printf("Number is zero.\n"): printf("Number is negative.\n");
return 0;
}
session 3 Using "ternary" operator, to check whether a given Year
is a Leap Year or Not
Step 1: Start.
Step 2: Read year.
Step 3: Check if year is completely divisible by 400 then leap year else if year is divible by 4 and not divisible by 100 then also leap year; else not a leap year.
Step 4: Display leap year or not.
Step 5: Stop.
Step 2: Read year.
Step 3: Check if year is completely divisible by 400 then leap year else if year is divible by 4 and not divisible by 100 then also leap year; else not a leap year.
Step 4: Display leap year or not.
Step 5: Stop.
#include <stdio.h>
int main()
{
int year;
printf("Enter year: ");
scanf("%d", &year);
(year%400 == 0)?
printf("%d is a leap year.\n", year):
(year%4 == 0 && year%100 != 0)?
printf("%d is a leap year.\n", year):
printf("%d isn't a leap year.\n", year);
return 0;
}
session 3 Using "ternary" operator, find the largest of three numbers
Step 1: Start.
Step 2: Read three numbers a, b and c.
Step 3: Check if a is greater than both a and c ->
then a is largest
else if b is greater than c -> then b is largest
else c is largest.
Step 4: Display largest.
Step 5: Stop.
Step 2: Read three numbers a, b and c.
Step 3: Check if a is greater than both a and c ->
then a is largest
else if b is greater than c -> then b is largest
else c is largest.
Step 4: Display largest.
Step 5: Stop.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
(a>b && a>c)?
printf("%d is largest.\n", a):
(b>c)?
printf("%d is largest.\n", b): printf("%d is largest.\n", c);
return 0;
}
session 3 Using "&" operator, check whether a given number is
Odd or Even
Step 1: Start.
Step 2: Read a number.
Step 3: Check whether number gives 1 after bitwise-and(&) operation with 1 then odd; else even.
Step 4: Display even or odd.
Step 5: Stop.
Step 2: Read a number.
Step 3: Check whether number gives 1 after bitwise-and(&) operation with 1 then odd; else even.
Step 4: Display even or odd.
Step 5: Stop.
#include <stdio.h>
int main()
{
int number;
printf("Enter number: ");
scanf("%d", &number);
(number&1 == 1)?
printf("%d is odd.\n", number):
printf("%d is even.\n", number);
return 0;
}
SESSION 4
session 4To illustrate the use of increment operator (postfix &
prefix)
Step 1: Start.
Step 2: Read a number.
Step 3: Display number before and after performing postfix and prefix increment.
Step 4: Stop.
Step 2: Read a number.
Step 3: Display number before and after performing postfix and prefix increment.
Step 4: Stop.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Number = %d\n", number);
printf("Printing number and doing postfix increment: %d\n", number++);
printf("Number after postfix increment = %d\n\n", number);
printf("Number = %d\n", number);
printf("Printing number and doing prefix increment: %d\n", ++number);
printf("Number after prefix increment = %d\n\n", number);
return 0;
}
session 4To illustrate the use of decrement operator (postfix &
prefix)
Step 1: Start.
Step 2: Read a number.
Step 3: Display number before and after performing postfix and prefix decrement.
Step 4: Stop.
Step 2: Read a number.
Step 3: Display number before and after performing postfix and prefix decrement.
Step 4: Stop.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Number = %d\n", number);
printf("Printing number and doing postfix decrement: %d\n", number--);
printf("Number after postfix decrement = %d\n\n", number);
printf("Number = %d\n", number);
printf("Printing number and doing prefix decrement: %d\n", --number);
printf("Number after prefix decrement = %d\n\n", number);
return 0;
}
session 4To perform the following using bitwise operators:
c = a & b ; d = a | b ; e = ~a
f = a >> n; g = a << n; h = a ^ b
Step 1: Start.
Step 2: Read three numbers.
Step 3: Compute binary and, or, not, xor, right shift and left shift operations.
Step 4: Display results.
Step 5: Stop.
Step 2: Read three numbers.
Step 3: Compute binary and, or, not, xor, right shift and left shift operations.
Step 4: Display results.
Step 5: Stop.
#include <stdio.h>
int main()
{
int a, b, n;
int c, d, e, f, g, h;
printf("Enter three number a, b and n: ");
scanf("%d %d %d", &a, &b, &n);
c = a&b;
d = a|b;
e = ~a;
f = a>>n;
g = a<<n;
h = a^b;
printf("a&b = %d\n", c);
printf("a|b = %d\n", d);
printf("~a = %d\n", e);
printf("a>>n = %d\n", f);
printf("a<<n = %d\n", g);
printf("a^b = %d\n", h);
return 0;
}
SESSION 5
session 5To determine whether a given number is "Odd" or
"Even" and print the message NUMBER IS EVEN or
NUMBER IS ODD with and without using "else"
statement.
Step 1: Start.
Step 2: Read a number.
Step 3:
Step 5: Stop.
Step 2: Read a number.
Step 3:
- If number gives remainder 0 after division by 2 then it is even, else odd.
- Compute even or odd.
Step 5: Stop.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("With using 'else' statement:\n");
if (n%2 != 0)
{
printf("NUMBER IS ODD\n");
}
else
{
printf("NUMBER IS EVEN\n");
}
printf("Without using 'else' statement:\n");
if (n%2 != 0)
{
printf("NUMBER IS ODD\n");
}
if (n%2 == 0)
{
printf("NUMBER IS EVEN\n");
}
return 0;
}
session 5To determine whether a given number is "Positive",
Negative" or "ZERO" and print the message
"NUMBER IS POSITIVE", "NUMBER IS
NEGATIVE" or "NUMBER IS ZERO" using nested
"if" statement.
Step 1: Start.
Step 2: Read a number.
Step 3: Check whether number is greater than, or equal to or less than zero.
Step 4: Display number is negative, zero or positive.
Step 5: Stop.
Step 2: Read a number.
Step 3: Check whether number is greater than, or equal to or less than zero.
Step 4: Display number is negative, zero or positive.
Step 5: Stop.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n >= 0)
{
if (n == 0)
{
printf("NUMBER IS ZERO.\n");
}
else
{
printf("NUMBER IS POSITIVE.\n");
}
}
else
{
printf("NUMBER IS NEGATIVE.\n");
}
return 0;
}
session 5To compute all the roots of a quadratic equation by
accepting the non-zero coefficients. Print appropriate
messages.
Step 1: Start.
Step 2: Read a number.
Step 3: Check whether number is greater than, or equal to or less than zero.
Step 4: Display number is negative, zero or positive.
Step 5: Stop.
Step 2: Read a number.
Step 3: Check whether number is greater than, or equal to or less than zero.
Step 4: Display number is negative, zero or positive.
Step 5: Stop.
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
printf("Enter coefficient of x², x and a constant: ");
scanf("%f %f %f", &a, &b, &c);
if (a != 0)
{
float x1, x2;
float d = b*b-(4*a*c);
if (d > 0)
{
printf("Roots are real and different:\n");
x1 = (-b + sqrt(d)) / (2*a);
x2 = (-b + sqrt(d)) / (2*a);
printf("x1: %f, x2: %f\n", x1, x2);
}
else if (d == 0)
{
printf("Roots are real and equal:\n");
x1 = -b / (2*a);
printf("x1 = x2 = %f\n", x1);
}
else
{
printf("Roots are imaginary:\n");
float realPart = -b / (2*a);
float imgPart = sqrt(-d) / (2*a);
printf("x1: %f+%fi, x2: %f-%fi\n", realPart, imgPart, realPart, imgPart);
}
}
else
{
printf("Equation is not quadratic.\n");
}
return 0;
}
SESSION 6
session 6To generate an electricity bill by accepting meter number of the consumer, number of units consumed
and print out the detail charges for the below scenario:
An electricity board charges the following rates for the
use of electricity:
- for the first 200 units 80 paise per unit
- for the next 100 units 90 paise per unit
- beyond 300 units Rs 1 per unit
All users are charged a minimum of Rs. 100 as meter
charge. If the total amount is more than Rs. 400, then an
additional surcharge of 15% of total amount is charged.
Step 1: Start.
Step 2: Read meter number and units.
Step 3: Compute total amount according to units consumed. (According to question)
Step 4: Display meter number and total amount.
Step 5: Stop.
Step 2: Read meter number and units.
Step 3: Compute total amount according to units consumed. (According to question)
Step 4: Display meter number and total amount.
Step 5: Stop.
#include <stdio.h>
int main()
{
int meterNumber;
float units, totalAmount = 100;
printf("Enter meter number and number of units consumed: ");
scanf("%d %f", &meterNumber, &units);
if (units <= 200)
{
totalAmount += units*0.8;
}
else if (units <= 200+100)
{
totalAmount += 200*0.8;
totalAmount += (units-200)*0.9;
}
else
{
totalAmount += 200*0.8;
totalAmount += 100*0.9;
totalAmount += (units-300)*1;
}
if (totalAmount > 400)
{
totalAmount += totalAmount*0.15;
}
printf("Total totalAmount for Meter-%d is: RS %.2f\n", meterNumber, totalAmount);
return 0;
}