Co 1st Semester Lab File
Co 1st Semester Lab File
Co 1st Semester Lab File
ENGINEERING
PROGRAMMING FUNDAMENTALS
CO 101
LAB FILE
SUBMITTED TO: SUBMITTEDBY:
EXPERIMENT-01
AIM: Write a C program to Print hello world using Printf and hello and world should be in different
lines?
It uses the printf function to display "hello" and "World" on separate lines.
Finally, the program returns 0, indicating successful execution.
#include<stdio.h>
int main()
{
printf("hello \n World");
return 0;
OUTPUT:
EXPERIMENT-02
AIM: Write a program in C to take two numbers from the user and give output as the sum of two
numbers?
OUTPUT:
EXPERIMENT-03
AIM: Write a program to calculate simple interest if principle is p, rate is r and time is t ?
1. The program calculates the simple interest (SI) based on the principle amount (p), rate (r), and time (t).
Algorithm:
OUTPUT:
EXPERIMENT-04
AIM: Write a program to find all factors of a number taken from the user?
to iterate from 1 to 'n' and checks if each number is a factor of 'n'. If it is, it is printed as an output.
Algorithm:
Enter a while loop that continues as long as 'i' is less than or equal to 'n'.
Within the loop, check if 'i' is a factor of 'n' by using the condition 'n % i == 0'.
Increment 'i' by 1.
AIM: Write a code to check whether the entered number is prime or not?
or not. It uses a while loop to iterate from 1 to 'n' and counts the number of factors of 'n'. If 'n' has exactly two
Algorithm:
5. Enter a while loop that continues as long as 'i' is less than or equal to 'n'.
6. Within the loop, check if 'i' is a factor of 'n' by using the condition 'n % i == 0'.
8. Increment 'i' by 1.
OUTPUT:
EXPERIMENT-06
AIM: Write a program to find all factors of a number taken from the user?
Theory and ALGORITHM: Theory: The given program generates and prints the Fibonacci
series up to a given number 'n'. It starts with the initial values of 'a' and 'b' as 0 and 1, respectively. It
then uses a while loop to calculate the Fibonacci sequence by adding the previous two numbers and
updating the values of 'a' and 'b'. The loop continues until it reaches 'n + 1' terms, and each number in
Algorithm:
6. Enter a while loop that continues as long as 'i' is less than or equal to 'n + 1'.
8. Calculate the next Fibonacci number by adding 'a' and 'b' and store it in a variable 'sum'.
9. Update 'a' with the value of 'b' and 'b' with the value of 'sum'.
11. Repeat steps 7-10 until the while loop condition is met.
user. It uses the Euclidean algorithm, which repeatedly divides the larger number by the remainder
obtained from the division of the two numbers until the remainder becomes zero. The final non-zero
Algorithm:
2. Prompt the user to enter two numbers, the dividend, and the divisor.
4. Enter a while loop that continues indefinitely (while loop with condition 1, which is always true).
5. Within the loop, calculate the remainder of the division of 'dividend' by 'divisor' and store it in the
variable 'rem'.
10. Print the value of 'divisor', which represents the HCF of the two numbers.
OUTPUT:
EXPERIMENT-08
three numbers using if statements and assigns the greatest value to a variable called 'greatest'. Finally, it displays
Algorithm:
3. Read and store the input values in variables: 'num1', 'num2', and 'num3'.
#include <stdio.h>
int main() {
int num1, num2, num3;
return 0;
}
OUTPUT:
EXPERIMENT-09
user and checks if the number is divisible by 2. If the remainder of the division is zero, it is an even
Algorithm:
4. Check if the number 'num' is divisible by 2 using the condition 'num % 2 == 0'.
int main() {
int num;
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
OUTPUT:
EXPERIMENT-10
int main() {
int n;
return 0;
}
OUTPUT:
EXPERIMENT-11
enter a value for 'n', and then it uses a loop to iterate from 1 to 'n' and calculate the sum of the numbers.
Algorithm:
6. Within the loop, add the value of the loop variable to 'sum'.
7. Repeat steps 5-6 until the loop variable reaches the value of 'n'.
8. Display the value of 'sum' as the sum of the first 'n' natural numbers.
int main() {
int n, sum = 0;
return 0;
}
OUTPUT:
EXPERIMENT-12
number, and then it uses a loop to iterate from 1 to the given number and calculate the factorial.
Algorithm:
6. Within the loop, multiply the value of the loop variable with the 'factorial'.
7. Repeat steps 5-6 until the loop variable reaches the value of 'n'.
8. Display the value of 'factorial' as the factorial of the given number 'n'.
int main() {
int n;
unsigned long long factorial = 1;
return 0;
}
OUTPUT:
EXPERIMENT-13(A)
loop to extract each digit from the number and print it.
Algorithm:
5. Within the loop, calculate the remainder of 'n' when divided by 10 using 'rem = n % 10'.
6. Print the value of 'rem', which represents the last digit of the number.
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
while (n != 0)
{
int rem = n % 10;
printf("%d", rem);
n=n/10;
return 0;
}
OUTPUT:
EXPERIMENT-13(B)
extract each digit from the number, build the reversed number, and finally, prints the reversed number.
Algorithm:
6. Within the loop, calculate the remainder of 'n' when divided by 10 using 'rem = n % 10'.
10. Print the value of 'ans', which represents the reversed number.
n=n/10;
}
printf("%d",ans);
return 0;
}
OUTPUT:
EXPERIMENT-14
Algorithm:
6. Use a 'for' loop to iterate from 0 to 'n-1' and read each element into 'arr[i]'.
8. Use another 'for' loop to iterate from 0 to 'n-1' and print each element of the array.
int n, i;
printf("enter the number of elements in an array=");
scanf("%d", &n);
int arr[n];
printf("enter your array elements=\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("your array elements are \n");
for (int i = 0; i<n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
OUTPUT:
EXPERIMENT-15
Theory: The given program allows the user to input the number of elements in an array and the values for each
element. It then prints the array elements in reverse order.
Algorithm:
1. Start the program.
2. Prompt the user to enter the number of elements in the array.
3. Read and store the input value in the variable 'n'.
4. Declare an integer array 'arr' of size 'n' to store the elements.
5. Prompt the user to enter the array elements.
6. Use a 'for' loop to iterate from 0 to 'n-1' and read each element into 'arr[i]'.
7. Display a message to indicate that the program will print the array elements in reverse order.
8. Use another 'for' loop to iterate from 'n-1' to 0 in reverse order.
9. Within the loop, print each element of the array.
10. Repeat step 9 until all array elements are printed in reverse order.
11.End the program.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
}
OUTPUT:
EXPERIMENT-16
AIM: Write a program to find the maximum out of all array elements?
for each element. It then finds the maximum value among all the elements in the array and displays it.
Algorithm:
6. Use a 'for' loop to iterate from 0 to 'n-1' and read each element into 'arr[i]'.
7. Initialize a variable 'max' with the first element of the array ('arr[0]').
9. Within the loop, compare each element of the array with the current maximum value 'max'.
10. If an element is greater than 'max', update 'max' to the new maximum value.
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
OUTPUT:
EXPERIMENT-17
element. It then reverses the indexes of the array elements and displays them.
Algorithm:
6. Use a 'for' loop to iterate from 0 to 'n-1' and read each element into 'arr[i]'.
7. Declare another integer array 'reversedArr' of size 'n' to store the reversed elements.
9. Assign each element of 'arr' to 'reversedArr' at the reversed index position ('reversedArr[n - 1 - i]
= arr[i]').
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int reversedArr[n];
for (int i = 0; i < n; i++) {
reversedArr[n - 1 - i] = arr[i];
}
return 0;
}
OUTPUT:
EXPERIMENT-18
Linear Search: Linear search is a simple search algorithm that sequentially checks each element in a
list/array until a match is found or the end of the list is reached. It is suitable for unordered lists.
Algorithm:
5. If the end of the list is reached without a match, return -1 to indicate that the key was not found.
Binary Search: Binary search is an efficient search algorithm that works on sorted lists/arrays. It
repeatedly divides the search space in half by comparing the middle element with the search key until
Algorithm:
2. Set the lower bound (left) to the first index and the upper bound (right) to the last index.
3. Repeat the following steps until the search space is empty: a. Calculate the middle index as (left
+ right) / 2. b. Compare the middle element with the search key: - If the middle element is equal
to the key, return the middle index. - If the middle element is greater than the key, set the new
upper bound to middle - 1. - If the middle element is smaller than the key, set the new lower
bound to middle + 1.
4. If the search space is empty and the key is not found, return -1 to indicate that the key was not
found.
CODE: 1.linear search This program is written by on
}
#include <stdio.h>
int main() {
int n, key;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
OUTPUT:
CODE: 2.Binary search This program is written by on
#include <stdio.h>
if (arr[mid] == key) {
else {
int main() {
int n, key;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
}
printf("Enter the key to search: ");
scanf("%d", &key);
if (index != -1) {
} else {
return 0;
OUTPUT:
EXPERIMENT-19
Bubble Sort: Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. It continues to iterate through the list
until no more swaps are needed, indicating that the list is sorted.
Algorithm:
4. Continue iterating through the list, repeating steps 2 and 3, until no more swaps are needed.
5. The largest element will "bubble" to the end of the list after each iteration.
6. Repeat the above steps for the remaining unsorted portion of the list until the entire list is sorted.
Insertion Sort: Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a
time. It iterates through the list, considering one element at a time, and inserts it into its correct position
Algorithm:
1. Start with the second element (index 1) and assume it is the first element of the sorted portion.
3. Compare the current element with the elements in the sorted portion from right to left.
4. Shift the elements in the sorted portion that are greater than the current element to the right.
5. Insert the current element into its correct position in the sorted portion.
6. Repeat steps 2 to 5 until all elements are considered and inserted into the sorted portion.
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
}
OUTPUT:
CODE: 2.Insertion sort This program is written by on
#include <stdio.h>
int arr[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
OUTPUT: