0% found this document useful (0 votes)
187 views135 pages

Co Lab File Last One

This document contains 12 programming problems and their solutions in C language. Problem 1 contains a program to find the sum and average of two numbers. Problem 2 finds the greatest of 10 numbers input in an array. Problem 3 calculates simple interest given principal, rate and time. Problems 4A-4E contain programs to print various patterns using loops. The remaining problems check if a number is prime, even/odd, palindrome, reverse digits, find greatest of 3 numbers, check leap year and convert between binary and decimal numbers. All programs are written by Samridhi Kapoor with roll number 2K20/B11/12.

Uploaded by

isha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views135 pages

Co Lab File Last One

This document contains 12 programming problems and their solutions in C language. Problem 1 contains a program to find the sum and average of two numbers. Problem 2 finds the greatest of 10 numbers input in an array. Problem 3 calculates simple interest given principal, rate and time. Problems 4A-4E contain programs to print various patterns using loops. The remaining problems check if a number is prime, even/odd, palindrome, reverse digits, find greatest of 3 numbers, check leap year and convert between binary and decimal numbers. All programs are written by Samridhi Kapoor with roll number 2K20/B11/12.

Uploaded by

isha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 135

CO 101

Programming Fundamentals
Lab File

INSTRUCTOR: MS. GULL KAUR


(ASST. PROFESSOR)
DONE BY:
ROLL NO. : 2K20/B11/12
NAME: SAMRIDHI KAPOOR
Problem 1: Program to find sum and average
of two numbers.
ALGORITHM

Step 1: Start
Step 2: Input number1, number2
Step 3: Calculate sum = (number1 + number2)
Step 4: avg = sum / 2
Step 5: Print sum and avg of two numbers
Step 6: Stop
FLOWCHART
PROGRAM
#include<stdio.h>
int main()
{
int num1, num2;
int sum=0;
float avg=0.0;
printf(";Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
sum= num1+num2;
avg=(float)(num1+num2)/2;
printf("Sum of %d and %d is: %d \t",num1,num2,sum);
printf("Average of %d and %d is: %.2f\n",num1,num2,avg);
printf("This output belongs to 2K20/B11/12 SAMRIDHI KAPOOR");
return 0;
}
OUTPUT
PROBLEM 2:
PROGRAM TO FIND GREATEST OF 10
NUMBERS

ALGORITHM
Step 1: Start
Step 2: Input array a[10] of 10 numbers
Step 3: max=a [0]
Step 4: for each i item in a [10]
if a[i]&gt;max
max=a[i]
Else
Continue to next item
Step 6: print max
Step 7: Stop
FLOWCHART
PROGRAM
#include <stdio.h>
int main() {
int a[10];
int i;
int greatest;
printf("Enter ten values:");
//Store 10 numbers in an array
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
}
}
printf("Greatest of ten numbers is %d", greatest);
printf("This output belongs to 2K20/B11/12 SAMRIDHI KAPOOR");
return 0;}
OUTPUT
Problem 3: PROGRAM TO FIN SIMPLE
INTEREST

ALGORITHM
Step 1: Start
Step 2: Input Principal Amount, Rate and Time
Step 3: Calculate Interest using formula SI= ((amount*rate*time)/100)
Step 4: Print Simple Interest
Step 5: Stop
FLOWCHART
PROGRAM
#include<stdio.h>
int main(){
int amt,time,rate;
float SI;
printf("Enter principle amount: ");
scanf("%d",&amt);
printf("Enter rate of interest: ");
scanf("%d",&rate);
printf("Enter time period: ");
scanf("%d",&time);
SI=(float)(amt*rate*time)/100;
printf("The simple interest is: %f",SI);
return 0;
}
OUTPUT
PROBLEM 4A: PROGRAM TO PRINT THE
FOLLOWING PATTERN

*****

*****

*****

*****

*****

PROGRAM

#include<stdio.h>
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("*");
}
printf("\n");
}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 4B: PROGRAM TO PRINT THE
FOLLOWING PATTERN
*

**

***

****

*****

PROGRAM

#include <stdio.h>
int main()
{
int i,j;
for (i=0; i<5; i++)
{
for (j=0; j<=i; j++)
{
printf("*");
}
printf("\n");
}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}
OUTPUT
PROBLEM 4C: PROGRAM TO PRINT THE
FOLLWING PATTERN
*

**

***

****

*****

PROGRAM

#include <stdio.h>

