CSELAB
CSELAB
CSELAB
ENGINEERING
PROGRAMMING FUNDAMENTALS
CO 101
LAB FILE
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
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
CODE:
#include <stdio.h>
int main(){
int a,b,sum;
sum=a+b;
return 0;
OUTPUT:
EXPERIMENT 4
CODE:
#include <stdio.h>
int main() {
float a, b, product;
product = a * b;
return 0;
OUTPUT:
EXPERIMENT 5
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
int main() {
int divisor, dividend, quotient, remainder;
printf("Enter divisor and dividend: ");
scanf("%d %d", ÷nd, &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
OUTPUT:
EXPERIMENT 7
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
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
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
CODE:
#include <stdio.h>
int main() {
int a, b, temp;
temp = a;
a = b;
b = temp;
printf("After swapping:\n");
return 0;
}
OUTPUT:
EXPERIMENT 11
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
CODE:
#include <stdio.h>
int main() {
} else {
return 0;
}
OUTPUT:
EXPERIMENT 13
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:
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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: