Problem solving and Programming using c
COURSE CODE: BCA101
st
1 semester (Academic Year 2024-25)
SUBMITTED BY SUBMITTED TO
STUDENT NAME: CHINTU SINGH Faculty: Ms. Romi Tiwari
Admission No.: 2401035 Designation: SOEIT
COURSE: BCA Department: School of
SECTION: A Engineering information And
Technology
INDEX Particular of The Experiment’s Performed
Sr. Name of Experiment Page Date of Signatur
No. No. Experiment e
1 Write a simple program of 4
hello world.
2 Write a program that takes 4-5
user input and then displays
output based on that input.
3 Draw a flowchart of while 5
loop and do while loop
using Raptor and Drakon
Tool.
4 Write a single program for 6-7
addition, subtraction,
multiplication and division.
5 Write a program to check 7
whether the no. is odd or
even.
6 Write a simple program of 8-9
calculator by using switch
statement.
7 Write a program to 9-10
Calculate the sum of the
first 100 natural numbers.
8 Print a pascal triangle patter 10-11
of start.
9 Write a program to calculate 11-12
a factorial of the number.
10 Write a recursive function to
find the greatest common 12-13
divisor (GCD) of two
numbers.
11 Write a program to calculate 13-14
the sum of elements in a 1D
array.
12 Write a program to reverse a 14-15
string.
13 Implement these sorting 15-16
algorithms on a 1D array.
14 Implement a binary search 16-17
function on a sorted array.
15 Write a program to swap 17-18
two variables using pointers.
1. Write a simple program of hello world.
INPUT:
#include <stdio.h>
int main()
{
// Print Hello, World! to the console
printf("Hello World!\n");
return 0;
}
Output:
Hello World
2. Write a program that takes user input and then displays output based on that
input.
INPUT:
#include <stdio.h>
int main()
{
char name[50];
int age;
// Ask for user's name
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
// Read the name (including spaces)
// Ask for user's age
printf("Enter your age: ");
scanf("%d", &age);
// Display the output based on user input
printf("\nHello, %sYou are %d years old.\n", name,
age);
return 0;
}
Output:
Enter your name: John Doe
Enter your age: 25
Hello, John Doe
You are 25 years old.
3.Draw a flowchart of while loop and do while loop using Raptor and Drakon Tool.
4. Write a single program for addition, subtraction, multiplication and division.
INPUT:
#include <stdio.h>
int main()
{ double num1, num2;
char operator;
// Ask the user to enter the two numbers and the operator
printf("Enter first number: "); scanf("%lf", &num1);
printf("Enter second number: "); scanf("%lf", &num2);
// Ask for the operation printf("Enter
operator (+, -, *, /): ");
scanf(" %c", &operator); // Note the space before %c to avoid newline character
issues
// Perform the appropriate operation based on the user's input
switch(operator) { case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
break ;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
break; case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break;
case '/':
// Check if the user is trying to divide by zero
if (num2 != 0) {
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
} else {
printf("Error! Division by zero is not allowed.\n");
} break;
default:
printf("Invalid operator! Please use +, -, *, or /.\n");
}
return 0;
}
Output:
Enter first number: 10
Enter second number: 5
Enter operator (+, -, *, /): +
10.00 + 5.00 = 15.00
5. Write a program to check whether the no. is odd or even.
INPUT:
#include <stdio.h> int
main() { int number;
// Ask the user to enter a number
printf("Enter a number: "); scanf("%d",
&number);
// Check if the number is even or odd if
(number % 2 == 0) { printf("%d is an even
number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
return 0;
}
Output:
Enter a number: 8
8 is an even number.
6. Write a simple program of calculator by using switch statement.
INPUT:
#include <stdio.h>
int main() { double num1,
num2;
char operator;
// Ask user for input printf("Enter
first number: "); scanf("%lf", &num1);
printf("Enter second number: "); scanf("%lf",
&num2);
// Ask user for the operator printf("Enter operator (+, -, *, /): "); scanf(" %c",
&operator); // The space before %c is to consume any leftover newline character
// Perform the operation based on the operator switch
(operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 +
num2);
break; case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 -
num2);
break; case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break; case '/':
if (num2 != 0) { printf("%.2lf / %.2lf = %.2lf\n",
num1, num2, num1 / num2);
} else {
printf("Error! Division by zero is not allowed.\n");
}
break; default: printf("Invalid operator!
Please use +, -, *, or /.\n"); break;
}
return 0;
}
Output:
Enter first number: 12
Enter second number: 8
Enter operator (+, -, *, /): +
12.00 + 8.00 = 20.00
7. Write a program to Calculate the sum of the first 100 natural numbers.
INPUT:
#include <stdio.h>
int main() { int sum =
0;
int i;
// Loop to sum the first 100 natural numbers
for (i = 1; i <= 100; i++) { sum += i; //
Add each number to the sum
}
// Print the result
printf("The sum of the first 100 natural numbers is: %d\n", sum);
return 0;
}
Output:
The sum of the first 100 natural numbers is: 5050
8. Print a pascal triangle patter of start.
INPUT:
. #include <stdio.h> int main()
{ int rows, i, j, space;
// Ask user for the number of rows
printf("Enter the number of rows for Pascal's Triangle: ");
scanf("%d", &rows);
// Loop for each row of the Pascal's Triangle
for(i = 0; i < rows; i++) { // Print leading
spaces for(space = 0; space < rows - i - 1;
space++) {
printf(" ");
}
// Print stars for each row
for(j = 0; j <= i; j++) { if
(j == 0 || j == i) {
// Print a star at the start or end of the row
printf("* ");
} else {
// Print a space in between the stars
printf(" ");
}
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
Output:
*
* *
* *
* *
* *
9. a program to calculate a factorial of the number.
INPUT:
#include <stdio.h>
int main() { int num; long long factorial = 1; // Use long long to
store large factorial results
// Ask user for input printf("Enter a
number: "); scanf("%d", &num);
// Check if the number is negative if (num < 0) {
printf("Error! Factorial of a negative number doesn't exist.\n");
} else {
// Calculate factorial using a for loop for (int i = 1;
i <= num; i++) { factorial *= i; // Multiply factorial
by current number
}
printf("Factorial of %d = %lld\n", num, factorial);
}
return 0;
}
Output:
Enter a number: 5
Factorial of 5 = 120
10. Write a recursive function to find the greatest common divisor (GCD) of two
numbers.
INPUT:
#include <stdio.h>
// Recursive function to calculate GCD
int gcd(int a, int b) {
// Base case: if b is 0, GCD is a
if (b == 0) {
return a;
}
// Recursive case: call gcd with swapped and reduced arguments else
{
return gcd(b, a % b);
}
}
int main() { int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1); printf("Enter
second number: "); scanf("%d",
&num2);
printf("GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));
return 0;
}
Output:
Enter first number: 5
Enter second number: 8
GCD of 5 and 8 is: 1
11. Write a program to calculate the sum of elements in a 1D array.
Input:
#include <stdio.h>
// Function to calculate sum of array elements
int calculateSum(int arr[], int size) {
int sum = 0; for (int i = 0; i <
size; i++) { sum += arr[i];
}
return sum;
}
int main() { int n;
printf("Enter the number of elements: "); scanf("%d",
&n);
int arr[n];
printf("Enter %d elements:\n", n); for (int i
= 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = calculateSum(arr, n); printf("Sum of
array elements: %d\n", sum);
return 0;
}
Output:
Enter the number of elements: 4 Enter 4 elements:
5
6
4
3
Sum of array elements: 18
12. Write a program to reverse a string.
Input:
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) { int
length = strlen(str); char temp[length
+ 1];
int i;
// Copy original string to temp array in reverse order
for (i = 0; i < length; i++) {
temp[i] = str[length - i - 1];
}
temp[length] = '\0'; // Null-terminate the string
// Copy reversed string back to original array
strcpy(str, temp);
int main() { char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str); printf("Reversed
string: %s\n", str);
return 0;
}
Output:
Enter a string: 567
Reversed string: 765
13. Implement these sorting algorithms on a 1D array.
Input:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp; for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) { if
(arr[j] > arr[j + 1]) { temp =
arr[j]; arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {5, 2, 8, 3, 1, 6, 4}; int n =
sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n);
printf("Sorted array: "); for (int i = 0; i < n;
i++) {
printf("%d ", arr[i]);
}
return 0;
}
Optput:
Sorted array: 1 2 3 4 5 6 8
14. Implement a binary search function on a sorted array.
Input:
#include <stdio.h>
// Binary Search Function
int binarySearch(int arr[], int target, int low, int high) { while (low <=
high) {
int mid = low + (high - low) / 2;
// Check if target is at mid index if
(arr[mid] == target) { return mid;
}
// If target is less than mid element, search left half
else if (arr[mid] > target) { high =
mid - 1;
}
// If target is greater than mid element, search right half else {
low = mid + 1;
}
}
// Return -1 if target is not found
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}; int n =
sizeof(arr) / sizeof(arr[0]);
int target; printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Enter target element: ");
scanf("%d", &target);
int result = binarySearch(arr, target, 0, n - 1);
if (result == -1) {
printf("Target element not found in the array.\n");
} else {
printf("Target element found at index %d.\n", result);
}
return 0;
}
OUTPUT:
Sorted Array: 2 5 8 12 16 23 38 56 72 91 Enter target
element: 23
Target element found at index 5.
15. Write a program to swap two variables using pointers.
INPUT:
#include <stdio.h>
void swap(int* a, int* b) { int temp
= *a; *a = *b;
*b = temp;
}
int main() { int x = 10;
int y = 20;
printf("Before swapping:\n");
scanf("x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping:\n");
printf("x = %d, y = %d\n", x, y);
return 0;
}
OUTPUT:
Before swapping:
x = 10, y = 20 After swapping:
x = 20, y = 10