CSELAB

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING
PROGRAMMING FUNDAMENTALS

CO 101
LAB FILE

SUBMITTED TO: SUBMITTED BY:


Mr. Manoj Sethi Priyanshu Barik
24/A05/051
Computer Science and Engineering (CSE-5)
AY: 2024-2025
INDEX

S. No. Objective Date Sign


1 C "Hello, World!" Program 23/08/2024
2 C Program to Print an Integer (Entered by the User) 23/08/2024
3 C Program to Add Two Integers
4 C Program to Multiply Two Floating-Point Numbers
INDEX
S. No. Objective Sign
Date
1 C "Hello, World!" Program 23/08/2024
2 C Program to Print an Integer (Entered by the User) 23/08/2024
3 C Program to Add Two Integers
4 C Program to Multiply Two Floating-Point Numbers
EXPERIMENT 1

AIM: C "Hello, World!" Program

Theory:
The "Hello, World!" program illustrates basic programming syntax and structure, including
execution functions and output statements. The printf function is key, as it displays text on the
console using format specifiers. This foundational example helps beginners grasp functions,
data types, and the significance of syntax in C programming.

CODE:
#include <stdio.h>

int main() {
printf("Hello,
World!");

return 0;

OUTPUT:
EXPERIMENT 2

AIM: C Program to Print an Integer (Entered by the User)

Theory:
In C programming, printf and scanf are essential functions for output and input operations. The
printf function outputs formatted text to the console, using format specifiers like %d to define
how to display different data types, such as integers. Conversely, scanf reads user input, also
utilizing format specifiers to interpret the type of input expected; for example, %d indicates that
an integer is being read. The corresponding variable must be passed by reference using the &
operator. Together, these functions enable basic user interaction, allowing programs to
communicate with users effectively.
CODE:
#include <stdio.h>

int main() {
int number;

printf("Enter an
integer: ");
scanf("%d",
&number);
printf("You entered: %d\n",

number); return 0;
}

OUTPUT:
EXPERIMENT 3

AIM: C Program to Add Two Integers


Theory:

Here's a brief theory for a C program that adds two integers:


The program uses #include <stdio.h> to include the Standard Input-Output library, enabling
functions like printf and scanf for input and output. Two integer variables are defined to hold the
values to be added. The program then prompts the user to enter these values, which are stored
using scanf. After reading the values, the program calculates their sum by adding the two
integers and stores it in a third variable. Finally, the sum is printed using printf.

CODE:
#include <stdio.h>

int main(){

int a,b,sum;

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

sum=a+b;

printf("The sum is : %d",sum);

return 0;

OUTPUT:
EXPERIMENT 4

AIM: C Program to Multiply Two Floating-Point Numbers


Theory:
The program includes `stdio.h` for input-output functions. It defines two `float` variables for
storing user inputs. The user is prompted to enter two floating-point numbers using `scanf`. The
program then multiplies these numbers, stores the result in another `float` variable, and
displays the product using `printf`.

CODE:

#include <stdio.h>
int main() {

float a, b, product;

printf("Enter two numbers: ");

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

product = a * b;

printf("The product is: %.2f\n", product);

return 0;

OUTPUT:
EXPERIMENT 5

AIM: C Program to Find ASCII Value of a Character


Theory:
The program includes `stdio.h` for input-output functions. It takes a character input from the
user, then uses `printf` to display its ASCII value by directly printing the character as an integer.

CODE:

#include <stdio.h>

int main() {
char a;
scanf("%c", &a);
int ascii = (int)a;
printf("The ASCII value is: %d\n", ascii);
return 0;
}

OUTPUT:
EXPERIMENT 6

AIM: C Program to Compute Quotient and Remainder


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter two
integers (dividend and divisor), then calculates the quotient and remainder using division and
modulus operators. Finally, it displays both results using `printf`.
CODE:
#include <stdio.h>

int main() {
int divisor, dividend, quotient, remainder;
printf("Enter divisor and dividend: ");
scanf("%d %d", &dividend, &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}

OUTPUT:
EXPERIMENT 7

AIM: C Program to Find size of int, float, double and char


Theory:
The program includes `stdio.h` for input-output functions. It uses the `sizeof` operator to
determine the sizes of `int`, `float`, `double`, and `char` data types in bytes. The results are
displayed using `printf`.
CODE:
#include <stdio.h>

int main() {
printf("The size of int is: %d bytes\n", sizeof(int));
printf("The size of float is: %d bytes\n", sizeof(float));
printf("The size of double is: %d bytes\n", sizeof(double));
printf("The size of char is: %d bytes\n", sizeof(char));
return 0;
}

OUTPUT:
EXPERIMENT 8

AIM: C Program to Demonstrate the Working of Keyword Long


Theory:
The program includes `stdio.h` for input-output functions. It defines variables using the `long`
keyword to demonstrate its use for larger integer values. The program assigns values to these
variables, then displays their sizes and values using `printf`, illustrating the capability of `long`
to store larger numbers compared to regular `int`.

CODE:

#include <stdio.h>

int main() {
long int a;
long long int b;
unsigned long int c;
printf("Size of long int: %lu bytes\n", sizeof(a));
printf("Size of long long int: %lu bytes\n", sizeof(b));
printf("Size of unsigned long int: %lu bytes\n", sizeof(c));
a = 123456789L;
b = 123456789123456789LL;
c = 123456789UL;
printf("Value of long int: %ld\n", a);
printf("Value of long long int: %lld\n", b);
printf("Value of unsigned long int: %lu\n", c);
return 0;
}

OUTPUT:
EXPERIMENT 9

AIM: C Program to Swap Two Numbers


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter two
numbers, then swaps their values using a temporary variable. Finally, it displays the swapped
numbers using `printf`.

CODE:
#include <stdio.h>

int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}

OUTPUT:
EXPERIMENT 10

AIM: C Program to Check Whether a Number is Even or Odd


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter an
integer, checks if the number is even or odd using the modulus operator, and then displays the
result using `printf`.

CODE:

#include <stdio.h>

int main() {

int a, b, temp;

printf("Enter two numbers: ");

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

temp = a;

a = b;

b = temp;

printf("After swapping:\n");

printf("a = %d\n", a);

printf("b = %d\n", b);

return 0;

}
OUTPUT:
EXPERIMENT 11

AIM: C Program to Check Whether a Character is a Vowel or Consonant


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a
character, checks if it is a vowel (a, e, i, o, u) or a consonant by comparing it against these letters,
and then displays the result using `printf`.

CODE:
#include <stdio.h>

int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'a' ||
ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
printf("%c is a vowel.\n", ch);
}
else {
printf("%c is a consonant.\n", ch);
}
return 0;
}

OUTPUT:
EXPERIMENT 12

AIM: C Program to Find the Largest Number Among Three Numbers


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter three
numbers, then compares them using conditional statements to determine the largest number.
Finally, it displays the largest number using `printf`.

CODE:

#include <stdio.h>

int main() {

int num1, num2, num3;

printf("Enter three numbers: ");

scanf("%d %d %d", &num1, &num2, &num3);

if (num1 >= num2 && num1 >= num3) {

printf("%d is the largest number.\n", num1);

} else if (num2 >= num1 && num2 >= num3) {

printf("%d is the largest number.\n", num2);

} else {

printf("%d is the largest number.\n", num3);

return 0;

}
OUTPUT:
EXPERIMENT 13

AIM: C Program to Find Roots of a Quadratic Equation


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter the
coefficients of a quadratic equation (a, b, and c). It calculates the discriminant to determine the
nature of the roots (real and distinct, real and equal, or complex). Based on the discriminant, the
program computes and displays the roots using `printf`.

CODE:

#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %.2f\n", root1);
printf("Root 2 = %.2f\n", root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("Roots are real and the same.\n");
printf("Root 1 = Root 2 = %.2f\n", root1);
}
else {
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);
printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);
}
return 0;
}
OUTPUT:
EXPERIMENT 14
OUTPUT:

AIM: C Program to Check Leap Year


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a year
and checks whether it is a leap year based on the rules: a year is a leap year if it is divisible by 4
but not by 100, or if it is divisible by 400. Finally, it displays the result using `printf`.

CODE:

#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
} else {
printf("%d is a leap year.\n", year);
}
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}

