Vous êtes sur la page 1sur 52

Q1. Write a program in C to compute the area of a triangle where the three sides are given.

#include<stdio.h>

#include<math.h>

int main()

float a,b,c,s,area;

printf("Enter side a");

scanf("%f",&a);

printf("Enter side b");

scanf("%f",&b);

printf("Enter side c");

scanf("%f",&c);

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c));

printf("Area of the triangle is %f",area);

Q2. Write a program in C to convert the temperature from Fahrenheit to Celsius.


#include<stdio.h>

int main()

float fahrenheit,celsius;

printf("Enter the temperature in fahrenheit");

scanf("%f",&fahrenheit);

celsius=5*(fahrenheit-32)/9;

printf("\n\n temperature in celsius is:%f",celsius);

6-01-2022

Q1

Write a program that estimates the temperature in a freezer (in °C) given the elapsed time (hours)
since a power failure. Assume this temperature (T) is given by

T= 4t2
t + 2− 20
where t is the time since the power failure. Your program should prompt the user to enter how long it
has been since the start of the power failure in whole hours and minutes. Note that you will need to
convert the elapsed time into hours. For example, if the user entered 2 30 (2 hours 30 minutes), you
would need to convert this to 2.5 hours.

A1 #include<stdio.h>

int main()

float time,temperature;

printf("Enter the time in hours");

scanf("%f",&time);

temperature=((4*(time*time)/(time+2))-20);

printf("Enter the temperature in celsius %f",temperature);

Q2

Write a program that takes the length and width of a rectangular yard and the length and width of a
rectangular house situated in the yard. Your program should compute the time required to cut the
grass at the rate of two square feet a second.
A2 #include<stdio.h>

int main()

float l1,w1,l2,w2,A_Y,A_H,T;

printf("Enter the length of yard in feet:");

scanf("%f",&l1);

printf("Enter the width of yard in feet:");

scanf("%f",&w1);

printf("Enter the length of house in feet:");

scanf("%f",&l2);

printf("Enter the width of house in feet:");

scanf("%f",&w2);

A_Y=l1*w1;

A_H=l2*w2;

T=A_Y-A_H/2;

printf("The time required to cut the grass is%f",T);

}
Q3 Metro City Planners proposes that a community conserve its water supply by replacing all the
community’s toilets with low-flush models that use only 2 liters per flush. Assume that there is about 1
toilet for every 3 persons, that existing toilets use an average of 15 liters per flush, that a toilet is
flushed on average 14 times per day, and that the cost to install each new toilet is $150. Write a
program that would estimate the magnitude (liters/day) and cost of the water saved based on the
community’s population.

A3 #include<stdio.h>

int main()

float population,cost,Wc,Wi,Wi_Cost,Wc_Cost;

printf("Enter the total population");

scanf("%f",&population);

printf("Enter the cost of water per litre in dollar");

scanf("%f",&cost);

Wc=population*14*15/3;

Wi=population*14*2/3;

//Wc=current water used //Wi=ideal water used

Wc_Cost=Wc*cost;

Wi_Cost=Wi*cost;

printf("Magnitude of water saved in litres per day is %f",Wc-Wi);

printf("Cost of water saved is %f",Wc_Cost-Wi_Cost-150*population/3);

}
13-01-2022

Q1
Write a program that swaps the values of two variables. You have to write two different programs as
fol lows

1. use a third variable.


2. without using a third variable. Note that you are not allowed to declare more than two variables in
the program.

A1 1 #include<stdio.h>

int main()

{int a, b, c;

printf("Enter 1st Number: ");

scanf("%d", &a);

printf("Enter 2nd Number: ");

scanf("%d", &b);

c = a;

a = b;

b = c;

printf("First number is %d", a);

printf("Second number is %d", b);

}
1 2)

#include<stdio.h>

int main()

int a, b;

printf("Enter 1st Number: ");

scanf("%d", &a);

printf("Enter 2nd Number: ");

