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

Computer File

This document contains the lab file submitted by Hemonesh Maheshwari (Roll No. 2K22/A4/36) to Prof. Anurag Goel for the Programming Fundamentals course (CO 102). It includes 15 experiments on basic C programming concepts such as printing output, calculating mathematical operations, determining prime/leap years, searching/sorting arrays, etc. Each experiment lists the aim, code snippet, and output of a program implementing the given task.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Computer File

This document contains the lab file submitted by Hemonesh Maheshwari (Roll No. 2K22/A4/36) to Prof. Anurag Goel for the Programming Fundamentals course (CO 102). It includes 15 experiments on basic C programming concepts such as printing output, calculating mathematical operations, determining prime/leap years, searching/sorting arrays, etc. Each experiment lists the aim, code snippet, and output of a program implementing the given task.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING
PROGRAMMING FUNDAMENTALS

CO 102

LAB FILE

SUBMITTED TO: PROF. ANURAG GOEL

SUBMITTED BY: HEMONESH MAHESHWARI

Roll No.: 2K22/A4/36


INDEX
S.N Objective Date
O Sign
1. Write a C program to print Hello World.
2. Write a C program to calculate the sum, difference, product
and quotient of 2 numbers.
3. Write a C program to determine whether number is prime
or not.
4. Write a C program to reverse a number.
5. Write a C program to find the sum of digits of a number.
6. Write a C program to find factorial of a number.
7. Write a C program to convert temperature from Celsius to
Fahrenheit.
8. Write a C program to determine greatest of 3 numbers.
9. Write a C program to print Fibonacci series till n terms.
10. Write a C program to determine whether a given year is a
leap year.
11. Write a C program to implement Linear Search.
12. Write a C program to implement Binary Search.
13. Write a C program to implement Bubble Sort.
14. Write a C program to implement Selection Sort.
15. Write a C program to implement Insertion Sort.
EXPERIMENT :1

AIM: Write a C program to print Hello World.

CODE:
#include <stdio.h>
int main() {
printf("Hello world");
return 0;
}

OUTPUT:
EXPERIMENT 2

AIM: Write a C program to calculate the sum, difference, product and quotient of 2
numbers.

CODE:
#include <stdio.h>
int main() {
printf("Hello world!\n");
printf("enter number 1\n");
int num1, num2;
scanf("%d", &num1);
printf("enter number 2\n");
scanf("%d" , &num2);
int sum= num1 + num2;
int difference = num1-num2;
int product = num1*num2;
int quotient = num1/num2;
printf("the sum of the numbers is %d\n", sum );
printf("the difference of the numbers is %d\n", difference );
printf("the product of the numbers is %d\n", product );
printf("the quotient of the numbers is %d", quotient);
return 0;
}
OUTPUT:
EXPERIMENT 3

AIM: Write a C program to determine whether number is prime or not.

CODE:
Prime or not
#include <stdio.h>
int main() {
// prime number
printf("enter the number\n");
int num;
scanf("%d", &num);
int ans;
if(num<=1){
printf("number is neither prime nor composite");
return;
}
for(int i=2; i<=num/2; i++){
if(num%i == 0){
ans =1;
break;
}
}
if(ans==1){
printf("number is composite");
}
else{
printf("number is prime");
}
return 0;
}
OUTPUT:
EXPERIMENT 4

AIM: Write a C program to reverse a number.

CODE:
REVERSE A NUMBER

// Online C compiler to run C program online


#include <stdio.h>

int main() {
// Write C code here
int num;
printf("Enter the number to reverse : ");
scanf("%d", &num);
int reverse=0;
int temp = num;
while(num){
reverse *= 10;
reverse += num%10;
num /= 10;
}
printf("reverse of %d is %d", temp, reverse);
}

OUTPUT:
EXPERIMENT 5

AIM: Write a C program to find the sum of digits of a number.

CODE:
SUM OF DIGITS

#include <stdio.h>

int main() {
// Write C code here
int num;
printf("Enter the number : ");
scanf("%d", &num);
int sum=0;
int temp = num;
while(num){
sum += num%10;
num /= 10;
}
printf("sum of digits of %d is %d", temp, sum);
}

OUTPUT:
EXPERIMENT 6

AIM: Write a C program to find factorial of a number.

CODE:
FACTORIAL

#include <stdio.h>

int main() {
// Write C code here
printf("Enter number : ");
int num;
scanf("%d", &num);
if(num<=1){
printf("factorial of %d is 1", num);
return 0;
}
int ans = 1;
for(int i=2; i<=num; i++){
ans *= i;
}
printf("Factorial of %d is %d", num, ans);
return 0;
}

OUTPUT:
EXPERIMENT 7

AIM: Write a C program to convert temperature from celsius to fahrenheit.

CODE:
CELSIUS TO FAHRENHEIT

#include <stdio.h>

int main() {
// Write C code here
printf("Enter temperature in celsius : ");
int temp;
scanf("%d",&temp);
int fahren = 9*temp/5 + 32;
printf("temperature in fahrenheit is : %d °F", fahren);

return 0;
}

OUTPUT:
EXPERIMENT 8

AIM: Write a C program to determine greatest of 3 numbers.

CODE:
// Online C compiler to run C program online
#include <stdio.h>