OUTPUT:
EXPERIMENT 15

AIM: C Program to Check Whether a Number is Positive or Negative


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a number
and checks if it is positive or negative using conditional statements. Finally, it displays the result
using `printf`.

CODE:
#include <stdio.h>
int main() {
float number;
printf("Enter a number: ");
scanf("%f", &number);
if (number > 0) {
printf("%.2f is a positive number.\n", number);
}
else if (number < 0) {
printf("%.2f is a negative number.\n", number);
}
else {
printf("The number is zero.\n");
}
return 0;
}

OUTPUT:
EXPERIMENT 16

AIM: C Program to Check Whether a Character is an Alphabet or not


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a
character and checks whether it is an alphabetic character (A-Z, a-z) using conditional
statements. Finally, it displays the result using `printf`.

CODE:
#include <stdio.h>
int main() {

char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("%c is an alphabet.\n", ch);
}
else {
printf("%c is not an alphabet.\n", ch);
}
return 0;
}

OUTPUT:
EXPERIMENT 17

AIM: C Program to Calculate the Sum of Natural Numbers


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a natural
number and calculates the sum of all natural numbers up to that number using a loop or the
formula \( \text{sum} = \frac{n(n + 1)}{2} \). Finally, it displays the sum using `printf`.

CODE:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a natural number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("The sum of natural numbers up to %d is: %d\n", n, sum);
return 0;
}

OUTPUT:
EXPERIMENT 18

AIM: C Program to Find Factorial of a Number


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a non-
negative integer and calculates its factorial using a loop or recursion. Finally, it displays the result
using `printf`.

CODE:
#include <stdio.h>
int main() {
int n, product = 1;
printf("Enter a natural number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
product *= i;
}
printf("The factorial of %d is: %d\n", n, product);
return 0;
}

OUTPUT:
EXPERIMENT 19

AIM: C Program to Generate Multiplication Table


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a number
and then generates its multiplication table using a loop. Each product is displayed using `printf`,
showing the number multiplied by integers from 1 to 10.

CODE:
#include <stdio.h>
int main() {
int n;
printf("Enter a number to generate its multiplication table: ");
scanf("%d", &n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}

OUTPUT:
EXPERIMENT 20

AIM: C Program to Display Fibonacci Sequence


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter the
number of terms for the Fibonacci sequence and then generates the sequence using a loop or
recursion. The program displays each term in the sequence using `printf`, where each term is
the sum of the two preceding ones.

CODE:
#include <stdio.h>
int main() {

int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 1; i <= n; i++) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
OUTPUT:
EXPERIMENT 21

AIM: C Program to Find GCD of two Numbers


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter two
integers and calculates the greatest common divisor (GCD) using the Euclidean algorithm. Finally,
it displays the GCD using `printf`.

CODE:
#include <stdio.h>
int main() {
int a, b, gcd;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
while (b != 0) {
gcd = b;
b = a % b;
a = gcd;
}
printf("GCD is: %d\n", a);
return 0;
}

OUTPUT:
EXPERIMENT 22

AIM: C Program to Find LCM of two Numbers


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter two
integers and calculates the least common multiple (LCM) using the formula \( \text{LCM}(a, b) =
\frac{|a \times b|}{\text{GCD}(a, b)} \). It first finds the GCD using the Euclidean algorithm, then
computes the LCM, and displays the result using `printf`.

CODE:
#include <stdio.h>
int main() {
int a, b, gcd, lcm, temp_a, temp_b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp_a = a;
temp_b = b;
while (b != 0) {
gcd = b;
b = a % b;
a = gcd;
}
gcd = a;
lcm = (temp_a * temp_b) / gcd;
printf("GCD is: %d\n", gcd);
printf("LCM is: %d\n", lcm);
return 0;
}
OUTPUT:
EXPERIMENT 23

AIM: C Program to Display Characters from A to Z Using Loop


Theory:
The program includes `stdio.h` for input-output functions. It uses a loop (usually a `for` loop) to
iterate through the ASCII values of characters from 'A' to 'Z'. During each iteration, it prints the
current character using `printf`, resulting in a display of all uppercase letters from A to Z.

CODE:
#include <stdio.h>
int main() {
printf("Characters from A to Z:\n");
for (char ch = 'A'; ch <= 'Z'; ch++) {
printf("%c ", ch);
}
printf("\n");
return 0;
}

OUTPUT:
EXPERIMENT 24

AIM: C Program to Count Number of Digits in an Integer


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter an
integer and counts the number of digits using a loop or by converting the integer to a string.
Finally, it displays the digit count using `printf`.

CODE:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter an integer: ");
scanf("%d", &n);
if (n == 0) {
count = 1;
}
else {
if (n < 0) {
n = -n;
}
while (n > 0) {
n /= 10;
count++;
}
}
printf("Number of digits: %d\n", count);
return 0;
}
OUTPUT:
EXPERIMENT 25