scanf("%d", &b);

a = a + b;

b = a - b;

a = a - b;

printf("1st number is %d", a);

printf("2nd number is %d", b);

}
OUTPUT

Q2 1) 1. Write a program to compute 10 . If the result is zero, then give a proper reason.
20

#include<stdio.h>

int main()

double base,exp,result;

printf("Enter a base number: ");

scanf("%lf",&base);

printf("Enter an exponent: ");

scanf("%lf", &exp);

result=pow(base,exp);

printf("%.1lf^%.1lf=%.2lf",base,exp,result);

return 0;
}

OUTPUT

Q2 2) Evaluate the following expressions if x is 10.5 , y is 7.2 , m is 5 , and n is 2 .


• x / (double)m
•x/m
• (double)(n * m)
• (double)(n / m) + y
• (double)(n / m)

A2 2) #include<stdio.h>

int main()

{double x=10.5,y=7.2,m=5,n=2,a,b,c,d,e;

a=x/(double)m;

printf("output1 %f",a);

b=x/m;

printf("output2 %f",b);

c=(double)(n*m);

printf("output3 %f",c);
d=(double)(n/m)+y;

printf("output4 %f",d);

e=(double)(n/m);

printf("output5 %f",e);

OUTPUT

Q2 3) Write a program that stores the values ‘A’, ‘B’, 19, and -0.42E7 in separate memory cells that
you have declared. Use an assignment statement to store the first value, but get the other three
values as input data from the user.

A2 3) #include<stdio.h>

int main()

char var1 = 'A';


char var2;

int var3;

double var4;

printf("ENTER THE REMAINING VALUES : ");

scanf("%c %d %lf", &var2, &var3, &var4);

printf(" var1= %c\n var2= %c\n var3= %d\n var4= %lf\n",var1, var2,

var3, var4);

return 0;

OUTPUT
Q 2 4) Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35
per mile. Your program should interact with the user in the following manner:
MILEAGE REIMBURSEMENT CALCULATOR
Enter beginning odometer reading=> 13505.2
Enter ending odometer reading=> 13810.6

A 2 4) #include<stdio.h>

int main()

printf("\nMILEAGE REIMBURSEMENT CALCULATOR\n");

float i, f, c;

printf("Enter initial odometer reading: ");

scanf("%f", &i);

printf("Enter final odometer reading: ");

scanf("%f", &f);

printf("\nYou travelled %f miles", f - i);

c = 0.35 * (f - i);

printf("\nAt $0.35 per mile, your reimbursement is %f", c);

OUTPUT
20-01-2022

Q1

Write a program that computes a customers water bill. The bill includes a $35 water demand charge
plus a consumption (use) charge of $1.10 for every thousand gallons used. Consumption is figured
from meter read ings (in thousands of gallons) taken recently and at the end of the previous month. If
the customers unpaid balance is greater than zero, a $2 late charge is assessed as well.
EXPLANATION:
The total water bill is the sum of the demand and use charges, the unpaid balance, and a possible
late charge. The demand charge is a program constant ($35), but the use charge must be computed.
To do this, we must know the previous and current meter readings (the problem inputs). After
obtaining these data, we can compute the use charge by multiplying the difference between the two
meter readings by the charge for 1000 gallons, the problem constant $1.10. Next, we can determine
the applicable late charge, if any, and finally compute the water bill by adding the four components.

A1 #include <stdio.h>

int main() {

float water_used, i, f, cost;

char late;
printf("Enter the previous month reading in 1000 gallons: ");

scanf("%f", &i);

printf("Enter the current month reading in 1000 gallons: ");

scanf("%f", &f);

printf("Late (yes or no): ");

scanf("%s", &late);

water_used = f - i;

cost = 35 + (1.1 * water_used);

if (late == "yes")

cost = cost + 2;

printf("\nCost is %f", cost);

return 0;

}
OUTPUT

Q2 Write an interactive program that contains an if statement that may be used to compute the area
of a square (area = side2) or a circle (area = π × radius2) after prompting the user to type the first
character of the figure name (S or C).

A2

#include <stdio.h>

#include<math.h>

int main() {

char choice;

float area_of_circle, r, area_of_square, s;

printf("Enter your choice(S or C): ");

scanf("%s", &choice);

if (choice == 'S')

printf("Enter side of square: ");

scanf("%f", &s);

area_of_square = s * s;
printf("Area of Square is %f", area_of_square);

else if (choice == 'C')

printf("Enter radius of circle: ");

scanf("%f", &r);

area_of_circle = 3.14 * r * r;

printf("Area of Circle is %f", area_of_circle);

return 0;

OUTPUT

HOMEWORK

Problem 1: Pattern Drawing

Print the following figures for n lines where n is the input from the user.
A1 #include<stdio.h>
int main()
{int n;
printf("Enter the number");
scanf("%d",&n);
printf("\n\n");
for(int i=1;i<=n;i++)
{

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


{
printf("* ");

printf("\n");
}
}

OUTPUT

A2 #include<stdio.h>
int main ()
{
int n;
printf ("Enter the number");
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
{

for (int j = (n - i); j >= 0;j--)


{
printf (" ");

}
for (int j = 1; j <= i; j++)
{
printf ("*");
}
printf ("\n");
}
}

OUTPUT

A3 #include<stdio.h>
int main ()
{
int n;
printf ("Enter the number");
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
{

for (int j = (n - i); j >= 0;)


{
printf (" ");
j = j - 1;

}
for (int j = 1; j <= i; j++)
{
printf ("* ");
}
{for(int j=1;j<=i-1;j++)
printf("* ");
}
printf ("\n");

}
}
OUTPUT

A4 #include<stdio.h>
int main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
for(int i=0; i<n; i++)
{
for(int j=n;j>i;j--)
{
printf(" ");

}
for(int k=i;k>=0;k--)
{
printf("%d ",k);
}
if(i>=1)
{
for(int l=1;l<=i;l++)
{
printf("%d ",l);

}
}
printf("\n");
}

}
OUTPUT

A5 #include<stdio.h>
int main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
if(n>26)
{
printf("Please Enter a number less than equal to 26");

for(int j=n-i;j>=0;j--)
{ printf(" ");
}
for(int k=i; k>=0 ; k--)
{
k=k+65;
char control=k;
printf("%c",control);
k=k-65;

}
printf("\n");

}
OUTPUT

A6 #include<stdio.h>
int main ()
{
int n;
printf ("Enter the number");
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
{

for (int j = (n - i); j >= 0;j--)


{
printf (" ");

}
for (int j = 1; j <= i; j++)
{
printf ("* ");
}
printf ("\n");

}
}
OUTPUT

A7 #include<stdio.h>
int main ()
{
int n;
int w = 1;
printf ("Enter a number: ");
scanf ("%d", &n);
for (int i = 0; i <= n; i++)
{
for (int j = i; j >= 0; j--)
{
if (w < 10)
{
printf ("%d ", w);
}
else
{
printf ("%d ", w);
}
w = w + 1;
}
printf ("\n");
}
}
OUTPUT

A8 #include<stdio.h>
int main ()
{
int n;
printf ("Enter the number");
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = (n - i -1); j >= 0;)
{
printf (" ");
j = j - 1;

}
for (int j = 1; j <= i; j++)
{
printf ("* ");
}
for (int j = 1; j <= i - 1; j++)
{
printf ("* ");
}
printf ("\n");
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
printf (" ");
}
for (int j = (n - i); j >= 1; j--)
{
printf ("* ");
}
for (int j = (n - i - 1); j >= 1; j--)
{
printf ("* ");
}
printf ("\n");

}
}

OUTPUT