int main() {
// Write C code here
int num1, num2, num3;
printf("Enter 3 distinct numbers : ");
scanf("%d %d %d", &num1, &num2, &num3);
if(num1>num2 && num1>num3){
printf("%d is the greatest", num1);
}
else{
if(num2>num3){
printf("%d is the greatest", num2);
}
else{
printf("%d is the greatest", num3);
}
}
return 0;
}

OUTPUT:
EXPERIMENT 9

AIM: Write a C program to print Fibonacci series till n terms.

CODE:
FIBONACCI SERIES

// Online C compiler to run C program online


#include <stdio.h>

int main() {
// Write C code here
int n;
printf("Enter the number of terms : ");
scanf("%d", &n);
if(n<=0){
printf("Invalid input (n > 0) ");
return;
}
if(n == 1){
printf("0");
return;
}
if(n == 2){
printf("0 1");
return;
}
int num1=0, num2=1,curr =1;
printf("0 1 ");
for(int i=3; i<=n; i++){
printf("%d ", curr);
num1 = num2;
num2 = curr;
curr = num1 + num2;
}
return 0;
}

OUTPUT:
EXPERIMENT 10

AIM: Write a C program to determine whether a given year is a leap year.

CODE:
// Program to find leap year.

#include <stdio.h>

int main() {
// Write C code here
int year;
printf("Enter year : ");
scanf("%d", &year);
if(year%400 == 0){
printf("%d is a leap year", year);
return;
}
if(year%100 == 0){
printf("%d is not a leap year", year);
return;
}
if(year%4 == 0) printf("%d is a leap year", year);
else printf("%d is not a leap year", year);

return 0;
}
OUTPUT:
EXPERIMENT 11

AIM: Write a C program to implement linear search.

CODE:
LINEAR SEARCH
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int n;
printf("Enter no. of elements in array:");
scanf("%d",&n);
int arr[n];
for(int i=0; i<n; i++){
printf("Enter number %d:",i+1);
scanf("%d", &arr[i]);
}
printf("\nEnter the number to search:");
int num;
scanf("%d" ,&num);
printf("Enter number to search for: ");
int num;
scanf("%d" ,&num);
int arr[5]={5,4,3,2,1};
int ans =0;
int found=0;
for(int i=0; i<5; i++){
if(arr[i]==num){
ans = i;
found= 1;
break;
}
}
if(found == 1){
printf("number found at index %d", ans);
}
else{
printf("number is absent.");
}
return 0;
}

OUTPUT:
EXPERIMENT 12

AIM: Write a C program to implement binary search.

CODE:
BINARY SEARCH
#include <stdio.h>
int binary_search(int arr[], int n, int num){
int low =0, high = n-1;
while(low<high){
int mid = low +(high-low)/2;
if(arr[mid]> num){
high = mid-1;
}
else if(arr[mid]==num){
return mid;
break;
}
else
low = mid +1;
}
return -1;
}
int main() {
int n;
printf("Enter no. of elements in array:");
scanf("%d",&n);
int arr[n];
for(int i=0; i<n; i++){
printf("Enter number %d:",i+1);
scanf("%d", &arr[i]);
}
printf("\nEnter the number to search:");
int num;
scanf("%d", &num);
int ans = binary_search(arr,5, num);
if(ans == -1)
printf("%d is absent.", num);
else
printf("%d is present at index %d.", num, ans);
return 0;
}

OUTPUT:
EXPERIMENT 13

AIM: Write a C program to implement Bubble Sort.

CODE:
BUBBLE SORT
#include <stdio.h>
void swap(int *x, int *y){
int temp= *x;
*x = *y;
*y = temp;
}
int main() {
int n;
printf("Enter no. of elements in array:");
scanf("%d",&n);
int arr[n];
for(int i=0; i<n; i++){
printf("Enter number %d:",i+1);
scanf("%d", &arr[i]);
}
for(int i=0; i<n-1; i++){
for(int j=0; j<n-1-i; j++){
if(arr[j]>arr[j+1]){
swap(&arr[j], &arr[j+1]);
}
}
}
for(int i=0; i<5; i+=1){
printf("%d ", arr[i]);
}

return 0;
}

OUTPUT:
EXPERIMENT 14

AIM: Write a C program to implement Selection Sort

CODE:
SELECTION SORT
#include <stdio.h>
void print(int *arr, int n){
for(int i=0; i<n; i++){
printf("%d ", arr[i]);
}
}
int main() {
int n;
printf("Enter no. of elements in array:");
scanf("%d",&n);
int arr[n];
for(int i=0; i<n; i++){
printf("Enter number %d:",i+1);
scanf("%d", &arr[i]);
}
for(int i=0; i<n; i++){
int min = i;
for(int j = i+1; j<n; j++){
if(arr[j]<arr[min]) min = j;
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
print(arr,n);
return 0;
}
OUTPUT:
EXPERIMENT 15

AIM: Write a C program to implement Insertion Sort

CODE:
#include <stdio.h>
void print(int *arr, int n){
for(int i=0; i<n; i++){
printf("%d ", arr[i]);
}
}
int main() {
int n;
printf("Enter no. of elements in array:");
scanf("%d",&n);
int arr[n];
for(int i=0; i<n; i++){
printf("Enter number %d:",i+1);
scanf("%d", &arr[i]);
}
for(int i=0; i<n; i++){
int temp = arr[i];
int j= i-1;
while(j>=0 && arr[j]>temp){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
print(arr,n);
return 0;
}

OUTPUT:

You might also like