AIM: C Program to Reverse a Number


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter an
integer and reverses the number by extracting its digits using a loop. The digits are stored and
then printed in reverse order using `printf`, resulting in the reversed number being displayed.

CODE:
#include <stdio.h>
int main() {
int n, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
printf("Reversed Number: %d\n", reversed);
return 0;
}

OUTPUT:
EXPERIMENT 26

AIM: C Program to Calculate the Power of a Number


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter a base
and an exponent and calculates the power using a loop or the `pow` function from the math
library. Finally, it displays the result using `printf`, showing the base raised to the specified
exponent.

CODE:
#include <stdio.h>
int main() {
int base, exponent;
long long result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exponent);
for (int i = 0; i < exponent; i++) {
result *= base;
}
printf("%d^%d = %lld\n", base, exponent, result);
return 0;
}

OUTPUT:
EXPERIMENT 27

AIM: C Program to Check Whether a Number is Palindrome or Not


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter an
integer and checks if it is a palindrome by reversing the number and comparing it with the
original. If they are the same, it displays that the number is a palindrome; otherwise, it indicates
that it is not, using `printf`.

CODE:
#include <stdio.h>
int main() {
int n, reversed = 0, original, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed) {
printf("%d is a palindrome.\n", original);
}
else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
OUTPUT:
EXPERIMENT 28

AIM: C Program to Check Whether a Number is Prime or Not


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter an
integer and checks whether the number is prime by testing for divisibility from 2 up to the square
root of the number. If the number is only divisible by 1 and itself, it is classified as prime, and the
result is displayed using `printf`.

CODE:
#include <stdio.h>
int main() {
int n, isPrime = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n <= 1) {
isPrime = 0;
}
else {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
printf("%d is a prime number.\n", n);
}
else {
printf("%d is not a prime number.\n", n);
}
return 0;
}
OUTPUT:
EXPERIMENT 29

AIM: C Program to Display Prime Numbers Between Two Intervals


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter two
integers that define the range. It then iterates through the numbers in that range, checking each
number for primality using a nested loop. The prime numbers found are displayed using `printf`.

CODE:
#include <stdio.h>
int main() {
int start, end, isPrime;
printf("Enter the start and end of the interval: ");
scanf("%d %d", &start, &end);
for (int num = start; num <= end; num++) {
if (num <= 1) {
continue;
}
isPrime = 1;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d ", num);
}
}
printf("\n");
return 0;
}
OUTPUT:
EXPERIMENT 30

AIM: C Program to Check Armstrong Number


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter an
integer and checks if it is an Armstrong number by calculating the sum of its digits raised to the
power of the number of digits. If this sum equals the original number, it displays that the number
is an Armstrong number; otherwise, it indicates that it is not, using `printf`.

CODE:
#include <stdio.h>
#include <math.h>
int main() {
int n, original, remainder, result = 0, digits = 0;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (original != 0) {
original /= 10;
digits++;
}
original = n;
while (original != 0) {
remainder = original % 10;
result += pow(remainder, digits);
original /= 10;
}
if (result == n) {
printf("%d is an Armstrong number.\n", n);
}
else {
printf("%d is not an Armstrong number.\n", n);
}
return 0;
}
OUTPUT:
EXPERIMENT 31

