C Assignment File
C Assignment File
C Assignment File
of
“ C++ ”
I Yogendra verma express our sincere gratitude to Sr. Assit. Prof. Nitin Paharia sir for giving
us the opportunity to work under his guidance on the assignment.
I am grateful to our Director Dr.Nishant Joshi and friends for their valuable suggestions in the
execution of this assignment.
I are also thankful to other staff that guided and helped us very kindly at each and every step
whenever we required.
I also acknowledge & convey thanks to the library staff, computer department of PIMR, Gwalior
for their kind and valuable support.
CERTIFICATE
This is to certify that Yogendra verma of BCA 1st Semester (B) of Prestige
Institute of Management & Research Gwalior, have successfully completed their
Assignment. he has prepared this assignment under my direct supervision and
guidance.
Question 1.
You are planning a car trip so you post on a carpooling website in order to
share the cost of the trip.
If you have 0 passengers the carpool site does not charge anything and you
alone pay the full cost of the trip. If you have 1 or more passengers the
carpool site adds a $1 fee to the cost of the trip and evenly divides the total
cost ($1 fee + gas) among the passengers and you. You want to write a
program that calculates the cost you have to pay. The program should read
the number of passengers (an integer) and the cost of gas for the trip (a
decimal number). The program should then print the cost that you have to
pay (a decimal number) with 2 digits after the decimal point.
Code:
#include<stdio.h>
float total_cost(int a,float b)
{
float t;
if(a>=1)
{
t=(b+1)/(a+1);
return(t);
}
else
{
t=b;
return(t);
}
}
void main()
{
int p;
float c;
printf("enter no. of passengers =");
scanf("%d",&p);
printf("enter cost of gas =");
scanf("%f",&c);
printf("%.2f",total_cost(p,c));
}
Examples
Input
0 23.9
Output
23.90
Input
2 45.5
Output
15.50
Input
3 34.8
Output
8.95
Question 2.
he hostel in which you plan to spend the night tonight offers very interesting
rates, as long as you do not arrive too late. Housekeeping finishes preparing
rooms by noon, and the sooner guests arrive after noon, the less they have to
pay. You are trying to build a C program that calculates your price to pay
based on your arrival time.
Your program will read an integer (between 0 and 12) indicating the number
of hours past noon of your arrival. For example, 0 indicates a noon arrival, 1 a
1pm arrival, 12 a midnight arrival, etc. The base price is 10 dollars, and 5
dollars are added for every hour after noon. Thankfully the total is capped at
53 dollars, so you'll never have to pay more than that. Your program should
print the price (an integer) you have to pay, given the input arrival time.
Code:
#include<stdio.h>
int total_fair(int a)
{
int t;
if(a>=0)
{
t=a*5+10; // base fair is 10
if(t>53)
t=53;
return(t);
}
}
void main()
{
int h;
printf("enter hours after noon of arrival =");
scanf("%d",&h);
printf("%d",total_fair(h));
}
Example 1
Input
Output
45
Example 2
Input
10
Output
53
Question 3.
You arrive in front of a bridge that you must cross to reach a village before
dark. Crossing the bridge is not free - the bridgekeeper asks you to roll two
dice to determine the cost. You decide to write a program to verify that he is
charging the right price.
Your program should read two integers, between 1 and 6, representing the
values of each die. If the sum is greater than or equal to 10, then you must
pay a special fee (36 coins). Otherwise, you pay twice the sum of the values
of the two dice. Your program must then display the text "Special tax" or
"Regular tax" followed by the amount you have to pay on the next line.
Code:
#include<stdio.h>
int tax(int a,int b)
{
int s,t;
s=a+b;
if(s>=10)
t=36;
else
t=2*s;
return(t);
}
void main()
{
int x,y,z;
printf("enter the values on the dice =");
scanf("%d%d",&x,&y);
z=tax(x,y);
if(z>=10)
printf("special tax =%d",z);
else
printf("regular tax =%d",z);
}
Example
Input
5
6
Output
Special tax’’
l.[pppppppppppooooookkkkkkkkkkbgvv36
Input
4
3
Output
Regular tax
14
Question 4.
You decide to bet on the final match of the Tug of War National
Championship.
Prior to the match the names and weights of the players are presented,
alternating by team (team 1 player 1, team 2 player 1, team 1 player 2, and
so on). There is the same number of players on each side. You record the
player weights as they are presented and calculate a total weight for each
time to inform your bet. You write a C program to assist with this.
Your program should first read an integer indicating the number of members
per team. Then, the program should read the player weights (integers
representing kilograms) alternating by team.
After calculating the total weight of each team, the program should display
the text "Team X has an advantage" (replacing X with 1 or 2 depending on
which team has a greater total weight).
You will then display the text "Total weight for team 1:" followed by the
weight of team 1, then "Total weight for team 2:" followed by the weight of
team 2 (see example below).
You are guaranteed that the two teams will not have the same total weight.
Code:
#include<stdio.h>
void main()
{
int a,n,i,weight[100],t1=0,t2=0;
printf("enter no. members in a team =\n");
scanf("%d",&n);
printf("enter weight of each team member alternatively =\n");
for(i=0;i<2*n;i++)
scanf("%d",&weight[i]);
for(i=0;i<2*n;i++)
{
if(i%2==0)
t1=t1+weight[i];
else
t2=t2+weight[i];
}
if(t1>t2)
{
a=1;
printf("team %d has an advantage\n",a);
}
else if(t1==t2)
printf("both team has same weight \n");
else
{
a=2;
printf("team %d has an advantage\n",a);
}
printf("total weight for team 1: %d\n",t1);
printf("total weight for team 2: %d\n",t2);
}
Example
Each team is composed of four players. Those of the first weigh 110, 113,
112, and 117kg, while those of the second weigh 106, 102, 121, and 111kg.
Team 1 weighs a total of 452kg whereas team 2 weighs a total of 440kg,
giving team 1 an advantage.
Input
4
110
106
113
102
112
121
117
111
Output
Team 1 has an advantage
Total weight for team 1: 452
Total weight for team 2: 440
Question 5.
The hostel in which you stop for the night changes its prices according to the
age of the customer and the weight of their luggage. The rules are not very
clear, so you decide to write a small program that will easily allow you and
your travel companions to know the price of one night.
One room costs nothing if you are 60 (the age of the innkeeper), or 5 dollars
if you are less than 10 years old. For everyone else, the cost is 30 dollars
plus an additional 10 dollars if you bring more than 20 pounds of luggage.
Your program should read the customer's age first, then the weight of their
luggage, then output the price they have to pay.
Code:
#include<stdio.h>
int total_fair(int a,int b)
{
int t=0;
if(a>60)
t=0;
else if(a<10)
t=t+5;
else
t=t+30;
if(b>20)
t=t+10;
return(t);
}
void main()
{
int a,l;
printf("enter your age and luggage weight in pounds=");
scanf("%d%d",&a,&l);
printf("your total fair is =%d",total_fair(a,l));
}
Example
Input:
22
25
Output:
40
Questions 6.
As you cross a forest you can't help but admire the nature around you
including the many species of trees. Despite your interest, you are a very
unskilled botanist and have a lot of trouble identifying different trees. A
friend gives you some guidance and you decide to write a program that will
give you the name of the tree based on its characteristics.
the "Tinuviel" is 5 meters high or less and its leaves are composed of 8 or
more leaflets
the "Calaelen" is 10 meters high or more and its leaves are composed of 10
or more leaflets
the "Falarion" is 8 meters high or less and its leaves are composed of 5 or
fewer leaflets
the "Dorthonion" is 12 meters tall or more and its leaves are composed
of 7 or fewer leaflets
Your program should read the height and the number of leaflets of a given
tree (both integers), and should be able to determine and display the name of
the corresponding tree. If the height and number of leaflets does not match
any of the tree type descriptions, your program should display "Uncertain".
Code:
#include<stdio.h>
int identify_tree(int a,int b)
{
if(a<5 && b>8)
printf("tinuviel");
else if(a>10 && b>10)
printf("calaelen");
else if(a<8 && b<5)
printf("falarion");
else if(a>12 && b<7)
printf("dorthonion");
else
printf("uncertain");
return 0;
}
void main()
{
int h,l;
printf("enter height and no. of leaflets =");
scanf("%d%d",&h,&l);
identify_tree(h,l);
}
Example 1
Input
12
12
Output
Calaelen
Example 2
Input
4
9
Output
Tinuviel
Example 3
Input
4
6
Output
Uncertain
Question 7.
Your grandparents gave you a fantastic cooking recipe but you can never
remember how much of each ingredient you have to use! There are 10
ingredients in the recipe and the quantities needed for each of them are
given as input (in grams). Your program must read 10 integers (the quantities
needed for each of the ingredients, in order) and store them in an array. It
should then read an integer which represents an ingredient's ID number
(between 0 and 9), and output the corresponding quantity.
Code:
#include<stdio.h>
int find_quantity(int a[],int size,int c)
{
int q;
q=a[c];
return(q);
}
void main()
{
int i,n,x[10];
printf("enter quantity of ingredients =\n");
for(i=0;i<10;i++)
scanf("%d",&x[i]);
printf("which ingredient's quantity do you want =");
scanf("%d",&n);
printf("the quantity of is = %d",find_quantity(x,10,n));
}
Example
Input:
Output:
25
Question 8.
You are responsible for a rail convoy of goods consisting of several boxcars.
You start the train and after a few minutes you realize that some boxcars are
overloaded and weigh too heavily on the rails while others are dangerously
light. So you decide to stop the train and spread the weight more evenly so
that all the boxcars have exactly the same weight (without changing the
total weight). For that you write a program which helps you in the
distribution of the weight.
Your program should first read the number of cars to be weighed (integer)
followed by the weights of the cars (doubles). Then your program should
calculate and display how much weight to add or subtract from each car
such that every car has the same weight. The total weight of all of the cars
should not change. These additions and subtractions of weights should be
displayed with one decimal place.
Code:
#include<stdio.h>
int weight_correction(double a[],int b)
{
int i,j;
double c,t=0;
for(i=0;i<b;i++)
t=t+a[i];
t=t/b;
for(j=0;j<b;j++)
{
c=t-a[j];
printf("%.1lf\n",c);
}
return 0;
}
void main()
{
int i,n;
double w[50];
printf("enter no. of boxcars =");
scanf("%d",&n);
printf("enter weight of boxcars =");
for(i=0;i<n;i++)
scanf("%lf",&w[i]);
printf("ideal weights are =");
weight_correction(w,n);
}
Example 1
Input
5
40.0
12.0
20.0
5.0
33.0
Output
-18.0
10.0
2.0
17.0
-11.0
Question 9.
you plan to make a delicious meal and want to take the money you need to
buy the ingredients. Fortunately you know in advance the price per pound of
each ingredient as well as the exact amount you need. The program should
read in the number of ingredients (up to a maximum of 10 ingredients), then
for each ingredient the price per pound. Finally your program should read the
weight necessary for the recipe (for each ingredient in the same order). Your
program should calculate the total cost of these purchases, then display it
with 6 decimal places.
Code:
int weight_correction(double a[],int b)
{
int i,j;
double c,t=0;
for(i=0;i<b;i++)
t=t+a[i];
t=t/b;
for(j=0;j<b;j++)
{
c=t-a[j];
printf("%.1lf\n",c);
}
return 0;
}
void main()
{
int i,n;
double w[50];
printf("enter no. of boxcars =");
scanf("%d",&n);
printf("enter weight of boxcars =");
for(i=0;i<n;i++)
scanf("%lf",&w[i]);
printf("ideal weights are =");
weight_correction(w,n);
}
Example
There are 4 ingredients and they all have a different price per pound: 9.90,
5.50, 12.0, and 15.0. You must take 0.25 lbs of the first, 1.5 lbs of the second,
0.3 lbs of the third and 1 lb of the fourth. It will cost exactly $29.325000.
Input:
4
9.90 5.50 12.0 15.0
0.250 1.5 0.300 1.0
Output:
29.325000
Question 10.
You want to determine the number of cities in a given region that have a
population strictly greater than 10,000. To do this, you write a program that
first reads the number of cities in a region as an integer, and then the
populations for each city one by one (also integers).
Code:
#include<stdio.h>
int main()
{
int n,p,i,count=0;
printf("Enter total no. of cities =");
scanf("%d",&n);
printf("enter the population =");
for(i=1;i<=n;i++)
{
scanf("%d",&p);
if(p>=10000)
count++;
}
printf("%d",count);
return 0;
}
Example
Input
6
1000
5000
15000
4780
0
23590
Output
Question 11.
Create a program that displays on the screen a square of n x n stars, with the
integer n given as an input.
Code:
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("*");
printf("\n");
}
}
Examples
Input:
Output:
*****
*****
*****
*****
*****
Input:
3
Output:
***
***
***
Question 12.
Much of the work of a university administration (in addition to managing
teachers, researchers, students, courses, etc.) is to ensure the proper
functioning of the university and in particular that the accounting job is well
done. Once a year, an annual report of expenditures must be made.
All expenses for the year have been recorded and classified in a multitude of
files and the sum of all these expenses must now be calculated. But no one
knows exactly how many different expenses have been made in the past
year!
Your program will have to read a sequence of positive integers and display
their sum. We do not know how many integers there will be, but the
sequence always ends with the value -1 (which is not an expense, just an
end marker).
Code:
#include<stdio.h>
void main()
{
int x,c=0;
printf("enter expences =");
while(1)
{
scanf("%d",&x);
if(x==-1)
break;
else
c=c+x;
}
printf("total expences =%d",c);
}
Example 1
Input
1000
2000
500
-1
Output
3500
Example 2
Input
-1
Output
Example 3
Input
150
250
350
4500
240
120
-1
Output
5610
Question 13.
In order to be able to better fight various epidemics in a region, the
department of medicine of a major university has launched a large study.
Researchers are interested in how fast an epidemic spreads, and therefore
the speed at which health measures must be put in place. Your program
should first read an integer representing the total population of the area.
Knowing that a person was infected on day 1 and that each infected person
contaminates two new people every day, you must calculate the day at
which the entire population of the area will be infected.
Code:
#include<stdio.h>
int total_days(int a)
{
int c=1,d=1; //d=days
while(1)
{ // 1
c=c*2+c; //1+2=1+2=3
d=d+1; //3*2=3+6=9
if(c>=a) //9*2=9+18=27
break;
}
return(d);
}
void main()
{
int p;
printf("enter population of area =");
scanf("%d",&p);
printf("total days left = %d",total_days(p));
}
Examples
Input:
Output:
Input:
10
Output:
4
Question 14.
We would like you to develop a program capable of making a child play
automatically the game of "more or less" -- the child must try to guess a
secret number. The program should respond to each guess with "it is more"
or "it is less" until the child finds the right number.
Your program must first read an integer indicating the number that the child
will have to find during the game. Next the program should repeatedly read
the player's guesses and display the text "it is more" if the child has
submitted a smaller number or "it is less" if they have submitted a larger
number. Once the correct number is reached, the program should print
"Number of tries needed:" followed by a new line and the integer number of
tries that it took the guesser.
Code:
#include<stdio.h>
void main()
{
int n,a,x,t=0;
printf("enter secret number =");
scanf("%d,&x");
while(1)
{
printf("guess a number between 0 to 50 = ");
scanf("%d",&n);
t=t+1;
if(n==x) //secret number is 33
{
printf("number is correct and total no of tries =%d\n",t);
break;
}
else if(n>x)
printf("it is more\n");
else
printf("it is less\n");
}
}
Example 1
Input
5
1 2 3 4 5
Output
it is more
it is more
it is more
it is more
Number of tries needed:
5
Example 2
Input
10
5 15 8 12 11 10
Output
it is more
it is less
it is more
it is less
it is less
Number of tries needed:
6
Question 15.
University chemists have developed a new process for the manufacturing of
a drug that heals wounds extremely quickly. The manufacturing process is
very lengthy and requires monitoring the chemicals at all times, sometimes
for hours! Entrusting this task to a student is not possible; students tend to
fall asleep or not pay close attention after a while. Therefore you need to
program an automatic device to monitor the manufacturing of the drug. The
device measures the temperature every 15 seconds and provides these
measurement to your program.
Your program should first read two integers representing the minimum and
maximum safe temperatures. Next, your program should continuously read
temperatures (integers) that are being provided by the device. Once the
chemical reaction is complete the device will send a value of -999, indicating
to you that temperature readins are done. For each recorded temperature
that is in the correct range (it could also be equal to the min or max values),
your program should display the text "Nothing to report". But as soon as a
temperature reaches an unsafe level your program must display the text
"Alert!" and stop reading temperatures (although the device may continue
sending temperature values).
Code:
#include<stdio.h>
int check_temp(int a[],int b,int p, int q)
{
int j;
for(j=0;j<b;j++)
{
if(a[j]>=q && a[j]<=p)
printf("nothing to report\n");
else
{
printf("alert\n");
break;
}
}
return 0;
}
void main()
{
int i=0,j,m,n,t[100];
printf("enter minimum and maximum temp. =");
scanf("%d%d",&n,&m);
printf("enter temp. readings =");
while(i>=0)
{
scanf("%d",&t[i]);
if(t[i]==-999)
break;
i++;
}
check_temp(t,i,m,n);
}
Examples
Input:
10 20
15 10 20 0 15 -999
Output:
Nothing to report
Nothing to report
Nothing to report
Alert!
Input:
0 100
15 50 75 -999
Output:
Nothing to report
Nothing to report
Nothing to report
Question 16.
Write a C-program that prints out a word as many times as specified. The
number of repetitions and the word should be given as input to the
program. You may assume that the word has no more than 100 characters
(be sure to also reserve space for the null terminator, \0, though!).
Code:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,t,n;
char str[100];
printf("enter no. of times =");
scanf("%d",&n);
printf("enter word =");
scanf("%s",str);
for(i=0;i<n;i++)
printf("%s\n",str);
}
Examples
Input:
2 Hello
Output:
Hello
Hello
Input:
4 thing
Output:
thing
thing
thing
thing
Question 17.
Your local public library keeps a record of all of its patrons, consisting of
index cards that hold a person's last name followed by their first name (so
that the cards can easily be sorted alphabetically by last name).
Unfortunately a computer error led to incorrectly printed forms last month,
resulting in a number of cards that list the patron's first name followed by
their last name rather than the other way around. Your job is to read these
pairs of first and last names and display them in the correct order (last name
followed by first name). You may assume that each first and last name has at
most 100 characters and does not contain any spaces.
Your program should first read the total number of names (an integer) in
order to know how many index cards need to be processed altogether.
Next, for each index card, your program should read a patron's first name
and last name and then display these names correctly, that is on one line,
the last name followed by one space, followed by the first name. Your
program should print the reversed name immediately after reading the
patron's names (ie, it should not wait until it has read all of the index cards
to begin printing).
Note that, for ease of viewing, the example below shows all of the inputs in
one block and all of the outputs in another block, despite the fact that
programmatically these will be interspersed.
Code:
#include<stdio.h>
void main()
{
int n,i;
char first_name[100],last_name[100];
printf("enter total no. of words =");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%s",&first_name,&last_name);
printf("%s %s\n",last_name,first_name);
}
}
Example
Input:
4
Alan Turing
Ada Lovelace
Donald Knuth
Claude Shannon
Output:
Turing Alan
Lovelace Ada
Knuth Donald
Shannon Claude
Question 18.
At the annual meeting of MOOC fans, participants register on the first day of
the event in order to receive their name tags, brochures and banquet
vouchers. Unfortunately this often results in long lines. In an attempt to
speed things up, there are now two people working the registration desk: one
person who has the registration materials for those fans whose names
contain an odd number of letters, the other for those whose names have an
even number of letters. Your job is to write a C-program that will tell a fan
which line to stand in.
To simplify the program, you may assume that student names are less than
50 characters long and contain no spaces. Your program should output an
integer value (1 or 2) depending on whether the fan should join line 1 (name
has even number of letters) or line 2 (name has odd number of letters).
Code:
#include<stdio.h>
#include<string.h>
void main()
{
char name[50];
int l;
printf("enter a name =");
scanf("%s",&name);
l=strlen(name);
if(l%2==0)
printf("stand in line 1");
else
printf("stand in line 2");
}
Examples
Input:
Sharrock
Output:
Input:
Bonfert
Output:
Question 19.
Your job is to find the length of the longest word in a text with no
punctuation or special characters of any kind - only contains words. To do
so, please write a C-program that takes as a input first the number of words
in a text, followed by all of the words in the text. The output of your program
should be the length of the longest word in the text.
To simplify your program, you can assume that the longest word will not
exceed 100 characters.
Code:
#include<stdio.h>
#include<string.h>
int longest(int a)
{
int m=0;
if(a>m)
m=a;
return(m);
}
void main()
{
char name[100];
int i,n,z,length;
printf("enter no. of words =");
scanf("%d",&n);
printf("enter words =");
for(i=0;i<n;i++)
{
scanf("%s",name);
length=strlen(name);
z=longest(length);
}
printf("longest word's length is = %d",z);
}
Examples
Input:
14
This is a simple example text
we have to find the largest word length
Output:
Question 20.
You are conducting a linguistic study and are interested in finding words that
contain the letter 't' or 'T' in the first half of the word (including the middle
letter if there is one). Specifically, if the first half of the word does contain a
't' or a 'T', your program should output a 1. If the first half does not contain
the letter 't' or 'T', but the second half does, then your program should output
a 2. Otherwise, if there is no 't' or 'T' in the word at all, your program's output
should be -1. You may assume that the word entered does not have more
than 50 letters.
Code:
#include<stdio.h>
#include<string.h>
int check_t_T(char a[],int t,int h)
{
int f,i;
for(i=0;i<=h;i++)
{
if(a[i]=='t' || a[i]=='T')
{
f=0;
return(f);
break;
}
}
for(i=h+1;i<t;i++)
{
if(a[i]=='t' || a[i]=='T')
{
f=1;
return(f);
break;
}
}
}
void main()
{
char word[50];
int a,b,i,l;
printf("enter a word =");
scanf("%s",word);
l=strlen(word); // l = total length
a=l/2; // a = half length
b=check_t_T(word,l,a);
if(b==0)
printf("1");
else if(b==1)
printf("2");
else
printf("-1");
}
Examples
Input:
apple
Output:
-1
Input:
raincoat
Output:
Input:
enter
Output:
Input:
Taylor
Output:
Question 21.
You are still conducting linguistic research! This time, you'd like to write a
program to find out how many letters occur multiple times in a given word.
Your program should read a word from the input and then sort the letters of
the word alphabetically (by their ASCII codes). Next, your program should
iterate through the letters of the word and compare each letter with the one
following it. If these equal each other, you increase a counter by 1, making
sure to then skip ahead far enough so that letters that occur more than twice
are not counted again. You may assume that the word you read from the
input has no more than 50 letters, and that the word is all lowercase
Code:
#include <stdio.h>
#include <string.h>
int repeat_word(char a[],int n)
{
char temp;
int i,j,count=0;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n-1;i++)
{
if(a[i]==a[i+1])
{
count++;
while(i<n-1 && a[i]==a[i+1])
i++;
}
}
return(count);
}
void main()
{
char word[50];
int c,j,l;
printf("Enter a word : ");
scanf("%s",word);
l=strlen(word);
c=repeat_word(word,l);
printf("Number of letters that occur multiple times: %d\n", c);
}
Examples
Input:
apple
Output:
Input:
keeper
Output:
Input:
erroneousnesses
Output:
Input:
taylor
Output:
Question 22.
You are writing a function named "special" that takes three input parameters (integers) and returns the
product of the first number with the sum of the second and third number. What is an appropriate
prototype for this function?
int special;
int special(int);
3. in both places
4. in neither places
Answer : A
Question 24.
On the first input line you are given the number of conversions to be made.
Each of the following lines contains a value to be converted as well as its
unit: m, g or c (for meters, grams or degrees Celsius). There will be a space
between the number and the unit. You should print your output value for each
input line immediately after calculating it (ie, you do not have to wait until
you have read all inputs).
Display the converted values with 6 decimal places, followed by a space and
their unit: ft, lbs or f (for feet, pounds or degrees Fahrenheit). Each
conversion result should be printed on its own line, and you should store and
display all decimal values as doubles.
You may use functions to complete this exercise, but that is not required.
However, you will need to use a comparison operation with characters, for
example:
Code:
#include<stdio.h>
float temp_fer(float c)
{
float f;
f=32+(1.8*c); //f= 32+1.8*c
return(f);
}
float meter_feet(float m)
{
float ft;
ft=0.3048*m; //ft=0.3048 m
return(ft);
}
float gram_pound(float g)
{
float lbs;
lbs=g/453.59237; //lbs=453.59237 gm
return(lbs);
}
void main()
{
char y;
float x;
int n,i;
printf("Enter the no. values :");
scanf("%d",&n);
printf("enter value and its unit(m=meters,g=grams and c=calcius) : \
n");
for(i=0;i<n;i++)
{
scanf("%f",&x);
scanf("%c",&y);
if(y=='m')
printf("%f ft\n",meter_feet(x));
else if(y=='g')
printf("%f lbs\n",gram_pound(x));
else if(y=='c')
printf("%f f\n",temp_fer(x));
else
printf("invalid measurement");
}
}
Example
The following entry indicates that there are four values to be converted. The
first is 10 meters, which, when converted, gives approximately 32.808 feet.
The second is 1245.243 grams, or about 2.745761 pounds, the third is 37.2
degrees Celsius, or 98.96 degrees Farenheit, and the fourth is 23 grams, or
0.050715 pounds.
Input
4
10 m
1245.243 g
37.2 c
23 g
Output
32.808000 ft
2.745761 lbs
98.960000 f
0.050715 lbs
Question 25.
The goal of this problem is to find the smallest integer in a list of numbers.
To help you with this task, please write a function called min() that finds and
returns the smallest amongst two integers (be sure to also write a prototype
for this function). The function thus takes two integers as input and returns
the smallest of the two. This function will use an if statement with a
condition that contains either "less than" or "greater than".
Next, please use min() in your main function to work your way through an
entire list of numbers in order to find its minimum. The first number you read
gives the number of elements in the list of integers under consideration. You
will then read the integer values, using min() to keep only the smallest
integer read at each step. In the end, please print out the smallest integer in
the list.
Code:
#include<stdio.h>
int min(int n[],int s)
{
int m;
int i;
m=n[0];
for(i=1;i<s;i++)
{
if(m>n[i])
{
m=n[i];
}
}
return(m);
}
void main()
{
int x,z,k,j,n,a[50];
printf("how many numbers do you want to enter = ");
scanf("%d",&n);
printf("enter numbers =");
for(j=0;j<n;j++)
scanf("%d",&a[j]);
z=min(a,n);
printf("manimum number is =%d",z);
}
Example
Input
10
4 3 6 2 6 8 9 8 5 4
Output