0% found this document useful (0 votes)
19 views53 pages

VedantJain PracFile

cc

Uploaded by

Vibhor Ohlan
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)
19 views53 pages

VedantJain PracFile

cc

Uploaded by

Vibhor Ohlan
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
You are on page 1/ 53

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING

PROGRAMMING FUNDAMENTALS

CO 101

LAB FILE

SUBMITTED TO: SUBMITTED BY:

Ms. XXX VEDANT JAIN


(24/A07/048)
S.No Objective Date Sign
EXPERIMENT 1

AIM
To write a program in C to find the sum and average of two numbers.

THEORY
Finding the sum and average of two numbers involves basic arithmetic
operations. The sum is obtained by adding the two numbers, and the
average is calculated by dividing the sum by the number of values, which in
this case is 2.

ALGORITHM
1. Start
2. Input num1 and num2
3. Set sum = num1 + num2
4. Set average = sum/2
5. Print “Sum is” followed by sum
6. Print “Average is” followed by average
7. Stop
FLOWCHART

CODE
#include <stdio.h>
/* Vedant Jain */
int main() {
int num1, num2;
scanf("%d%d", &num1, &num2);
int sum = num1 + num2;
int average = sum/2;
printf("Sum is %d\n", sum);
printf("Average is %d\n", average);

return 0;
}

OUTPUT
EXPERIMENT 2

AIM
To write a C Program to find the greatest of 3 numbers.

THEORY
To determine the greatest of three numbers, comparisons are performed
between each pair of numbers. The logic involves checking each number
against the others and determining which number is the highest.

ALGORITHM
1. Start
2. Input num1, num2, num3
3. If num1 is greater than or equal to num2 and num1 is greater than or
equal to num3, print “Greatest is” followed by num1
4. Otherwise if num2 is greater than or equal to num1 and num2 is
greater than or equal to num3, print “Greatest is” followed by num2
5. Otherwise print “Greatest is” followed by num3
6. Stop
FLOWCHART

CODE
#include <stdio.h>
/* Vedant Jain */
int main() {
int num1, num2, num3;
scanf("%d%d%d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("Greatest is %d\n", num1);
}
else if (num2 >= num1 && num2 >= num3) {
printf("Greatest is %d\n", num2);
}
else printf("Greatest is %d\n", num3);
return 0;
}

OUTPUT
EXPERIMENT 3

AIM
To write a C Program to find Simple Interest.

THEORY
Simple interest is calculated using the formula:

Simple Interest=P×R×T100\text{Simple Interest} = \frac{P \times R \times


T}{100}Simple Interest=100P×R×T​

where:

● P is the principal amount,


● R is the rate of interest per annum,
● T is the time in years.

This formula helps in determining the amount of interest earned or paid


over a period of time, based on the principal amount and interest rate.

ALGORITHM
1. Start
2. Input p, r, t
3. Set SI = p*r*t / 100
4. Print “Simple Interest Is” followed by SI
5. Stop
FLOWCHART

CODE
#include <stdio.h>
/* Vedant Jain */
int main() {
float p, r, t;
scanf("%f%f%f", &p, &r, &t);
printf("S.I is %f\n", p*r*t/100);
return 0;}

OUTPUT
EXPERIMENT 4

AIM
To write a C Program to find a character is vowel or consonant.

THEORY
In English, vowels are the letters A, E, I, O, U (both uppercase and
lowercase). All other alphabetical characters are consonants. This program
involves checking if a character belongs to the set of vowels or not. This
basic character comparison is fundamental in many programming tasks
that involve string and character manipulations.

ALGORITHM
1. Start
2. Input c
3. If c is equal to ‘a’ or c is equal to ‘e’ or c is equal to ‘i’ or c is equal to
‘o’ or c is equal to ‘u’; print “Vowel”
4. Otherwise, print “Consonant”
5. Stop
FLOWCHART