AIM: C Program to Display Armstrong Number Between Two Intervals


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter two
integers that define the range. It then iterates through the numbers in that range and checks
each number to see if it is an Armstrong number by calculating the sum of its digits raised to the
power of the number of digits. If a number is an Armstrong number, it is displayed using `printf`.

CODE:
#include <stdio.h>
#include <math.h>
int main() {
int start, end, num, original, remainder, result, digits;
printf("Enter the start and end of the interval: ");
scanf("%d %d", &start, &end);
printf("Armstrong numbers between %d and %d are: ", start, end);
for (num = start; num <= end; num++) {
original = num;
digits = 0;
result = 0;
while (original != 0) {
original /= 10;
digits++;
}
original = num;
while (original != 0) {
remainder = original % 10;
result += pow(remainder, digits);
original /= 10;
}
if (result == num) {
printf("%d ", num);
}
}
printf("\n");
return 0;
}
OUTPUT:
EXPERIMENT 32

AIM: C Program to Display Factors of a Number


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter an
integer and then iterates through numbers from 1 to that integer, checking for factors by
determining if the integer is divisible by each number. If a number is a factor, it is displayed using
`printf`.

CODE:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factors of %d are: ", n);
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}

OUTPUT:
EXPERIMENT 33

AIM: C Program to Make a Simple Calculator Using switch...case


Theory:
The program includes `stdio.h` for input-output functions. It prompts the user to enter two
numbers and an operator (such as +, -, *, or /). Using a `switch...case` statement, it performs the
corresponding arithmetic operation based on the operator provided. The result of the operation is
then displayed using `printf`.

CODE:
#include <stdio.h>
int main() {
char operator;
float num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch (operator) {
case '+':
result = num1 + num2;
printf("%.2f + %.2f = %.2f\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%.2f * %.2f = %.2f\n", num1, num2, result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("%.2f / %.2f = %.2f\n", num1, num2, result);
} else {
printf("Error: Division by zero.\n");
}
break;
default:
printf("Error: Invalid operator.\n");
}
return 0;
}
OUTPUT:
EXPERIMENT 34

AIM: C Program to Display Prime Numbers Between Intervals Using Function


Theory:
The program includes `stdio.h` for input-output functions and defines a function to check for
prime numbers. It prompts the user to enter two integers that define the range. The function
iterates through the numbers in that range, checks each for primality, and prints the prime
numbers using `printf`. The main function manages user input and calls the prime-checking
function.
CODE:
#include <stdio.h>
void displayPrimes(int start, int end) {
printf("Prime numbers between %d and %d are: ", start, end);
for (int num = start; num <= end; num++) {
int isPrime = 1;
if (num <= 1) isPrime = 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d ", num);
}
}
printf("\n");
}
int main() {
int start, end;
printf("Enter the start and end of the interval: ");
scanf("%d %d", &start, &end);
displayPrimes(start, end);
return 0;
}
OUTPUT:
EXPERIMENT 35

AIM: C Program to Check Prime or Armstrong Number Using User-defined Function


Theory:
The program includes `stdio.h` for input-output functions and defines two user-defined
functions: one for checking if a number is prime and another for checking if it is an Armstrong
number. The user is prompted to enter an integer, and the program calls these functions to
determine the type of number. The results are displayed using `printf`, indicating whether the
number is prime, Armstrong, both, or neither.
CODE:
#include <stdio.h>
#include <math.h>
void checkNumber(int num) {
int isPrime = 1, isArmstrong = 0, sum = 0, originalNum = num,
remainder, digits = 0;
if (num <= 1) isPrime = 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
digits++;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
sum += pow(remainder, digits);
originalNum /= 10;
}
if (sum == num) isArmstrong = 1;
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
if (isArmstrong) {
printf("%d is an Armstrong number.\n", num);
}
else {
printf("%d is not an Armstrong number.\n", num);
}
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
checkNumber(number);
return 0;
}

OUTPUT:
EXPERIMENT 36

AIM: C Program to Check Whether a Number can be Expressed as Sum of Two Prime
Numbers
Theory:
The program includes `stdio.h` for input-output functions and defines a function to check for
prime numbers. It prompts the user to enter an integer and then iterates through all prime
numbers less than or equal to that integer. For each prime number, it checks if the difference
between the entered number and the prime is also prime. If such a pair is found, the program
indicates that the number can be expressed as the sum of two prime numbers; otherwise, it
states that it cannot. The results are displayed using `printf`.
CODE:
#include <stdio.h>
void checkSumOfPrimes(int number) {
int isPrime;
for (int i = 2; i <= number / 2; i++) {
isPrime = 1;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
int other = number - i;
isPrime = 1;
for (int j = 2; j * j <= other; j++) {
if (other % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d can be expressed as the sum of %d and %d.\n", number,
i, other);
return;
}
}
}
printf("%d cannot be expressed as the sum of two prime numbers.\n",
number);
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
checkSumOfPrimes(number);
return 0;
}
OUTPUT:
EXPERIMENT 37

AIM: C Program to Find the Sum of Natural Numbers using Recursion

Theory:
The program includes `stdio.h` for input-output functions and defines a recursive function to
calculate the sum of natural numbers. It prompts the user to enter a positive integer and calls the
recursive function, which adds the current number to the sum of the remaining numbers until it
reaches zero. Finally, it displays the result using `printf`.

CODE:
#include <stdio.h>
int sumOfNaturalNumbers(int n) {
if (n <= 1) return n;
return n + sumOfNaturalNumbers(n - 1);
}
int main() {
int number;
printf("Enter a natural number: ");
scanf("%d", &number);
printf("The sum of natural numbers up to %d is: %d\n", number,
sumOfNaturalNumbers(number));
return 0;
}

OUTPUT:
EXPERIMENT 38

AIM: C Program to Find Factorial of a Number Using Recursion

Theory:
The program includes `stdio.h` for input-output functions and defines a recursive function to
calculate the factorial of a non-negative integer. It prompts the user to enter a number and calls
the recursive function, which multiplies the current number by the factorial of the number minus
one until it reaches the base case of 0! = 1. Finally, it displays the factorial result using `printf`.

CODE:
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("The factorial of %d is: %d\n", number, factorial(number));
return 0;
}

OUTPUT:
EXPERIMENT 39

AIM: C Program to Find G.C.D Using Recursion

Theory:
The program includes `stdio.h` for input-output functions and defines a recursive function to
calculate the greatest common divisor (GCD) using the Euclidean algorithm. It prompts the user
to enter two integers and calls the recursive function, which reduces the problem size by calling
itself with the divisor and the remainder until the remainder is zero. Finally, it displays the GCD
result using `printf`.
CODE:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("The GCD of %d and %d is: %d\n", num1, num2, gcd(num1,
num2));
return 0;
}

OUTPUT:
EXPERIMENT 40

AIM: C Program to Convert Binary Number to Decimal and vice-versa

Theory:
The program includes `stdio.h` for input-output functions and defines functions for converting
binary to decimal and vice-versa. It prompts the user to select a conversion type, takes the input
number, and calls the respective function to perform the conversion. The result is displayed using
`printf`.

CODE:
#include <stdio.h>
#include <math.h>
int binaryToDecimal(int binary) {
int decimal = 0, base = 1;
while (binary > 0) {
int last_digit = binary % 10;
binary /= 10;
decimal += last_digit * base;
base *= 2;
}
return decimal;
}
int decimalToBinary(int decimal) {
int binary = 0, base = 1;
while (decimal > 0) {
int remainder = decimal % 2;
decimal /= 2;
binary += remainder * base;
base *= 10;
}
return binary;
}
int main() {
int choice;
printf("Choose an option:\n1. Binary to Decimal\n2. Decimal to
Binary\n");
scanf("%d", &choice);
if (choice == 1) {
int binary;
printf("Enter a binary number: ");
scanf("%d", &binary);
printf("Decimal equivalent: %d\n", binaryToDecimal(binary));
} else if (choice == 2) {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Binary equivalent: %d\n", decimalToBinary(decimal));
} else {
printf("Invalid choice.\n");
}
return 0;
}
OUTPUT:

You might also like