0% found this document useful (0 votes)
27 views26 pages

PPA Lab

The document contains a series of C programming lab exercises, each demonstrating fundamental programming concepts such as input/output, arithmetic operations, control structures, and functions. Each lab includes a code snippet, a brief explanation of the code, and the logic behind the implementation. Topics covered range from printing messages and performing calculations to checking properties of numbers like even/odd, prime, and perfect numbers.

Uploaded by

sekhonshubham03
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)
27 views26 pages

PPA Lab

The document contains a series of C programming lab exercises, each demonstrating fundamental programming concepts such as input/output, arithmetic operations, control structures, and functions. Each lab includes a code snippet, a brief explanation of the code, and the logic behind the implementation. Topics covered range from printing messages and performing calculations to checking properties of numbers like even/odd, prime, and perfect numbers.

Uploaded by

sekhonshubham03
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/ 26

BCA PPA Lab Practicals

Lab-1: WAP to Print "Hello World"


// Program to print "Hello World"
#include <stdio.h>
int main() {
printf("Hello World\n");
return 0;
}

Explanation:

● #include <stdio.h>: Includes the standard input-output library.


● printf("Hello World\n");: Prints "Hello World" followed by a newline.
● return 0;: Indicates successful execution.

Lab-2: Display Messages Using \n, \t, \a


// Program to display messages using escape sequence characters
#include <stdio.h>
int main() {
printf("This is a new line\n");
printf("This is a tab space:\tHello\n");
printf("This is an alert sound\a\n"); // This will trigger a beep
sound
return 0;
}

Explanation:

● \n: Adds a new line.


● \t: Inserts a tab space.
● \a: Triggers an alert/beep sound.
Lab-3: WAP to Add Two Numbers
// Program to add two numbers
#include <stdio.h>
int main() {
int num1, num2, sum;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2); // Takes input for two integers

sum = num1 + num2; // Calculates the sum


printf("The sum is: %d\n", sum); // Prints the result

return 0;
}

Explanation:

● Input: Numbers entered by the user are stored in num1 and num2.
● Processing: sum = num1 + num2 calculates the sum.
● Output: Displays the result using printf.

Lab-4: WAP to Swap Two Values Without Using a Third Variable


// Program to swap two values without using a third variable
#include <stdio.h>
int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b); // Inputs two numbers

// Swapping using arithmetic operations


a = a + b;
b = a - b;
a = a - b;

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}

Explanation:

● Logic:
1. Add a and b and store in a (a = a + b).
2. Subtract the new a with b to get the original a in b.
3. Subtract the new b from a to get the original b in a.

Lab-5: WAP to Print the Sum of a Four-Digit Number


// Program to calculate the sum of digits of a four-digit number
#include <stdio.h>
int main() {
int num, sum = 0, digit;

printf("Enter a four-digit number: ");


scanf("%d", &num);

while (num > 0) {


digit = num % 10; // Extract the last digit
sum += digit; // Add the digit to the sum
num /= 10; // Remove the last digit
}

printf("The sum of the digits is: %d\n", sum);

return 0;
}

Explanation:

● Logic:
○ Extract the last digit using num % 10.
○ Add the digit to sum.
○ Remove the last digit by dividing num by 10.
○ Repeat until num becomes 0.

Lab-6: WAP to Check Whether the Given Number is Even or Odd

// Program to check if a number is even or odd

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0)

printf("%d is even.\n", num);

else

printf("%d is odd.\n", num);

return 0;

Explanation:
● num % 2 == 0: Checks if the number is divisible by 2 (even). If true, it prints "even";
otherwise, it prints "odd".

Lab-7: WAP to Check Whether the Given Year is Leap Year or Not

// Program to check whether a year is leap year or not

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))

printf("%d is a leap year.\n", year);

else

printf("%d is not a leap year.\n", year);

return 0;

Explanation:

● A year is a leap year if it is divisible by 400, or if divisible by 4 but not divisible by 100.
Lab-8: WAP to Find Out the Greatest Number Among 3 Numbers

// Program to find the greatest of three numbers

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

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

if (a >= b && a >= c)

printf("%d is the greatest number.\n", a);

else if (b >= a && b >= c)

printf("%d is the greatest number.\n", b);

else

printf("%d is the greatest number.\n", c);

return 0;

}
Explanation:

● The program compares the three numbers to find the greatest using conditional
if-else statements.

Lab-9: WAP to Check Whether the Given Character is a Vowel or Not Using
Switch Case