CODE
#include <stdio.h>
/* Vedant Jain */
int main() {
char c;
scanf("%c", &c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c ==
'u') printf("Vowel\n");
else printf("Consonant\n");
return 0;
}
OUTPUT
EXPERIMENT 5

AIM
Write a program that calculates the ticket price based on age with the
following conditions: age below 12 pay a ticket price of 5, age below 18 pay
a ticket price of 10, age below 60 pay a ticket price of 20, age over 60 play
a ticket price of 15.

THEORY
Ticket pricing can vary based on age, with different categories receiving
different rates. In this case, the program calculates the ticket price
according to the following age-based conditions:

● Age below 12: Ticket price is $5.


● Age below 18: Ticket price is $10.
● Age below 60: Ticket price is $20.
● Age 60 or above: Ticket price is $15.

ALGORITHM
1. Start
2. Input age
3. If age is less than 12, print “$5”
4. Otherwise if age is less than 18, print "$10"
5. Otherwise if age is less than 60, print "$20"
6. Otherwise, print "$15"
7. Stop
FLOWCHART
CODE
#include <stdio.h>
/* Vedant Jain */
int main() {
int age;

printf("Enter the age of the individual: ");


scanf("%d", &age);

if (age < 12) {


printf("$5\n");
} else if (age < 18) {
printf("$10\n");
} else if (age < 60) {
printf("$20\n");
} else {
printf("$15\n");
}

return 0;
}

OUTPUT
EXPERIMENT 6

AIM
To write a program in C that determines if a given year is a leap year.

THEORY
A year is a leap year if:

● It is divisible by 4.
● However, if it is also divisible by 100, it must be divisible by 400 to be
considered a leap year.

ALGORITHM
1. Start
2. Input year
3. If the year is divisible by 400, print "Leap year".
4. Otherwise, if the year is divisible by 100, print "Not a leap year".
5. Otherwise, if the year is divisible by 4, print "Leap year".
6. Otherwise, print "Not a leap year".
7. Stop
FLOWCHART
CODE
#include <stdio.h>

int main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

if (year % 400 == 0) {
printf("Leap year\n");
} else if (year % 100 == 0) {
printf("Not a leap year\n");
} else if (year % 4 == 0) {
printf("Leap year\n");
} else {
printf("Not a leap year\n");
}

return 0;
}

OUTPUT
EXPERIMENT 7

AIM
To write a C program that transforms a numerical grade into a letter grade.

THEORY
Grades are often categorized into letter grades for better interpretation. For
example:

● A grade between 91 and 100 is considered 'O' (Outstanding).


● A grade between 81 and 90 is considered 'A' (Excellent).
● A grade between 71 and 80 is considered 'B' (Good).
● A grade between 61 and 70 is considered 'C' (Average).
● A grade between 51 and 60 is considered 'D' (Pass).
● A grade below 51 is considered 'F' (Fail)..

ALGORITHM
1. Start
2. Input marks
3. If the grade is between 91 and 100, print 'O'.
4. Otherwise, if the grade is between 81 and 90, print 'A'.
5. Otherwise, if the grade is between 71 and 80, print 'B'.
6. Otherwise, if the grade is between 61 and 70, print 'C'.
7. Otherwise, if the grade is between 51 and 60, print 'D'.
8. Otherwise, print 'F'.
9. Stop
FLOWCHART

CODE
#include <stdio.h>
/* Vedant Jain */
int main() {
int grade;

printf("Enter the numerical grade: ");


scanf("%d", &grade);
if (grade >= 91 && grade <= 100) {
printf("O\n");
} else if (grade >= 81 && grade <= 90) {
printf("A\n");
} else if (grade >= 71 && grade <= 80) {
printf("B\n");
} else if (grade >= 61 && grade <= 70) {
printf("C\n");
} else if (grade >= 51 && grade <= 60) {
printf("D\n");
} else if (grade < 51) {
printf("F\n");
} else {
printf("Invalid grade\n");
}

return 0;
}

OUTPUT
EXPERIMENT 8

AIM
To write a program in C to determine if a given number is a prime number.

THEORY
A prime number is a natural number greater than 1 that has no positive
divisors other than 1 and itself. To determine if a number is prime:

● Check if the number is less than or equal to 1 (not prime).


● For numbers greater than 1, check if there are any divisors other than
1 and the number itself. A number is prime if it is only divisible by 1
and itself.

ALGORITHM
1. Start
2. Input number
3. If the number is less than or equal to 1, print "Not a prime number".
4. For numbers greater than 1, check divisibility from 2 up to the square
root of the number.
● If the number is divisible by any number in this range, print "Not a
prime number".
● If no divisors are found, print "Prime number".
5. Stop
FLOWCHART

CODE
#include <stdio.h>
#include <stdbool.h>

bool isPrime(int num) {


if (num <= 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
for (int i = 3; i * i <= num; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (isPrime(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}

return 0;
}

OUTPUT
EXPERIMENT 9

AIM
To write a C Program to find sum of a 5 digit number.

THEORY
● We will extract the digits of the numbers via modulus operation (Mod
10). Then, we will add to a variable and print.

ALGORITHM
1. Start
2. Input num
3. Set sum = 0
4. If num > 0, add num % 10 to sum
5. Else go to step 8
6. Set sum to sum/10
7. Go to step 4
8. Print sum
9. Stop
FLOWCHART
CODE
#include <stdio.h>
/* Vedant Jain */
int main (void) {
int n;
scanf("%d", &n);
int sum = 0;
while(n) {
sum += n%10;
n /= 10;
}
printf("%d\n", sum);
}

OUTPUT
EXPERIMENT 10

AIM
To write a C Program to reverse a 5 digit number.

THEORY
We will extract the digits of the numbers via modulus operation (Mod 10). A
second variable containing the reversed digit will be maintained.

ALGORITHM
1. Start
2. Input num
3. Set revNum = 0
4. If num > 0, revNum = revNum * 10 + num % 10
5. Else go to step 8
6. Set sum to sum/10
7. Go to step 4
8. Print revNum
9. Stop
FLOWCHART

CODE
#include <stdio.h>
/* Vedant Jain */
int main (void) {
int n;
scanf("%d", &n);
int revNum = 0;
while(n) {
revNum = revNum * 10 + n %10;
n /= 10;
}
printf("%d\n", revNum);
}

OUTPUT
EXPERIMENT 11

AIM
To write a C program that efficiently calculates the sum of natural numbers
up to a given integer.

THEORY
The sum of natural numbers up to a given integer n can be calculated
using the formula:

sum = n * (n + 1) / 2

This formula is derived from the arithmetic series formula.

ALGORITHM
1. Start
2. Read the input integer n from the user.
3. Calculate the sum using the formula sum = n * (n + 1) / 2.
4. Print the calculated sum.
5. Stop

CODE
#include <stdio.h>

int main() {
int n, sum;

printf("Enter a positive integer: ");


scanf("%d", &n);

if (n < 0) {
printf("Please enter a positive integer.\n");
} else {
sum = n * (n + 1) / 2;
printf("The sum of natural numbers up to %d is %d\n",
n, sum);
}

return 0;
}

OUTPUT
EXPERIMENT 12

AIM
To create a program that calculates the factorial of a given number.

THEORY
The program will take a positive integer as input and calculate its factorial
using a loop. The factorial is the product of all positive integers less than or
equal to the given number.

ALGORITHM
6. Start
7. Take input of a positive integer n
8. Initialise a variable factorial to 1
9. Repeat steps 5-6 for i from 1 to n
10. Multiply factorial by i
11. End loop
12. Print factorial
13. Stop

CODE
#include <stdio.h>

int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n, result;

printf("Enter a non-negative integer: ");


scanf("%d", &n);

if (n < 0) {
printf("Factorial of negative numbers is not
defined.\n");
} else {
result = factorial(n);
printf("The factorial of %d is %d\n", n, result);
}

return 0;
}

OUTPUT
EXPERIMENT 13

AIM
To write a C program to display the Fibonacci sequence up to a given
number of terms.

THEORY
The Fibonacci sequence is a series of numbers where each number is the
sum of the two preceding ones, starting from 0 and 1.

ALGORITHM
1. Start
2. Declare variables n, t1, t2, and nextTerm
3. Initialize t1 to 0 and t2 to 1
4. Take input for the number of terms n
5. Print the first two terms (0 and 1)
6. Repeat steps 7-9 n-2 times
7. Calculate the next term: nextTerm = t1 + t2
8. Print the nextTerm
9. Update the values: t1 = t2 and t2 = nextTerm
10. Stop

CODE
#include <stdio.h>

int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int n, result;

printf("Enter a non-negative integer: ");


scanf("%d", &n);

if (n < 0) {
printf("Factorial of negative numbers is not
defined.\n");
} else {
result = factorial(n);
printf("The factorial of %d is %d\n", n, result);
}

return 0;
}

OUTPUT
EXPERIMENT 14

AIM
To write a C program to find the greatest common divisor (GCD) of two
numbers using Euclid's algorithm.

THEORY
Euclid's algorithm is a recursive algorithm to find the greatest common
divisor of two non-negative integers. It works by repeatedly dividing the
larger number by the smaller number and replacing the larger number with
the remainder. The process continues until the remainder is 0. The GCD is
then the last non-zero remainder.

ALGORITHM
1. Start
2. Take input for two numbers a and b
3. If b is 0, return a as the GCD
4. Otherwise, call the function recursively with b as the first argument
and the remainder of a divided by b as the second argument
5. The GCD is the result of the recursive call

CODE
#include <stdio.h>

int gcd(int a, int b) {


if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
int a, b;

printf("Enter two numbers: ");


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

int result = gcd(a, b);

printf("GCD of %d and %d is %d\n", a, b, result);

return 0;
}

OUTPUT
EXPERIMENT 15

AIM
To write a C program to find the least common multiple (LCM) of two
numbers.

THEORY
The least common multiple of two numbers is the smallest number that is a
multiple of both numbers. One way to find the LCM is to use the formula:

LCM(a, b) = (a * b) / GCD(a, b)

where GCD(a, b) is the greatest common divisor of a and b.

ALGORITHM
1. Start
2. Take input for two numbers a and b
3. Calculate the GCD of a and b using the GCD algorithm
4. Calculate the LCM using the formula: LCM(a, b) = (a * b) /
GCD(a, b)
5. Print the LCM
6. Stop

CODE
#include <stdio.h>

int gcdf(int a, int b); // Function declaration

int main() {
int a, b, gcd, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

gcd = gcdf(a, b);


lcm = (a * b) / gcd;

printf("LCM of %d and %d is %d\n", a, b, lcm);

return 0;
}

// Function definition
int gcdf(int a, int b) {
if (b == 0)
return a;
else
return gcdf(b, a % b);
}

OUTPUT
EXPERIMENT 16

AIM
To display the characters from A to Z using a loop in C.

THEORY
The ASCII values of characters 'A' to 'Z' are consecutive. By iterating over a
loop from the ASCII value of 'A' to the ASCII value of 'Z', we can print the
corresponding characters.

ALGORITHM
1. Start
2. Initialize a variable ch with the ASCII value of 'A' (65).
3. Use a loop to iterate from ch to the ASCII value of 'Z' (90).
4. Inside the loop, print the character represented by the current value
of ch.
5. Increment ch by 1 in each iteration.
6. Stop

CODE
#include <stdio.h>

int main() {
char ch;

for (ch = 'A'; ch <= 'Z'; ch++) {


printf("%c ", ch);
}

return 0;
}
OUTPUT
EXPERIMENT 17

AIM
To count the number of digits in a given integer in C.

THEORY
We can repeatedly divide the integer by 10 until it becomes 0. Each division
removes one digit from the right. The number of divisions performed is
equal to the number of digits in the original integer.

ALGORITHM
1. Start
2. Input an integer num.
3. Initialise a counter count to 0.
4. If num is not equal to zero, increment ch by 1, Else go to step 7
5. Set num = num / 10
6. Go to step 4
7. Print count
8. Stop
#include <stdio.h>

int main() {
int num, count = 0;

printf("Enter an integer: ");


scanf("%d", &num);

while (num != 0) {
count++;
num /= 10;
}

printf("Number of digits: %d\n", count);

return 0;
}

OUTPUT
EXPERIMENT 18

AIM
To calculate the power of a given number in C.

THEORY
The power of a number x raised to the exponent y is calculated by
multiplying x by itself y times. This can be done using a loop or recursively.

ALGORITHM
1. Start
2. Input an integer num.
3. Input an integer pow.
4. Initialise i to 0.
5. If i is less than pow, set num = num * num
6. Else go to step 9
7. Increment i by 1.
8. Go to step 5
9. Print num
10. Stop
#include <stdio.h>

int main() {
int x, y, result = 1;

printf("Enter the base number: ");


scanf("%d", &x);

printf("Enter the exponent: ");


scanf("%d", &y);

for (int i = 0; i < y; i++) {


result *= x;
}

printf("%d raised to the power of %d is %d\n", x, y, result);

return 0;
}

OUTPUT
EXPERIMENT 19

AIM
To check whether a given number is a palindrome or not in C.

THEORY
A palindrome number reads the same backward as forward. To check if a
number is a palindrome, we can reverse it and compare it with the original
number.

ALGORITHM
1. Start
2. Input an integer num.
3. Initialise integer temp = num
4. Initialise integer named reversed to 0.
5. If num is not equal to zero, Set reversed = reversed + num % 10.
Else, go to step 8
6. Set num = num / 10
7. Go to step 5
8. If reversed is equal to temp, print “Palindrome” Else print “Not
Palindrome”
9. Stop
#include <stdio.h>

int main() {
int num, reversed = 0, originalNum;

printf("Enter an integer: ");


scanf("%d", &num);

originalNum = num;

while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}

if (originalNum == reversed) {
printf("%d is a palindrome number.\n", originalNum);
} else {
printf("%d is not a palindrome number.\n", originalNum);
}

return 0;
}

OUTPUT
EXPERIMENT 20

AIM
To display prime numbers within a specified interval in C.

THEORY
A prime number is a natural number greater than 1 that has no positive
divisors other than 1 and itself. To check if a number is prime, we can
iterate from 2 to the square root of the number and see if it is divisible by
any of them.

ALGORITHM
1. Start
2. Input the lower and upper intervals.
3. Iterate from the lower interval to the upper interval:
4. For each number, check if it's prime:
4.1. If the number is 1 or less than 2, it's not prime.
4.2. Iterate from 2 to the square root of the number:
4.2.1. If the number is divisible by any of them, it's not prime.
5. If the number is prime, print it.
6. Stop
#include <stdio.h>
#include <math.h>

int isPrime(int num) {


if (num <= 1) {
return 0; // 1 and numbers less than 2 are not prime
}

for (int i = 2; i <= sqrt(num); i++) {


if (num % i == 0) {
return 0; // Divisible by a number other than 1 and itself
}
}

return 1; // Prime number


}

int main() {
int lower, upper;

printf("Enter the lower interval: ");


scanf("%d", &lower);

printf("Enter the upper interval: ");


scanf("%d", &upper);

printf("Prime numbers between %d and %d:\n", lower, upper);

for (int i = lower; i <= upper; i++) {


if (isPrime(i)) {
printf("%d ", i);
}
}

return 0;
}

OUTPUT

You might also like