int main()
{
int i, j;

for(i=1; i<=5; i++)


{
for(j=i; j<5; j++)
{
printf(" ");
}

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


{
printf("*");
}

printf("\n");
}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 4D: PROGRAM TO PRINT THE
FOLLOWING PATTERN

**

***

****

*****

PROGRAM

#include<stdio.h>
#include<conio.h>
int main()
{
int s, i, j;

for(i = 1; i <= 5; i++)


{

for(s = i; s < 5; s++)


printf(" ");

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


printf("* ");

printf("\n");
}
return 0;
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
}

OUTPUT
PROBLEM 4E: PROGRAM TO PRINT THE
FOLLOWING PATTERN

***

*****

*******

PROGRAM

#include <stdio.h>
int main() {
int i, j, k = 0;

for (i = 1; i <= 5; ++i, k = 0) {


for (j = 1; j <= 5 - i; ++j) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 5: PROGRAM TO FIND WHETHER
THE ENTERED NUMBER IS PRIME

PROGRAM
#include<stdio.h>
#include<math.h>
int main()
{
int n, i, p = 1;

printf("Enter a number");
scanf("%d", &n);
for (i = 2; i<= sqrt(n); i++) {

printf("%d\t",i);
if (n%i == 0) {
p = 0;
break;
}
}

if(n<=1)
p = 0;
else if (n==2)
p=1;
if (p == 1) {
printf("%d is a prime number\n", n);
}
else {
printf("%d is not a prime number\n", n);

}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 6: PROGRAM TO FIND WHETHER
THE ENTERED NUMBER IS EVEN OR ODD

PROGRAM
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
printf(“This output belongs to SAMRIDHI KAPOOR 2K20/B11/12”);
return 0;
}

OUTPUT
PROBLEM 7: PROGRAM TO REVERSE THE
DIGITS OF A NUMBER

PROGRAM:
#include <stdio.h>
int main()
{
int num, reverse, remainder;
printf("Enter an number: ");
scanf("%d", &num);
while(num != 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("The reverse of the given number is %d ", reverse);
printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 8: TO FIND GREATEST AND
SMALLEST OF 3 NUMBERS

PROGRAM
#include <stdio.h>
int main()
{
int num1, num2, num3, largest, smallest;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Enter third number: ");
scanf("%d", &num3);
largest = (num1>num2)?((num1>num3)?num1:num3):
((num2>num3)?num2:num3);
smallest = (num1<num2)?((num1<num3)?num1:num3):
((num2<num3)?num2:num3);
printf("Largest number is %d", largest);
printf("\nSmallest number is %d", smallest);

printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");


return 0;
}

OUTPUT

PROBLEM 9: IF THE GIVEN NUMBER


(INTEGER) IS A PALINDROME OR NOT AND IF
IT IS A PALINDROME THEN FIND IT IS AN
EVEN LENGTH OR ODD LENGTH
PALINDROME

PROGRAM:
#include <stdio.h>
int main()
{
int n, reverse = 0,remainder, num, count;
printf("Enter the number: ");
scanf("%d", &n);
num = n;
while(n!=0){
remainder=n%10;
reverse = reverse * 10 + remainder;
n = n/10;
}
printf("The reverse number is: %d", reverse);

if(reverse == num){
printf("\nIt is a palindrome number");}

else
printf("\nIt is not a palindrome number");
while(num != 0)
{
num = num/10;
count = count + 1;

}
printf("\nThe number of digits in the palindrome is %d", count);
if(count % 2 == 0){
printf("\nIt is an even length palindrome");
}
else printf("\nIt is an odd length palindrome");

printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");


return 0;
}

OUTPUT
PROBLEM 10: TO FIND WHETHER THE YEAR
IS LEAP YEAR OR NOT

PROGRAM
#include <stdio.h>
int main()
{
int year;
printf("Enter the year: ");
scanf("%d", &year);

if(year%4 == 0){
if(year%100 == 0){
if ( year%400 == 0)
printf("\nThe given year is a leap year");
else
printf("\nThe given year is not a leap year");
}
else
printf("\nThe given year is a leap year");}
else
printf("\nThe given year is not a leap year");
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 11(A): PROGRAM FOR THE
CONVERSIO OF BINARY NUMBER TO
DECIMAL NUMBER
PROGRAM
#include<stdio.h>
#include<math.h>
int main()
{
int a[32],sum=0,i=-1;

printf("Enter the binary number from left to right");


printf("(Enter any other character to stop)\n");

do
{
i++;
scanf("%d",&a[i]);
}while(a[i]==1||a[i]==0);

printf("your binary number is: ");

int j=0;
while(j<i)
{
printf("%d ",a[j]);
sum=sum+pow(2,i-j-1)*a[j];
j++;
}
printf("\nyour decimal number is: %d",sum);
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 11(B): PROGRAM FOR THE
CONVERSION OF DECIMAL NUMBER TO
BINARY NUMBER

PROGRAM:

#include<stdio.h>
int main()
{
int a[10],n,i;

printf("Enter the decimal number to convert: ");


scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("Binary of Given Number is : ");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 11(C): PROGRAM FOR THE
CONVERSION OF BINARY NUMERS TO
HEXADECIMAL NUMBERS

PROGRAM

#include<stdio.h>
#include<conio.h>
int main()
{
int bin_num, hex=0, mul=1, count=1, rem, i=0;
char hex_num[20];
printf("Enter any Binary Number: ");
scanf("%d", &bin_num);
while(bin_num!=0)
{
rem = bin_num%10;
hex = hex + (rem*mul);
if(count%4==0)
{
if(hex<10)
hex_num[i] = hex+48;
else
hex_num[i] = hex+55;
mul = 1;
hex = 0;
count = 1;
i++;
}
else
{
mul = mul*2;
count++;
}
bin_num = bin_num/10;
}
if(count!=1)
hex_num[i] = hex+48;
if(count==1)
i--;
printf("\nEquivalent Hexadecimal Value = ");
for(i=i; i>=0; i--)
printf("%c", hex_num[i]);
getch();
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 11(D): PROGRAM FOT THE
CONVERSION OF HEXADECIMAL NUMBER TO
BINARY NUMBER

PROGRAM

#include<stdio.h>
int main()
{
int i=0;
char hexdec[11];
printf("Enter Hexadecimal Number: ");
gets(hexdec);
printf("\nEquivalent Binary Value = ");
while(hexdec[i])
{
switch(hexdec[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
printf("1010");
break;
case 'a':
printf("1010");
break;
case 'B':
printf("1011");
break;
case 'b':
printf("1011");
break;
case 'C':
printf("1100");
break;
case 'c':
printf("1100");
break;
case 'D':
printf("1101");
break;
case 'd':
printf("1101");
break;
case 'E':
printf("1110");
break;
case 'e':
printf("1110");
break;
case 'F':
printf("1111");
break;
case 'f':
printf("1111");
break;
default:
printf("--Invalid Hex Digit (%c)--", hexdec[i]);
}
i++;
}
getch();
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}X
OUTPUT
PROBLEM 11(E): PROGRAM FOR THE
CONVERSION OF BINARY NUMBERS TO
OCTAL NUMBERS

PROGRAM

#include<stdio.h>
#include<conio.h>
int main()
{
int bin, oct=0, i=0, mul=1, count=1, rem, octnum[20];
printf("Enter any Binary Number: ");
scanf("%d", &bin);
while(bin!=0)
{
rem = bin%10;
oct = oct + (rem*mul);
if(count%3==0)
{
octnum[i] = oct;
mul = 1;
oct = 0;
count = 1;
i++;
}
else
{
mul = mul*2;
count++;
}
bin = bin/10;
}
if(count!=1)
octnum[i] = oct;
printf("\nEquivalent Octal Value = ");
for(i=i; i>=0; i--)
printf("%d", octnum[i]);
getch();
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}
OUTPUT
PROBLEM 11(F): PROGRAM FOR THE
CONVERSION OF OCTAL NUMBER TO
BINARY NUMBER

PROGRAM

#include<stdio.h>
#include<conio.h>
int main()
{
int octnum, rev=0, rem;
printf("Enter any Octal Number: ");
scanf("%d", &octnum);
while(octnum!=0)
{
rem = octnum%10;
rev = (rev*10) + rem;
octnum = octnum/10;
}
octnum = rev;
printf("\nEquivalent Binary value = ");
while(octnum!=0)
{
rem = octnum%10;
switch(rem)
{
case 0: printf("000");
break;
case 1: printf("001");
break;
case 2: printf("010");
break;
case 3: printf("011");
break;
case 4: printf("100");
break;
case 5: printf("101");
break;
case 6: printf("110");
break;
case 7: printf("111");
break;
default: printf(" InvalidOctalDigit(%d) ", rem);
break;
}
octnum = octnum/10;
}
getch();
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 11(G): PROGRAM FOR
CONVERSION OF SIGNED BINARY TO
DECIMAL

PROGRAM

#include<stdio.h>
#include<math.h>
int main()
{
int a[32],sum=0;
printf("Enter the binary number\n");
int i=-1;
do
{
i++;
scanf("%d",&a[i]);
} while (a[i]==1||a[i]==0);
i--;
printf("you binary number is:");
int j=0;
while(j<=i)
{
printf("%d",a[j]);
j++;
}
if(a[0]==1)
{
while(i>=0)
{
if(a[i]==1)
break;
i--;
}
i--;
while(i>=0)
{
if(a[i]==0)
a[i]=1;
else
a[i]=0;
i--;
}

for(i=0;i<j;i++)
{
sum+=a[i]*pow(2,j-i-1);
}
sum*=-1;
printf("\nthe decimal conversion is:%d",sum);
}
else
{
j=0;
while(j<=i)
{
sum+=a[j]*pow(2,i-j);
j++;
}
printf("\nthe decimal conversion is: %d",sum);
}
printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}
OUTPUT
PROBLEM 11(H): PROGRAM FOR
CONVERSION OF SIGNED DECIMAL TO
BINARY

PROGRAM

#include<stdio.h>
int main()
{
int a[32],n;
printf("Enter the decimal number\n");
scanf("%d",&n);
printf("Your decimal number is:%d",n);
if(n>=0)
{
int i=-1;
while(n>0)
{
i++;
int rem=n%2;
a[i]=rem;
n=n/2;
}
printf("\nThe binary conversion is:0");
while(i>=0)
{
printf("%d",a[i]);
i--;
}
}
else
{
n*=-1;
int i=-1;
while(n>0)
{
i++;
int rem=n%2;
a[i]=rem;
n=n/2;
}
int j=i;
i=0;
while(i<=j)
{
if(a[i]==1)
break;
i++;
}
while(i<=j)
{
i++;
if(a[i]==1)
a[i]=0;
else
a[i]=1;
}
printf("\nThe binary conversion is:1");
while(j>=0)
{
printf("%d",a[j]);
j--;
}
}
return 0;
printf("This output belongs to SAMRIDHI KAPOOR");
}
OUTPUT
PROBLEM 12: PROGRAM TO DESIGN A
CALCULATOR TO PERFORM ADDITION,
SUBTRACTION, MULTIPLICATION, DIVISION,
POWER

PROGRAM
#include<stdio.h>
#include<stdlib.h>

void display(float n1, float n2, char ch, float result);


void add(float n1, float n2);
void subtract(float n1, float n2);
void multiply(float n1, float n2);
void divide(float n1, float n2);
void rem(float n1, float n2);
void power(float n1, float n2);

int main()
{
float n1, n2;
int ch;
do{
printf("Enter two numbers: ");
scanf("%f %f", &n1, &n2);

printf("\n1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Division");
printf("\n5.Remainder");
printf("\n6.Power (x^y)");
printf("\n7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
add(n1,n2);
break;
case 2:
subtract(n1,n2);
break;
case 3:
multiply(n1,n2);
break;
case 4:
divide(n1,n2);
break;
case 5:
rem(n1,n2);
break;
case 6:
power(n1,n2);
break;
case 7:
printf("Thank You.");
exit(0);
default:
printf("Invalid input.");

}while(1);

return 0;
}

void display(float n1, float n2, char ch, float result)


{
printf("%.2f %c %.2f = %.2f\n", n1, ch, n2, result);
}

void add(float n1, float n2)


{
float result = n1 + n2;
display(n1, n2, '+', result);
}

void subtract(float n1, float n2)


{
float result = n1 - n2;
display(n1, n2, '-', result);
}

void multiply(float n1, float n2)


{
float result = n1 * n2;
display(n1, n2, '*', result);
}

void divide(float n1, float n2)


{
float result = n1 / n2;
display(n1, n2, '/', result);
}

void rem(float n1, float n2)


{
int num1 = n1;
int num2 = n2;
int result = num1%num2;
printf("%d %% %d = %d\n", num1, num2, result);
}

void power(float n1, float n2)


{
if(n2<0) printf("Second number should be +ve.");
else
{
float result=1.0;
for(int i=1; i<=n2; i++)
{
result *= n1;
}
display(n1, n2, '^', result);
}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 13: CALENDAR PROGRAM TO
DISPLAY THE DAY TO THE INPUT OF
CORRESPONDING DATE MONTH AND YEAR

PROGRAM
#include<stdio.h>
#include<iostream>

using namespace std;


bool leapyear(int y)
{
if(y%100==0)
{
if(y%400==0)
return true;

else
return false;

}
else
{
if(y%4==0)
return true;
else
return false;
}

}
int main()
{
int i;
int d,m,y;
printf("Enter date: ");
scanf(" %d",&d);
printf("Enter month: ");
scanf(" %d", &m);
printf("Enter year: ");
scanf(" %d", &y);
if(d==1 && m==1 && y==1900)
printf("Monday \n");
int nofdays=0;

for(i=1900;i<y;i++)
{
int t=leapyear(i);
if(t==1)
nofdays=nofdays+366;
else
nofdays=nofdays+365;

}
int u=leapyear(y);

for(i=1;i<m;i++)
{

switch(i)
{
case 1:
nofdays=nofdays+31;
break;
case 2:
if(u==1)
nofdays=nofdays+29;
else
nofdays=nofdays+28;
break;
case 3:
nofdays=nofdays+31;
break;
case 4:
nofdays=nofdays+30;
break;
case 5:
nofdays=nofdays+31;
break;
case 6:
nofdays=nofdays+30;
break;
case 7:
nofdays=nofdays+31;
break;
case 8:
nofdays=nofdays+31;
break;
case 9:
nofdays=nofdays+30;
break;
case 10:
nofdays=nofdays+31;
break;
case 11:
nofdays=nofdays+30;
break;
case 12:
nofdays=nofdays+31;
break;
}

}
for(i=1;i<d;i++)
{
nofdays=nofdays+1;

int rem=nofdays%7;
switch(rem)
{
case 0:
printf("Monday \n");
break;
case 1:
printf("Tuesday \n");
break;
case 2:
printf("Wednesday \n");
break;
case 3:
printf("Thursday \n");
break;
case 4:
printf("Friday \n");
break;
case 5:
printf("Saturday \n");
break;
case 6:
printf("Sunday \n");
break;

}
printf("\n This output belongs to SAMRIDHI KAPOOR 2K20/B11/12 ");
return 0;
}
OUTPUT
PROBLEM 14: LINEAR SEARCH PROGRAM

PROGRAM
#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);
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 15: BINARY SEARCH PROGRAM

PROGRAM
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");

return 0;
}
OUTPUT
PROBLEM 16: TO FIND THE FACTORIAL OF A
NUMBER USING RECURSION

PROGRAM
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OUTPUT
PROBLEM 17: TO FIND THE NTH TERM OF A
FIBONACCI SERIES USING RECURSION

PROGRAM
#include<stdio.h>
int Fibonacci(int n)
{
if (n <= 1)
return n;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int main()
{
int n;
printf("Enter the n value: ");
scanf("%d", &n);
printf("%d", Fibonacci(n - 1));
printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}
OUTPUT
PROBLEM 18: PROGRAM TO SHOW THE
ADDITION OF TWO 3X3 MATRICES

PROGRAM
#include <stdio.h>
int main()
{
int a[3][3], b[3][3], add[3][3];
int i, j;
printf("Enter the values of the first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &a[i][j]);
}}
printf("Enter the values of the second matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &b[i][j]);
}}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{add[i][j]=a[i][j] + b[i][j];
}}
printf("The addition of two matrices is:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{printf("%d", add[i][j]);
printf("\n");
}}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}
OUTPUT
PROBLEM 19: PROGRAM TO SHOW
MULTIPLICATION OF TWO 3X3 MATRICES

PROGRAM
#include <stdio.h>

int main()
{
int a[3][3], b[3][3], c[3][3];
int i, j, k;

printf(" Enter elements of first matrix\n ");


for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
scanf(" %d ", &a[i] [j] );
}
}
printf(" Enter elements of second matrix\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
scanf(" %d ", &b[i][j]);
}
}

for( i = 0; i < 3; i++ )


{
for( j = 0; j < 3; j++ )
{
c[i][j] = 0;
for( k = 0; k < 3; k++ )
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
printf(" \n\n Multiplication of two matrices is\n ");
for( i = 0; i < 3; i++ )
{
for(j = 0; j < 3; j++ )
printf(" %d ", c[i][j]);
printf(" \n ");
}
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}
OUTPUT
PROBLEM 20: PROGRAM TO SWAP TWO
NUMBERS USING POINTERS
PROGRAM
#include <stdio.h>

int main()
{
int x, y, *a, *b, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;
b = &y;

temp = *b;
*b = *a;
*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);


return 0;
}

OUTPUT
PROBLEM 21: PROGRAM TO SORT AN ARRAY
USING BUBBLE SORT

PROGRAM
#include <stdio.h>
int main()
{
int n, j, i, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
int array[n];
printf("Enter %d integers\n", n);
for (i= 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i- 1; j++)
{
if (array[j] > array[j+1])
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)


printf("%d\n", array[i]);

printf("\nThis output belongs to SAMRIDHI KAPOOR


2K20/B11/12");
return 0;
}
OUTPUT

PROBLEM 22: PROGRAM TO SORT AN ARRAY


USING INSERTION SORT
PROGRAM
#include<stdio.h>
int main(){

int i, j, noe, temp, num[100];

printf("Enter the number of elements to sort: ");


scanf("%d",&noe);

printf("Enter %d elements: ", noe);


for(i=0;i<noe;i++)
scanf("%d",&num[i]);

for(i=1;i<noe;i++){
temp=num[i];
j=i-1;
while((temp<num[j])&&(j>=0)){
num[j+1]=num[j];
j=j-1;
}
num[j+1]=temp;
}

printf("Order of Sorted elements: ");


for(i=0;i<noe;i++)
printf(" %d",num[i]);
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");

return 0;
}

OUTPUT
PROBLEM 23: PROGRAM TO SORT AN ARRAY
USING SELECTION SORT
PROGRAM
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);


for (c = 0; c < n; c++)
scanf("%d", &array[c]);

for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times


{
position = c;

for (d = c + 1; d < n; d++)


{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");

return 0;
}

OUTPUT

PROBLEM 24: TO FIND THE LENGTH OF THE


STRING WITHOUT USING STRLEN FUNCTION
PROGRAM
#include<stdio.h>

int FindLength(char str[]);


int main() {
char str[100];
int length;

printf("\nEnter the String : ");


gets(str);

length = FindLength(str);

printf("\nLength of the String is : %d", length);


printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return(0);
}

int FindLength(char str[]) {


int len = 0;
while (str[len] != '\0')
len++;
return (len);

OUTPUT
PROBLEM 25: PROGRAM TO FIND THE
NUMBER OF VOWELS IN A STRING
PROGRAM
#include <stdio.h>
int tot_vowel(char str[]);
int main(){
char str[100];
int len, i, vowel = 0;
printf("Enter the string: ");
gets(str);
vowel = tot_vowel(str);
printf("\nTotal number of vowels in the string is: %d", vowel);
printf("\nThis putput belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}
int tot_vowel(char str[]){
int vowel = 0;
int len, i;
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')
vowel=vowel+1;
}
return (vowel);
}

OUTPUT
PROBLEM 26: TO CHECK IF A GIVEN STRING
IS A PALINDROME OR NOT
PROGRAM:
#include <stdio.h>
void ispalindrome(char *str)
{
char *revstr, *startstr;
int stringlength = strlen(str);
int i, flag = 0;
startstr = str;
for(i = 0; i < stringlength - 1; i++)
str++;
revstr = str;
while(startstr < revstr)
{
if(*startstr != *revstr)
flag = 1;
startstr++;
revstr--;
}
if(flag == 0)
printf("\nEntered string is a palindrome");
else
printf("\nEntered string is not a palindrome");
}

int main(int argc, char **argv)


{
char str[20];
printf("Enter a string: ");
gets(str);
printf("\n");
ispalindrome(str);
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
getch();
return 0;
}

OUTPUT
PROBLEM 27: PROGRAM TO COMPARE TWO
STRINGS
PROGRAM:
#include <stdio.h>
#include <string.h>

int main()
{
char Str1[100], Str2[100];
int result, i;

printf("\n Please Enter the First String : ");


gets(Str1);

printf("\n Please Enter the Second String : ");


gets(Str2);

for(i = 0; Str1[i] == Str2[i] && Str1[i] == '\0'; i++);

if(Str1[i] < Str2[i])


{
printf("\n str1 is Less than str2");
}
else if(Str1[i] > Str2[i])
{
printf("\n str2 is Less than str1");
}
else
{
printf("\n str1 is Equal to str2");
}
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 28: PROGRAM TO CONCATENATE
TWO STRINGS
PROGRAM
#include <stdio.h>

int main()

char str1[50], str2[50], i, j;

printf("\nEnter first string: ");

scanf("%s",str1);

printf("\nEnter second string: ");

scanf("%s",str2);

for(i=0; str1[i]!='\0'; ++i);

for(j=0; str2[j]!='\0'; ++j, ++i)

str1[i]=str2[j];

str1[i]='\0';

printf("\nOutput: %s",str1);

printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");

return 0;

}
OUTPUT
PROBLEM 29: PROGRAM TO REVERSE A
STRING
PROGRAM
#include <stdio.h>
int main()
{
char s[1000], r[1000];
int begin, end, count = 0;

printf("Input a string\n");
gets(s);

// Calculating string length

while (s[count] != '\0')


count++;

end = count - 1;

for (begin = 0; begin < count; begin++) {


r[begin] = s[end];
end--;
}
r[begin] = '\0';

printf("%s\n", r);
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");

return 0;
}

OUTPUT
PROBLEM 30: PROGRAM TO FIND THE AREA
AND PERIMETER OF CIRCLE, TRIANGLE,
RECTANGLE AND SQUARE USING FUNCTIONS
PROGRAM
#include<stdio.h>
#define PI 3.14
int rect_area(int num1,int num2)
{
int a_rect;
a_rect=num1*num2;
return a_rect;
}
int rect_perimeter(int num1,int num2)
{
int p_rect;
p_rect=2*(num1+num2);
return p_rect;
}
int squ_area(int num3)
{
int a_squ;
a_squ=num3*num3;
return a_squ;
}
int squ_perimeter(int num3)
{
int p_squ;
p_squ=4*(num3);
return p_squ;
}
float cir_area(int num5)
{
float a_cir;
a_cir=PI*(num5*num5);
return a_cir;
}
float cir_perimeter(int num5)
{
float p_cir;
p_cir=2*PI*num5;
return p_cir;
}
int tri_area(int num6,int num7)
{
int a_tri;
a_tri=0.5*num6*num7;
return a_tri;
}
int tri_perimeter(int num8,int num9,int num10)
{
int p_tri;
p_tri=num8+num9+num10;
return p_tri;
}

int main()
{
int a, b, h, len, breadth, num, area, rad, perimeter, side1, side2,
side3, height;
float circumference, area_c;
printf("1 rectangle");
printf("\n2 square");
printf("\n3 circle");
printf("\n4 triangle");
printf("\nEnter the figure number you want to calculate area and
perimeter of: ");
scanf("%d",&num);
switch(num)
{
case 1:
printf("Enter the length of rect: ");
scanf("%d",&len);
printf("Enter the breadth of rect: ");
scanf("%d",&breadth);
area=rect_area(len, breadth);
perimeter=rect_perimeter(len, breadth);
printf("Area of rectangle is: %d", area);
printf("\nPerimeter of rectangle is: %d", perimeter);

case 2:
printf("\n\nEnter the side of square: ");
scanf("%d",&len);
area=squ_area(len);
perimeter=squ_perimeter(len);
printf("Area of square is: %d", area);
printf("\nPerimeter of square is: %d", perimeter);

case 3:
printf("\n\nEnter the radius of circle: ");
scanf("%d",&rad);
area_c=cir_area(rad);
circumference=cir_perimeter(rad);
printf("Area of circle is: %f", area_c);
printf("\nCircumference of circle is: %f", circumference);
case 4:
printf("\n\nEnter the base of triangle (side1): ");
scanf("%d",&side1);
printf("Enter the height of the triangle: ");
scanf("%d",&height);
printf("Enter the side 2: ");
scanf("%d",&side2);
printf("Enter the side3: ");
scanf("%d",&side3);
area=tri_area(side1, height);
perimeter=tri_perimeter(side1, side2, side3);
printf("Area of triangle is: %d",area);
printf("\nThe perimeter of the triangle is: %d ",perimeter);
}
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}
OUTPUT
PROBLEM 31: PROGRAM TO FIND THE
EXPONENTIAL FUNCTION
PROGRAM
#include<stdio.h>
int main()

{
int n, exp, exp1;
long long int value = 1;

printf("Enter the number and its exponential: \n");


scanf("%d%d",&n, &exp);

exp1 = exp;

while(exp-- > 0)
{
value *= n;
}

printf("%d^%d = %lld", n, exp1, value);


printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 32: PROGRAM TO CONVERT
UPPER CASE TO LOWER CASE AND VICE-
VERSA
PROGRAM
#include <stdio.h>
int main()
{
int i, len = 0;
char str[25];
printf("Enter a string to convert upper to lwer case and viceversa:
");
scanf("%s",&str);

len = sizeof(str)/sizeof(str[0]);

for(i = 0; i < len; i++){

if(isupper(str[i])){

str[i] = tolower(str[i]);
}

else if(islower(str[i])){
str[i] = toupper(str[i]);
}
}
printf("String after case conversion : %s", str);
printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
return 0;}

OUTPUT
PROBLEM 33: PROGRAM RO CREATE A FILE
CALLED EMP.TXT AND STORE INFORMATION
ABOUT A PERSON IN TERMS OF ITS NAME,
AGE AND SALARY
PROGRAM
#include <stdio.h>

void main()
{
FILE *fptr;
char name[20];
int age;
float salary;

fptr = fopen("emp.txt", "w");

if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
}

OUTPUT
PROBLEM 34: PROGRAM TO GENERATE THE
EMPLOY DETAILS USING STRUCTURES
PROGRAM
#include <stdio.h>
struct employee{
char name[30];
int empId;
float salary;
};

int main()
{

struct employee emp;

printf("\nEnter details :\n");


printf("Name :"); gets(emp.name);
printf("ID :"); scanf("%d",&emp.empId);
printf("Salary :"); scanf("%f",&emp.salary);

printf("\nEntered detail is:");


printf("\nName: %s" ,emp.name);
printf("\nId: %d" ,emp.empId);
printf("\nSalary: %f\n",emp.salary);
printf("This output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return 0;
}

OUTPUT
PROBLEM 35: PROGRAM TO PASS AND
RETURN POINTER TO FUNCTION HENCE
CALCULATE AVERAGE OF AN ARRAY.
PROGRAM
#include <stdio.h>
int average(int *array,int n);
int main(){
int a[20],*array,n,i;
printf("enter number of elements: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter %d element: ",i+1);
scanf("%d",&a[i]);
}
array=a;
average(array,n);

}
int average(int *array,int n){
int i,sum=0;
float avg;
for(i=0;i<n;i++){
sum+=*(array+i);
}
avg=(float)sum/6;
printf("average is %f",avg);
printf("\nThis output belongs to SAMRIDHI KAPOOR 2K20/B11/12");
return avg;

OUTPUT
PROBLEM 36: PROGRAM TO PASS AN ARRAY
AS POINTER TO A FUNCTION THAT
CALCULATES THE SUM OF ALL ELEMENTS OF
THE ARRAY.
PROGRAM
#include<stdio.h>
int main()
{
int array[5];
int i,sum=0;
int *ptr;

printf("\nEnter array elements (5 integer values):");


for(i=0;i<5;i++)
scanf("%d",&array[i]);

ptr = array;

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

sum = sum + *ptr;


ptr++;
}

printf("\nThe sum is: %d",sum);


printf("\nThis output belongs to SAMRIDHI KAPOOR
2K20/B11/12");
}

OUTPUT
PROBLEM 37: PROGRAM TO DEMONSTRATE
THE EXAMPLE OF ARRAY OF POINTERS
PROGRAM
#include <stdio.h>

int main()
{
int a,b,c;

int *ptr[3];

ptr[0]= &a;
ptr[1]= &b;
ptr[2]= &c;

a=100;
b=200;
c=300;

printf("value of a: %d, b: %d, c: %d\n",*ptr[0],*ptr[1],*ptr[2]);


*ptr[0] +=10;
*ptr[1] +=10;
*ptr[2] +=10;
printf("After adding 10\nvalue of a: %d, b: %d, c:
%d\n",*ptr[0],*ptr[1],*ptr[2]);
printf("This output belongs to SAMRIDHI AKPOOR 2K20/B11/12");

return 0;
}

OUTPUT
PROBLEM 38: PROGRAM WHICH COPIES ONE
FILE CONTENTS TO ANOTHER FILE.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
void main() {
   FILE *fp1, *fp2;
   char ch;
   clrscr();
 
   fp1 = fopen("Sample.txt", "r");
   fp2 = fopen("Output.txt", "w");
 
   while (1) {
      ch = fgetc(fp1);
 
      if (ch == EOF)
         break;
      else
         putc(ch, fp2);
   }
 
   printf("This output belongs to Samridhi kapoor 2k20/b11/12");
   fclose(fp1);
   fclose(fp2);
}
OUTPUT

 This is the output of


.txt file
PROBLEM 39: PROGRAM TO READ A FILE
AND AFTER CONVERTING ALL LOWER CASE
TO UPPER CASE LETTERS WRITE IT
PROGRAM
#include<stdio.h>
#include<process.h>
 
void main() {
   FILE *fp1, *fp2;
   char a;
   clrscr();
 
   fp1 = fopen("test.txt", "r");
   if (fp1 == NULL) {
      puts("cannot open this file");
      exit(1);
   }
 
   fp2 = fopen("test1.txt", "w");
   if (fp2 == NULL) {
      puts("Not able to open this file");
      fclose(fp1);
      exit(1);
   }
 
   do {
      a = fgetc(fp1);
      a = toupper(a);
      fputc(a, fp2);
   } while (a != EOF);
 
   fcloseall();
printf(“File successfully copied…”);
printf(“This output belongs to Samridhi kapoor 2k20/b11/12”);
   getch();
}

OUTPUT
 This is the output of .txt file
PROBLEM 40: PROGRAM TO FIND THE SIZE
OF A GIVEN FILE.
PROGRAM
#include <stdio.h>
int findfileSize(char f_n[]) {
FILE* fp = fopen(f_n, "r");
if (fp == NULL) {
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
int res = ftell(fp);
fclose(fp);
return res;
}
int main() {
char f_n[] = { "b.txt" };
int result = findfileSize(f_n);
if (result != -1)
printf("Size of the file is %ld bytes \n", result);
printf(“This output belongs to Samridhi kapoor 2k20/b11/12”);
return 0;
}
OUTPUT
 This is the output of .txt file

You might also like