A9 #include<stdio.h>
int main()
{
int rows,coef=1,space,i,j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i=0;i<rows;i++)
{
for(space=1;space<=rows-i;space++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j==0 || i==0)
coef=1;
else
coef=coef*(i-j+1);
printf("%4d",coef);

}
printf("\n");
}

}
OUTPUT

27-01-2022

Q1

An Internet Service Provider charges its subscribers every month based on the information provided
in the following table:

Table 1: Rate Chart


Data Usage (n) in GBs Charges in $

0.0 < n ≤ 1.0 250

1.0 < n ≤ 2.0 500

2.0 < n ≤ 4.0 750

4.0 < n ≤ 6.0 1000

6.0 < n ≤ 8.0 1250

8.0 < n ≤ 10.0 1500

n > 10.0 2000

Given the amount of data used by the subscriber (i.e. n), write a program to calculate the charges
to be paid by the subscriber. Print a message to indicate bad data as well.

A1 #include <stdio.h>

int
main ()
{
int n, cost;
cost = 0;
printf ("Enter data used in GBs: ");
scanf ("%d", &n);
if (n > 10)
{
cost = 250+500+750+1000+1250+1500+2000;
}
else if (n > 8 && n <= 10)
{
cost = 250+500+750+1000+1250+1500;
}
else if (n > 6 && n <= 8)
{
cost = 250+500+750+1000+1250;
}
else if (n > 4 && n <= 6)
{
cost = 250+500+700+1000;
}
else if (n > 2 && n <= 4)
{
cost = 250+500+750;
}
else if (n > 1 && n <= 2)
{
cost = 250+500;
}
else if (n > 0 && n <= 1)
{
cost = 250;
}
else
printf ("Bad Data");

printf ("Total cost %d", cost);

return 0;
}

OUTPUT
Q2 There are 9,870 people in a town whose population increases by 10% each year. Write a loop
that displays the annual population and determines how many years it will take for the population to
surpass 30,000.

A2 #include <stdio.h>

int main ()
{
int i;
float popu;
i = 0;
popu = 9870;
while (popu <= 30000)
{
popu = popu * 1.1;
i += 1;
printf ("\nPopulation after %d years is %f", i, round(popu));

}
printf ("\nYears to surpass 30,000 is %d", i);

return 0;
}

OUTPUT

03-02-2022

Q1

The greatest common divisor (gcd) of two integers is the product of the integers’ common factors.
Write a program that inputs two numbers and compute the gcd.
A1 #include <stdio.h>

int main ()
{
int a, b, i, gcd;
printf ("Enter 1st Number: ");
scanf ("%d", &a);
printf ("Enter 2nd Number: ");
scanf ("%d", &b);
for (i = 1; i <= a && i <= b; i++)
{
if (a % i == 0 && b % i == 0)
{
gcd = i;
}
}
printf ("\nGCD of the numbers is: %d", gcd);
return 0;
}

OUTPUT

Q2

Write a program to process weekly employee time cards for all employees of an organization. Each
employee will have three data items: an identification number, the hourly wage rate, and the number
of hours worked during a given week. Each employee is to be paid for total worked time and a half for
all hours worked over 40. A tax amount of 3.625% of gross salary will be deducted. The program
output should show the employee’s number and net pay. Display the total payroll and the average
amount paid at the end of the run. You need to solve the problem using the topics covered in the
class only.

A2 #include<stdio.h>
#include<math.h>
int main()
{
double ident_num,wage,hours,gross_salary,net_salary,avg,net_pay;
int n,i;
net_pay=0;
printf("Enter the total number of employees\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the identity number \n",i);
scanf("%lf",&ident_num);
printf("Enter the hourly wage in rupees \n",i);
scanf("%lf",&wage);
printf("Enter the hours worked \n",i);
scanf("%lf",&hours);
if(hours<=40)
{
gross_salary=wage*hours;
}
if(hours>40)
{
gross_salary=wage*40+(hours-40)*(wage/2);
}
net_salary=0.96375*gross_salary;

printf("net salary of employee %lf \n",net_salary);

net_pay=net_pay+net_salary;

}
avg=net_pay/n;
printf("\n");

printf("The number of employees %d\n",n);


printf("Their total payroll %lf\n",net_pay);
printf("Their average salary is %lf\n",avg);

}
OUTPUT

Q3 Write a menu driven program to implement an arithmetic calculator. The calculator must compute
addition, subtraction, multiplication, division, remainder and percentage between two numerical
values. The program must run until it gets confirmation from the user.

A3 #include <stdio.h>

int main ()
{
int a, b, i, sum, diff, pro, qui, rem, perd, choice, oper;
printf ("Want to calculate? Type 1 (for Yes) and 0 (for NO): ");
scanf ("%d", &choice);
while (choice == 1)
{
printf ("\nEnter 1st Number: ");
scanf ("%d", &a);
printf ("\nEnter 2nd Number: ");
scanf ("%d", &b);
sum = a + b;
if (a > b)
{
diff = a - b;
qui = a / b;
rem = a % b;
perd = (a - b) * 100 / a;
}
else
{
diff = b - a;
qui = b / a;
rem = b % a;
perd = (b - a) * 100 / b;
}
pro = a * b;
printf ("\n 1 for addition");
printf ("\n 2 for subtraction");
printf ("\n 3 for product");
printf ("\n 4 for division");
printf ("\n 5 for remainder");
printf ("\n 6 for percentage difference");
printf ("\n\nEnter operation : ");
scanf ("%d", &oper);
if (oper == 1)
{
printf ("%d", sum);
}
if (oper == 2)
{
printf ("%d", diff);
}
if (oper == 3)
{
printf ("%d", pro);
}
if (oper == 4)
{
printf ("%d", qui);
}
if (oper == 5)
{
printf ("%d", rem);
}
if (oper == 6)
{
printf ("%d", perd);
}

return 0;
}

OUTPUT

10-02-2022

Q1
Write a program to find maximum and minimum element from an array and also find the average of all
elements.

A1 #include <stdio.h>
int main()
{
int a[1000],i,n,min,max,sum=0;
float mean;

printf("Enter size of the array : ");


scanf("%d",&n);

printf("Enter elements in array : ");


for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
for(i=1;i<=n;i++)
{
sum+=a[i];
}
mean=sum/n;
printf("The mean of numbers is %f",mean);

return 0;
}
OUTPUT

Q2

Write a C program to input elements in an array from user and sort all even and odd elements of the
given array separately without using any other array. If minimum element of the array is even, then all
even elements should be placed in sorted order before odd elements otherwise all odd elements
should be placed first.

A2 #include<stdio.h>
int main()
{
int a[10000],b[10000],i,n,j,k,temp,c=0;

printf("Enter size of the array : ");


scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
if(a[i]%2==1)
c++;
}
for(i=0; i<n-1; i++)
{

for(j=0; j<n-i-1; j++)


{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

k=0;
j=n-c;

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


{
if(a[i]%2==0)
{
if(k<n-c)
b[k++]=a[i];
}
else
{
if(j<n)
b[j++]=a[i];
}
}

printf("\narray after sorting even and odd elements separately:\n ");

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


{
a[i]=b[i];
printf("%d ",a[i]);
}

OUTPUT
Q3 Write a menu driven program to implement stack.
A3 #include<stdio.h>
#define MAX 10

int stack_arr[MAX];
int top = -1;

void push(int item);


int pop();
int peek();
int isEmpty();
int isFull();
void display();

int main()
{
int choice,item;
while(1)
{
printf("\n1.Push\n");
printf("2.Pop\n");
printf("3.Display the top element\n");
printf("4.Display all stack elements\n");
printf("5.Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("\nEnter the item to be pushed : ");
scanf("%d",&item);
push(item);
break;
case 2:
item = pop();
printf("\nPopped item is : %d\n",item );
break;
case 3:
printf("\nItem at the top is : %d\n", peek() );
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\nWrong choice\n");
}/*End of switch*/
}/*End of while*/

return 0;

}/*End of main()*/

void push(int item)


{
if( isFull() )
{
printf("\nStack Overflow\n");
return;
}
top = top+1;
stack_arr[top] = item;
}/*End of push()*/

int pop()
{
int item;
if( isEmpty() )
{
printf("\nStack Underflow\n");
exit(1);
}
item = stack_arr[top];
top = top-1;
return item;
}/*End of pop()*/

int peek()
{
if( isEmpty() )
{
printf("\nStack Underflow\n");
exit(1);
}
return stack_arr[top];
}/*End of peek()*/

int isEmpty()
{
if( top == -1 )
return 1;
else
return 0;
}/*End of isEmpty*/

int isFull()
{
if( top == MAX-1 )
return 1;
else
return 0;
}/*End of isFull*/

void display()
{
int i;
if( isEmpty() )
{
printf("\nStack is empty\n");
return;
}
printf("\nStack elements :\n\n");
for(i=top;i>=0;i--)
printf(" %d\n", stack_arr[i] );
printf("\n");
}/*End of display()*/
OUTPUT

17-02-2022

Q1

Write a program to multiply an M × N matrix with a P × Q matrix. If the multiplication is not possible,
then clearly print the message.

A1 #include <stdio.h>

// function to get matrix elements entered by the user


void getMatrixElements(int matrix[][10], int row, int column) {

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

for (int i = 0; i < row; ++i) {


for (int j = 0; j < column; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}

// function to multiply two matrices


void multiplyMatrices(int first[][10],
int second[][10],
int result[][10],
int r1, int c1, int r2, int c2) {

// Initializing elements of matrix mult to 0.


for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
result[i][j] = 0;
}
}

// Multiplying first and second matrices and storing it in result


for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}

// function to display the matrix


void display(int result[][10], int row, int column) {

printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}

int main() {
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);

// Taking input until


// 1st matrix columns is not equal to 2nd matrix row
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}

// get elements of the first matrix


getMatrixElements(first, r1, c1);

// get elements of the second matrix


getMatrixElements(second, r2, c2);

// multiply two matrices.


multiplyMatrices(first, second, result, r1, c1, r2, c2);

// display the result


display(result, r1, c2);

return 0;
}
OUTPUT

Q3 A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit code scanned by
comparing the code’s last digit (called a check digit) to its own computation of the check digit from the
first 11 digits as follows:
• Calculate the sum of the digits in the odd-numbered positions (the first, third, . . ., eleventh digits)
and multiply this sum by 3.
• Calculate the sum of the digits in the even-numbered positions (the second, fourth, . . ., tenth
digits) and add this to the previous result.
• If the last digit of the result from step 2 is 0, then 0 is the check digit. Otherwise, subtract the last
digit from 10 to calculate the check digit.
• If the check digit matches the final digit of the 12-digit UPC, the UPC is assumed correct.
Write a program that prompts the user to enter the 12 digits of a barcode separated by spaces. The
program should store the digits in an integer array, calculate the check digit, and compare it to the
final barcode digit. If the digits match, output the barcode with the message “validated”. If not, output
the barcode with the message “error in barcode”. Also, output with labels the results from steps 1 and
2 of the check-digit calculations. Note that the “first” digit of the barcode will be stored in index 0 of the
array.

A3 #include <stdio.h>
#include <math.h>
int main()
{
int arr[12], sum_odd = 0, sum_even = 0, total_sum = 0, last_digit, check_digit;

printf("> Enter an array: ");


for(int i = 0; i < 12; i++)
{
scanf("%d", &arr[i]);
}
for(int i = 0; i < 11; i+=2)
{
sum_odd += arr[i];
}
for(int i = 1; i < 10; i+=2)
{
sum_even += arr[i];
}
total_sum = (sum_odd * 3) + sum_even;

last_digit = total_sum % 10;

if(last_digit == 0)
{
check_digit = 0;
}
else
{
check_digit = 10 - last_digit;
}
if(check_digit == arr[11])
{
printf("\n> Validated");
}
else
{
printf("\n> Barcode is: ");
for(int i = 0; i < 12; i++)
{
printf("%d", arr[i]);
}
printf("\n> Error in barcode\n");
printf("> Step 1 result: %d\n", (sum_odd * 3));
printf("> Step 2 result: %d\n", total_sum);
}
return 0;
}
OUTPUT

Q2

Write a program to take two numerical lists of the same length ended by a sentinel value and store
the lists in arrays X and Y, each of which has 20 elements. Let n be the actual number of data values
in each list. Store the product of corresponding elements of X and Y in a third array, Z, also of size 20.
Display the arrays X, Y, and Z in a three-column table. Then compute and display the square root of
the sum of the items in Z. Make up your own data, and be sure to test your program on at least one
data set with number lists of exactly 20 items. One data set should have lists of 21 numbers, and one
set should have significantly shorter lists.

A2 #include <stdio.h>
#include <math.h>
int main ()
{
int x[20], y[20], z[20], n, sum = 0;
float square_root;
printf("> Enter size of list (less equal to 20): ");
scanf("%d", &n);
printf ("> Enter 1st array: ");
for (int i = 0; i < n; i++)
{
scanf ("%d", &x[i]);
}
printf ("> Enter 2nd array: ");
for (int i = 0; i < n; i++)
{
scanf ("%d", &y[i]);
}
for (int i = 0; i < n; i++)
{
z[i] = x[i] * y[i];
sum += z[i];
}
printf ("> 1st List (X): ");
for (int i = 0; i < n; i++)
{
printf ("%d ", x[i]);
}
printf ("\n");
printf ("> 2nd List (Y): ");
for (int i = 0; i < n; i++)
{
printf ("%d ", y[i]);
}
printf ("\n");
printf ("> Product List (Z): ");
for (int i = 0; i < n; i++)
{
printf ("%d ", z[i]);
}
printf ("\n");
square_root = sqrt(sum);
printf("> Square root of sum of items in z: %f", square_root);

return 0;
}

OUTPUT

24-02-2022

Q1
Each year the Department of Traffic Accidents receives accident count reports from a number of cities
and towns across the country. To summarize these reports, the department provides a frequency
distribution print out that gives the number of cities reporting accident counts in the following ranges:
0–99, 100–199, 200–299, 300–399, 400–499, and 500 or above. The department needs a computer
program to take the number of acci dents for each reporting city or town and add one to the count for
the appropriate accident range. After all the data have been processed, the resulting frequency
counts are to be displayed.

A1 #include <stdio.h>

int main ()
{
int n;
printf ("Enter total cities :\n");
scanf ("%d", &n);
int acc[n], a, i, r1 = 0, r2 = 0, r3 = 0, r4 = 0, r5 = 0, r6 = 0;
printf ("Enter accident count of cities :\n");
for (i = 0; i < n; i++)
{

scanf (" %d", &acc[i]);


}
for (i = 0; i < n; i++)
{
a = acc[i];
if ((a >= 0) && (a <= 99))
{
r1 = r1 + 1;
}

else if ((a >= 100) && (a <= 199))


{
r2 = r2 + 1;
}

else if ((a >= 200) && (a <= 299))


{
r3 = r3 + 1;
}

else if ((a >= 300) && (a <= 399))


{
r4 = r4 + 1;
}

else if ((a >= 400) && (a <= 499))


{
r5 = r5 + 1;
}
else if (a >= 500)
{
r6 = r6 + 1;
}
}
printf ("\n Range Frequency");
printf ("\n 0-99 %d", r1);
printf ("\n 100-199 %d", r2);
printf ("\n 200-299 %d", r3);
printf ("\n 300-399 %d", r4);
printf ("\n 400-499 %d", r5);
printf ("\n 500 or above %d", r6);
}

OUTPUT

Q2

Write a program that returns a 1 for true if its string argument ends in the substring OH . Try the
program on the following data: KOH, H2O2, NaCl, NaOH, C9H8O4, Mg(OH)2

A2 #include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int str_len;
char input_s[30];
{
printf("Enter the chemical name:\n");
scanf("%s",&input_s);
str_len=strlen(input_s);
if(input_s[str_len-1]=='H'&&input_s[str_len-2]=='O')
printf("1\n");
else
printf("0\n");
}
getch();
}

OUTPUT

Q3

Write a program to demonstrate the use of following functions- strlen(), strcpy(), strncpy(), strcmp(),
strcat(). Then write programs to implement each of the above string library functions.

A3 #include<stdio.h>
#include<string.h>
int main()
{
int n;
char name[20],age[20],temp[20],verify[5]="MOHAK";
printf("Enter verified user name : ");
scanf("%[^\n]s",&name);
printf("Enter verified user age : ");
scanf("%s",&age);

n=strlen(name);
printf("\nName length is - %d character.\n",n);

strcpy(temp,name);
printf("%s",temp);
printf("Name Copy Completed\n");

strcat(name,age);
printf(name);
printf("Name and age join completed.\n");

if(strcmp(verify,temp)=='\1')
{
printf("%s",name);
printf("This user is verified. ");
}
else
{
printf("%s",name);
printf("This user is not verified.");
}

return 0;

OUTPUT

Q4 Write a program that takes data a line at a time and reverses the words of the line. The line
should have one blank between each pair of words.

A4 #include<stdio.h>
#include<string.h>
int main()
{
int i,j,len,stlen,start,end;
char line[100]={};

printf("Enter The String : \n");


gets(line);
start=0;
end=0;
strcat(line," ");

len=strlen(line);
printf("\n\nThe result is : \n");
for(i=0;i<len;i++)
{
if(line[i]==' ')
{
end=i-1;
for(j=end;j>start-1;j--)
{
printf("%c",line[j]);

}
printf(" ");
start=i+1;
}

}
printf("\n\n");

OUTPUT

03-03-2022

Q1 Write a recursive program that takes a sentence as input and check whether the given sentence is a
palindrome or not.

A1 #include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isPalRec(char str[],


int s, int e)
{

if (s == e)
return true;

if (str[s] != str[e])
return false;

if (s < e + 1)
return isPalRec(str, s + 1, e - 1);

return true;
}

bool isPalindrome(char str[])


{
int n = strlen(str);

if (n == 0)
return true;

return isPalRec(str, 0, n - 1);


}

int main()
{
char str[1000];
printf("Enter the sentence to be checked \n");
gets(str);

if (isPalindrome(str))
printf("yes");
else
printf("No");

return 0;
}

OUTPUT

Q2 Write a recursive program that prints all the subsets of a string. Note that all the substrings must be
unique.

A2 #include <stdio.h>

char string[50], n;

void subset();
int main()
{
int i, len;
printf("Enter the len of main set : ");
scanf("%d", &len);
printf("Enter the elements of main set : ");
scanf("%s", string);
n = len;
printf("The subsets are :\n");
for (i = 1;i <= n;i++)
subset(0, 0, i);
}

void subset(int start, int index, int num_sub)


{
int i, j;
if (index - start + 1 == num_sub)
{
if (num_sub == 1)
{
for (i = 0;i < n;i++)
printf("%c\n", string[i]);
}
else
{
for (j = index;j < n;j++)
{
for (i = start;i < index;i++)
printf("%c", string[i]);
printf("%c\n", string[j]);
}
if (start != n - num_sub)
subset(start + 1, start + 1, num_sub);
}
}
else
{
subset(start, index + 1, num_sub);
}
}

OUTPUT

Vous aimerez peut-être aussi