File Format
File Format
CO 101
LAB FILE
SUBMITTED SUBMITED
BY:
MRIDUL KR. SINGH
2K22/B6/67
INDEX
S. No. Objective Date Sign
1. Write a C program to find sum and average of TWO
numbers ?
2. WRITE A PROGRAM TO FIND THE LARGEST
NUMBER ?
3. WRITE A PROGRAM TO FIND SIMPLE
INTEREST ?
4. WAP TO PRINT TRIANGLE OF STARS ?
5. WAP TO FIND WHETHER A NUMBER
ENTERED IS PRIME OR NOT ?
6. WAP TO FIND SUM OF A 5 DIGIT NUMBER?
7. WAP TO REVERSE A 5 DIGIT NUMBER ?
8. WAP TO CONVERT DECIMAL TO BINARY ?
9. WAP TO GENERATE FIBONACCI SEQUENCE ?
10. WAP TO FIND EXPONENTIAL FUNCTION ?
11. WAP TO FIND A NO. IN AN ARRAY USING
LINER SEARCH ?
12. WAP TO FIND A NUMBER IN ARRAY USING
BINARY SEARCH ?
EXPERIMENT 1
EXPERIMENT 2
AIM:WRITE A PROGRAM TO FIND THE LARGEST NUMBER ?
CODE:
#include <stdio.h>
int main() {
int a, b;
printf("Please Enter Two different values\n");
scanf("%d %d", &a, &b);
if(a > b)
{
printf("%d is Largest\n", a);
}
else if (b > a)
{
printf("%d is Largest\n", b);
}
else
{
printf("Both are Equal\n");
}
return 0;
}
OUTPUT:
Please Enter Two Different Values
50
60
60 is Largest
EXPERIMENT 3
AIM: WRITE A PROGRAM TO FIND SIMPLE INTEREST ?
CODE:
# include <conio.h>
# include <stdio.h>
# include <stdlib.h>
int main()
{
int principal, rate, time, interest;
printf("Enter the principal: ");
scanf("%d", &principal);
printf("Enter the rate: ");
scanf("%d", &rate);
printf("Enter the time: ");
scanf("%d", &time);
interest = principal * rate * time / 100;
printf("The Simple interest is %d", interest);
return 0;
}
OUTPUT:
Enter the principal : 1000
Enter the rate : 2
Enter the time : 23
The Simple interst is :460
EXPERIMENT 4
AIM: WAP TO PRINT TRIANGLE OF STARS ?
CODE:
#include<stdio.h>
int main() {
int i,j,rows;
printf("Enter the number of rows\n");
scanf("%d", &rows);
for(i = 0; i < rows; i++) {
/* Prints one row of triangle */
for(j = 0; j <= i; ++j) {
printf("* ");
}
/* move to next row */
printf("\n");
}
return 0;
}
OUTPUT :
Enter the numbe of rows 5
EXPERIMENT 5
AIM:WAP TO FIND WHETHER A NUMBER ENTERED IS PRIME
OR NOT ?
CODE:
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
//logic
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}
OUTPUT:
Enter any number 5
5 is a Prime Number
EXPERIMENT 6
AIM: WAP TO FIND SUM OF A 5 DIGIT NUMBER?
CODE:
#include <stdio.h>
int main(){
int num = 58612;
int sum = 0;
while(num != 0){
sum += num % 10;
num = num/10;
}
printf("Digit sum: %d", sum);
}
OUTPUT:
Digit sum: 22
EXPERIMENT 7
AIM: WAP TO REVERSE A 5 DIGIT NUMBER ?
CODE:
#include <stdio.h>
int main() {
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
OUTPUT:
Enter an integer: 54654
Reversed number = 45645
EXPERIMENT 8
AIM: WAP TO CONVERT DECIMAL TO BINARY ?
CODE:
#include <stdio.h>
#include <math.h>
long long convert(int);
int main() {
int n, bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}
long long convert(int n) {
long long bin = 0;
int rem, i = 1;
while (n!=0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
OUTPUT:
Enter a decimal number:13
13 in decimal =1101 in binary
EXPERIMENT 9
AIM: WAP TO GENERATE FIBONACCI SEQUENCE ?
CODE:
#include <stdio.h>
void main()
{
int fib1 = 0, fib2 = 1, fib3, limit, count = 0;
printf("Enter the limit to generate the Fibonacci Series \n");
scanf("%d", &limit);
printf("Fibonacci Series is ...\n");
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2;
while (count < limit)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}
OUTPUT:
Enter the limit to generate the Fibonacci Series
6
Fibonacci Series is ...
0
1
1
2
3
5
EXPERIMENT 10
AIM: WAP TO FIND EXPONENTIAL FUNCTION ?
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum=1, t=1;
clrscr();
printf("Enter the value for x : ");
scanf("%f", &x);
printf("\nEnter the value for n : ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
t=t*x/i;
sum=sum+t;
}
printf("\nThe Exponential Value of %f = %.4f", x, sum);
getch();
}
OUTPUT:
Enter the value for x: 3
Enter the value of n: 10
The exponential value of 3.0000 = 20.097
EXPERIMENT 11
AIM: WAP TO FIND A NO. IN AN ARRAY USING LINER SEARCH
?
CODE:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
OUTPUT:
Enter number of elements in array
5
Enter 5 number
5
6
4
2
9
Enter number to search
4
4 is present at location 3
EXPERIMENT 12
AIM: WAP TO FIND A NUMBER IN ARRAY USING BINARY
SEARCH ?
CODE:
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
OUTPUT:
Enter number of elemnts
5
Enter 5 integers
1
2
3
4
5
Enter value to find
4
4 found at location 4