0% found this document useful (0 votes)
15 views

Clabfile

Uploaded by

Rynosuar gaming
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)
15 views

Clabfile

Uploaded by

Rynosuar gaming
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/ 25

Practical File of

PPS C Language (BCA-101)

Submitted in partial fulfillment of the


requirements of

Bachelor of Computer Application (BCA)


to
Guru Gobind Singh Indraprastha University, Delhi

Supervised by: Submitted by:


(Guide Name) (Student Name)
Designation: Roll No.:-

Banarsidas Chandiwala Institute of Information Technology,


New Delhi – 110019
Batch ( 2024-2028 )
2. Write a program to find the greatest number among 3 numbers given by the user.

PROGRAM:-

#include<stdio.h>

int main(){

int a,b,c;

printf("\nenter 1st number: ");

scanf("%d",&a);

printf("\nenter 2nd number: ");

scanf("%d",&b);

printf("\nenter 3rd number: ");

scanf("%d",&c);

if(a>b){if(a>c){

printf("%d is greater ",a);

}else {

if(b>c){

printf("%D is greater ",b);

}else{

printf("%d is greater ",c);

return 0;

}
OUTPUT:-
4. Write a program to display the following pattern upto N rows, taking the value of N from
the user.

23

456

7 8 9 10

PROGRAM:-

#include<stdio.h>

int main() {

int N, current_number = 1;

printf("Enter the number of rows (N): ");

scanf("%d", &N);

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

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

printf("%d ", current_number);

current_number++;

printf("\n");

return 0;

}
OUTPUT:-
6. Write a program to search for a number entered by the user in a given array and display
the array in ascending order.

PROGRAM:-

#include<stdio.h>

int main() {

int size, target, found = 0;

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

scanf("%d", &size);

int arr[size];

printf("Enter %d elements:\n", size);

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

scanf("%d", &arr[i]);

printf("Enter the number to search for: ");

scanf("%d", &target);

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

if (arr[i] == target) {

printf("Number %d found at index %d.\n", target, i);

found = 1;

break;

if (!found) {

printf("Number %d not found in the array.\n", target);

for (int i = 0; i < size - 1; i++) {


for (int j = i + 1; j < size; j++) {

if (arr[i] > arr[j]) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

printf("Array in ascending order: ");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:-
8. Write a program to add , subtract, multiply and divide two numbers using pointers.

PROGRAM:-

#include <stdio.h>

int main() {

int num1, num2;

int *ptr1, *ptr2;

int addition, subtraction, multiplication;

float division;

printf("Enter two numbers: ");

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

ptr1 = &num1;

ptr2 = &num2;

addition = *ptr1 + *ptr2;

printf("Addition: %d + %d = %d\n", *ptr1, *ptr2, addition);

subtraction = *ptr1 - *ptr2;

printf("Subtraction: %d - %d = %d\n", *ptr1, *ptr2, subtraction);

multiplication = *ptr1 * *ptr2;

printf("Multiplication: %d * %d = %d\n", *ptr1, *ptr2, multiplication);

if (*ptr2 != 0) {

division = (float)(*ptr1) / *ptr2;

printf("Division: %d / %d = %.2f\n", *ptr1, *ptr2, division);

} else {

printf("Error: Division by zero is not allowed.\n");

return 0;}
OUTPUT:-
10. Write a program to create two files with names ‘EvenFile’ and ‘OddFile’. Input 20 numbers
from the user and save even numbers in ‘EvenFile’ and odd numbers in ‘OddFile’.

PROGRAM:-

#include <stdio.h>

int main() {

FILE *evenFile, *oddFile;

int number;

evenFile = fopen("Even File.txt", "w");

oddFile = fopen("Odd File.txt", "w");

if (evenFile == NULL || oddFile == NULL) {

printf("Error opening files.\n");

return 1;

printf("Enter 20 numbers:\n");

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

scanf("%d", &number);

if (number % 2 == 0) {

fprintf(evenFile, "%d\n", number);

} else {

fprintf(oddFile, "%d\n", number);

fclose(evenFile);

fclose(oddFile);

printf("Even numbers saved in 'Even File.txt'\n");

printf("Odd numbers saved in 'Odd File.txt'\n");


return 0;

OUTPUT:-
12. Write a menu driven program to perform the following operations:

(i) Print Armstrong numbers upto N,

(ii) Display prime numbers between 1 to N,

(iii) Reverse of an integer.

PROGRAM:-

#include <stdio.h>

#include <math.h>

int isArmstrong(int num) {

int original = num, sum = 0, digits = 0;

while (num != 0) {

digits++;

num /= 10;

num = original;

while (num != 0) {

int remainder = num % 10;

sum += pow(remainder, digits);

num /= 10;

return sum == original;

int isPrime(int num) {

if (num <= 1) return 0;

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

if (num % i == 0)

return 0;
}

return 1;

int reverseNumber(int num) {

int reversed = 0;

while (num != 0) {

reversed = reversed * 10 + (num % 10);

num /= 10;

return reversed;

int main() {

int choice, N, num;

do {

printf("\nMenu:\n");

printf("1. Print Armstrong numbers up to N\n");

printf("2. Display prime numbers between 1 and N\n");

printf("3. Reverse an integer\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value of N: ");

scanf("%d", &N);

printf("Armstrong numbers up to %d:\n", N);


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

if (isArmstrong(i)) {

printf("%d ", i);

printf("\n");

break;

case 2:

printf("Enter the value of N: ");

scanf("%d", &N);

printf("Prime numbers between 1 and %d:\n", N);

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

if (isPrime(i)) {

printf("%d ", i);

printf("\n");

break;

case 3:

printf("Enter an integer to reverse: ");

scanf("%d", &num);

printf("Reversed number: %d\n", reverseNumber(num));

break;

case 4:

printf("Exiting program.\n");

break;
default:

printf("Invalid choice! Please enter a number between 1 and 4.\n");

} while (choice != 4);

return 0;

OUTPUT:
14. Write a program to calculate factorial of a number and display Fibonacci series upto N
terms using recursive functions.

PROGRAM:-

#include <stdio.h>

long long factorial(int n) {

if (n <= 1)

return 1;

else

return n * factorial(n - 1);

void fibonacci(int n) {

static int a = 0, b = 1;

if (n > 0) {

printf("%d ", a);

a = b;

b = a + b;

fibonacci(n - 1);

int main() {

int num, N;

printf("Enter a number to calculate its factorial: ");

scanf("%d", &num);

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

printf("Enter the number of terms for the Fibonacci series: ");

scanf("%d", &N);
printf("Fibonacci series up to %d terms: ", N);

fibonacci(N);

printf("\n");

return 0;

OUTPUT:-
16. Write a program to make use of arrays with structures in the following ways:

(i) Use array as a structure data member.

(ii) Create array of structure variables.

PROGRAM:-

#include<stdio.h>

struct Student {

char name[50];

int marks[5];

};

struct School {

char name[50];

int rollNumber;

float marks;

};

int main() {

struct Student student1;

printf("\nEnter student's name: ");

scanf("%s", student1.name);

printf("\nEnter marks for 5 subjects:\n");

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

printf("Subject %d: ", i + 1);

scanf("%d", &student1.marks[i]);

printf("\nStudent Name: %s\n", student1.name);

printf("Marks for 5 subjects: ");


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

printf("%d ", student1.marks[i]);

printf("\n");

struct School school[2];

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

printf("\nEnter details for student %d\n", i + 1);

printf("\nEnter student's name: ");

scanf("%s", school[i].name);

printf("\nEnter roll number: ");

scanf("%d", &school[i].rollNumber);

printf("\nEnter marks (float value): ");

scanf("%f", &school[i].marks);

printf("\nDetails of students in the School:\n");

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

printf("Student %d: Name: %s, Roll Number: %d, Marks: %.2f\n",

i + 1, school[i].name, school[i].rollNumber, school[i].marks);

return 0;

}
OUTPUT:-
18. Write a program to perform I/O and make use of file positioning functions on binary files.

PROGRAM:-

#include <stdio.h>

#include <string.h>

struct Employee {

int id;

char name[30];

float salary;

};

int main() {

FILE *file;

struct Employee emp1, emp2, emp3;

file = fopen("employees.dat", "wb+");

if (file == NULL) {

printf("Error opening file for writing.\n");

return 1;

printf("\nEnter details for Employee 1:\n");

printf("Enter ID: ");

scanf("%d", &emp1.id);

printf("Enter Name: ");

scanf(" ");

fgets(emp1.name, sizeof(emp1.name), stdin);

emp1.name[strcspn(emp1.name, "\n")] = '\0';

printf("Enter Salary: ");


scanf("%f", &emp1.salary);

printf("\nEnter details for Employee 2:\n");

printf("Enter ID: ");

scanf("%d", &emp2.id);

printf("Enter Name: ");

scanf(" ");

fgets(emp2.name, sizeof(emp2.name), stdin);

emp2.name[strcspn(emp2.name, "\n")] = '\0';

printf("Enter Salary: ");

scanf("%f", &emp2.salary);

fwrite(&emp1, sizeof(struct Employee), 1, file);

fwrite(&emp2, sizeof(struct Employee), 1, file);

fseek(file, 0, SEEK_SET);

fread(&emp3, sizeof(struct Employee), 1, file);

printf("Employee 1: ID = %d, Name = %s, Salary = %.2f\n", emp3.id, emp3.name, emp3.salary);

fread(&emp3, sizeof(struct Employee), 1, file);

printf("Employee 2: ID = %d, Name = %s, Salary = %.2f\n", emp3.id, emp3.name, emp3.salary);

fclose(file);

return 0;

}
OUTPUT:-
20. Write a program to read time in string format and extract hours, minutes and second also
check time validity.

PROGRAM:-

#include <stdio.h>

#include <stdlib.h>

int main() {

char timeStr[10];

int hours, minutes, seconds;

printf("Enter time in HH:MM:SS format: ");

scanf("%s", timeStr);

if (sscanf(timeStr, "%d:%d:%d", &hours, &minutes, &seconds) != 3) {

printf("Invalid time format.\n");

return 1;

if (hours < 0 || hours > 23) {

printf("Invalid hour. It should be between 00 and 23.\n");

} else if (minutes < 0 || minutes > 59) {

printf("Invalid minutes. It should be between 00 and 59.\n");

} else if (seconds < 0 || seconds > 59) {

printf("Invalid seconds. It should be between 00 and 59.\n");

} else {

printf("Valid time entered: %02d:%02d:%02d\n", hours, minutes, seconds);

return 0;

}
OUTPUT:-

You might also like