// Program to check whether a character is a vowel or not using


switch-case

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

switch(ch) {

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

case 'A':

case 'E':

case 'I':
case 'O':

case 'U':

printf("%c is a vowel.\n", ch);

break;

default:

printf("%c is not a vowel.\n", ch);

return 0;

Explanation:

● The program uses a switch statement to check if the entered character is a vowel (both
lowercase and uppercase).

Lab-10: WAP to Calculate the Factorial of a Given Number

// Program to calculate the factorial of a number

#include <stdio.h>

int main() {

int num, fact = 1;

printf("Enter a number: ");

scanf("%d", &num);
for (int i = 1; i <= num; i++) {

fact *= i; // Multiply fact with each number from 1 to num

printf("Factorial of %d is: %d\n", num, fact);

return 0;

Explanation:

● The program uses a loop to multiply all integers from 1 to num to compute the factorial.

Lab-11: WAP to Print the Sum of Given n Numbers

// Program to calculate the sum of n numbers

#include <stdio.h>

int main() {

int n, sum = 0, num;

printf("Enter the number of elements: ");

scanf("%d", &n);

for (int i = 1; i <= n; i++) {


printf("Enter number %d: ", i);

scanf("%d", &num);

sum += num;

printf("The sum of the numbers is: %d\n", sum);

return 0;

Explanation:

● The program prompts the user for n numbers and adds them together using a for loop.

Lab-12: WAP to Print the Reverse of a Given Number

// Program to print the reverse of a given number

#include <stdio.h>

int main() {

int num, reversed = 0;

printf("Enter a number: ");

scanf("%d", &num);

while (num != 0) {
reversed = reversed * 10 + num % 10; // Get the last digit and
build the reverse

num /= 10; // Remove the last digit

printf("The reverse of the number is: %d\n", reversed);

return 0;

Explanation:

● This program reverses the number by extracting its digits and building the reversed
number.

Lab-13: WAP to Print the Table of Any Given Number

// Program to print the table of any given number

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Multiplication Table of %d:\n", num);


for (int i = 1; i <= 10; i++) {

printf("%d x %d = %d\n", num, i, num * i);

return 0;

Explanation:

● This program prints the multiplication table of a given number from 1 to 10.

Lab-14: WAP to Check Whether the Given Number is Prime or Not

// Program to check whether the number is prime or not

#include <stdio.h>

int main() {

int num, is_prime = 1;

printf("Enter a number: ");

scanf("%d", &num);

if (num <= 1) {

printf("%d is not a prime number.\n", num);

return 0;

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

if (num % i == 0) {

is_prime = 0; // Number is divisible by i, so it's not


prime

break;

if (is_prime)

printf("%d is a prime number.\n", num);

else

printf("%d is not a prime number.\n", num);

return 0;

Explanation:

● This program checks if a number is prime by dividing it by all numbers from 2 to num/2.

Lab-15: WAP to Check Whether the Given Number is Armstrong or Not

// Program to check if the number is Armstrong or not

#include <stdio.h>
#include <math.h>

int main() {

int num, temp, sum = 0, digit, count = 0;

printf("Enter a number: ");

scanf("%d", &num);

temp = num;

while (temp != 0) {

temp /= 10;

count++;

temp = num;

while (temp != 0) {

digit = temp % 10;

sum += pow(digit, count); // Sum of the digits raised to the


power of the number of digits

temp /= 10;

if (sum == num)

printf("%d is an Armstrong number.\n", num);


else

printf("%d is not an Armstrong number.\n", num);

return 0;

Explanation:

● An Armstrong number is a number that is equal to the sum of its own digits raised to the
power of the number of digits.

Lab-16: WAP to Print the Fibonacci Series Up to a Given Range

// Program to print the Fibonacci series up to a given range

#include <stdio.h>

int main() {

int n, first = 0, second = 1, next;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

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


if (i <= 1)

next = i;

else {

next = first + second;

first = second;

second = next;

printf("%d ", next);

return 0;

Explanation:

● The program calculates the Fibonacci series, where each term is the sum of the two
preceding ones. The first two terms are 0 and 1, and then the program prints the next
terms based on the sum.

Lab-17: WAP to Print the Sine and Cosine Series

// Program to print the sine and cosine series for a given angle

#include <stdio.h>

#include <math.h>

int main() {

double angle, sine_value, cosine_value;


printf("Enter an angle in degrees: ");

scanf("%lf", &angle);

// Converting angle to radians

angle = angle * (3.14159 / 180);

sine_value = sin(angle);

cosine_value = cos(angle);

printf("Sine value: %lf\n", sine_value);

printf("Cosine value: %lf\n", cosine_value);

return 0;

Explanation:

● This program calculates the sine and cosine values for a given angle in degrees by
converting it to radians first using the formula (degrees * π) / 180.

Lab-18: WAP to Calculate the nCr (Combination)

// Program to calculate the combination nCr

#include <stdio.h>
int factorial(int num) {

int fact = 1;

for (int i = 1; i <= num; i++) {

fact *= i;

return fact;

int main() {

int n, r;

printf("Enter values of n and r: ");

scanf("%d %d", &n, &r);

int nCr = factorial(n) / (factorial(r) * factorial(n - r));

printf("The value of %dC%d is: %d\n", n, r, nCr);

return 0;

Explanation:
● The program calculates the combination nCr using the formula nCr = n! / (r! *
(n - r)!), where n! is the factorial of n.

Lab-19: WAP to Print the Pascal Triangle

// Program to print Pascal's triangle

#include <stdio.h>

int factorial(int num) {

int fact = 1;

for (int i = 1; i <= num; i++) {

fact *= i;

return fact;

int main() {

int rows;

printf("Enter the number of rows for Pascal's Triangle: ");

scanf("%d", &rows);

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

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


printf("%d ", factorial(i) / (factorial(j) * factorial(i -
j)));

printf("\n");

return 0;

Explanation:

● The program prints Pascal’s triangle using the combination formula nCr for each
element in the triangle. It loops through rows and columns to generate the values.

Lab-20: WAP to Check Whether the Given Number is Perfect or Not

// Program to check if a number is perfect or not

#include <stdio.h>

int main() {

int num, sum = 0;

printf("Enter a number: ");

scanf("%d", &num);

for (int i = 1; i < num; i++) {

if (num % i == 0)
sum += i; // Sum of divisors

if (sum == num)

printf("%d is a perfect number.\n", num);

else

printf("%d is not a perfect number.\n", num);

return 0;

Explanation:

● A perfect number is a number that is equal to the sum of its proper divisors (excluding
the number itself). This program checks for this property.

Lab-21: WAP to Calculate the GCD of Given Two Numbers

// Program to calculate the GCD of two numbers

#include <stdio.h>

int gcd(int a, int b) {

if (b == 0)

return a;

return gcd(b, a % b); // Recursive call

}
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;

Explanation:

● The program uses the Euclidean algorithm to calculate the GCD (Greatest Common
Divisor) of two numbers recursively.

Lab-22: WAP to Count the Digits in a Given Number

// Program to count the digits in a given number

#include <stdio.h>

int main() {

int num, count = 0;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {

num /= 10; // Remove the last digit

count++;

printf("The number of digits is: %d\n", count);

return 0;

Explanation:

● This program uses a while loop to repeatedly divide the number by 10 until it becomes
zero, counting how many digits the number has.

Lab-23: WAP to Calculate the Factorial of a Given Number Using Recursion

// Recursive function to calculate the factorial

#include <stdio.h>

int factorial(int num) {

if (num == 0 || num == 1)

return 1;

return num * factorial(num - 1);


}

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial of %d is: %d\n", num, factorial(num));

return 0;

Explanation:

● The program uses recursion to calculate the factorial. The base case is when num is 0 or
1, which returns 1.

Lab-24: WAP to Demonstrate the Call by Value

// Function to demonstrate call by value

#include <stdio.h>

void add(int a, int b) {

int sum = a + b;

printf("Sum (inside function) = %d\n", sum);


}

int main() {

int x = 5, y = 3;

printf("Sum (before function) = %d\n", x + y);

add(x, y); // Passing values to function

return 0;

Explanation:

● In Call by Value, the actual values of x and y are passed to the function add. The
function performs the addition, but the values in the main function remain unchanged.

Lab-25: WAP to Demonstrate the Call by Reference

// Function to demonstrate call by reference

#include <stdio.h>

void add(int *a, int *b) {

int sum = *a + *b;

printf("Sum (inside function) = %d\n", sum);

}
int main() {

int x = 5, y = 3;

printf("Sum (before function) = %d\n", x + y);

add(&x, &y); // Passing addresses to function

return 0;

Explanation:

● In Call by Reference, the addresses of x and y are passed to the function. The function
can modify the actual values of x and y.

You might also like