Co Lab File Last One
Co Lab File Last One
Programming Fundamentals
Lab File
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]>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;
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;
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;
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);
OUTPUT
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");
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;
do
{
i++;
scanf("%d",&a[i]);
}while(a[i]==1||a[i]==0);
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;
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>
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;
}
OUTPUT
PROBLEM 13: CALENDAR PROGRAM TO
DISPLAY THE DAY TO THE INPUT OF
CORRESPONDING DATE MONTH AND YEAR
PROGRAM
#include<stdio.h>
#include<iostream>
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;
OUTPUT
PROBLEM 15: BINARY SEARCH PROGRAM
PROGRAM
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
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;
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;
}
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;
int main()
{
int x, y, *a, *b, temp;
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
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;
}
}
}
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;
}
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;
return 0;
}
OUTPUT
length = FindLength(str);
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");
}
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;
OUTPUT
PROBLEM 28: PROGRAM TO CONCATENATE
TWO STRINGS
PROGRAM
#include <stdio.h>
int main()
scanf("%s",str1);
scanf("%s",str2);
str1[i]=str2[j];
str1[i]='\0';
printf("\nOutput: %s",str1);
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);
end = count - 1;
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;
exp1 = exp;
while(exp-- > 0)
{
value *= n;
}
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]);
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;
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()
{
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;
ptr = array;
for(i=0;i<5;i++)
{
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;
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
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