0% found this document useful (0 votes)
98 views

Name Uzma Ashiq Reg 1441 - 320001

The document contains sample code for several programming questions. It includes code to: 1) Get user input for name, address, age, weight, and height and display it. 2) Print even numbers from 1 to 10. 3) Convert pounds to kilograms based on a given conversion rate. 4) Get lap times from a user and calculate the fastest, slowest, and average times.

Uploaded by

Zulfiqar Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Name Uzma Ashiq Reg 1441 - 320001

The document contains sample code for several programming questions. It includes code to: 1) Get user input for name, address, age, weight, and height and display it. 2) Print even numbers from 1 to 10. 3) Convert pounds to kilograms based on a given conversion rate. 4) Get lap times from a user and calculate the fastest, slowest, and average times.

Uploaded by

Zulfiqar Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name uzma ashiq

Reg 1441_320001

Q3.   Get input of your name, address, age in years, weight and height from
keyboard and display the information using unformatted I/O (String I/O).
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

main ()

char sname[10], addr[10], sage[10], swt[10], sht[10];

int age=0;

float wt=0.0, ht=0.0;

printf("\nEnter name....");

gets(sname);

printf("enter address.....");

gets(addr);

printf("\nEntere age....");

scanf("%d",age);

printf("\nEnter weight....");

scanf("%d",&wt);

printf("\nEnter height....");

scanf("%d",&ht);

itoa(age,sage,10);

sprintf(swt,"%-2f",wt);
sprintf(sht,"%-2f",ht);

puts("\n");

puts(sname);

puts(addr);

puts(sage);

puts(swt);

puts(sht);

Output:

Enter name…. sadia

Enter address…..islamabad

Enter age….18

Enter weight….43

Enter height….5

Q2.   Write a program that prints the even no. from 1 to 10.


#include <stdio.h>

#include <stdlib.h>

#include<conio.h>

int main()

{
int i=2;

printf("Even Numbers from 1 t0 10 are:\n");

printf("%d",i);

i=i+2; printf("%d",i);

i=i+2; printf("%d",i);

i=i+2; printf("%d",i);

i=i+2; printf("%d",i);

return 0;
}

Result :

Even Numbers from 1 t0 10 are:

2 4 6 8 10

Q5.   Pounds to Kilograms
One pound is equivalent to 0.454 kilograms. Write a program that asks the user to
enter the mass of an object in pounds and then calculates and displays the mass
of the object in kilograms.
ONE_POUND_IN_KILOGRAMS = .454

object_mass_pounds = float(input('\nEnter mass of an object: ')) # 3.4

object_mass_kilograms = object_mass_pounds * ONE_POUND_IN_KILOGRAMS

print('Object in kilograms =', format(object_mass_kilograms, ',.2f'), end='\n\n') # 1434500.90

RESULT
Enter mass of an object: 5

Object in kilograms = 2.27

Q7.   Write a program that asks the user to enter the number of times that they
have run around a racetrack, and then uses a loop to prompt them to enter the
lap time for each of their laps. When the loop finishes, the program should display
the time of their fastest lap, the time of their slowest lap, and their average lap
time.

Get input as string

s = input('Enter the lap times separated by space')

lap_time = []

#Split string on whitespace, convert each string to float and save to laptime string

for item in s.split():

lap_time.append(float(item))

#Calculate fastest and slowest by min and max function

fastest = min(lap_time)

slowest = max(lap_time)

#Calculate average by divinding sum of lap times by no of items in list

average = sum(lap_time)/len(lap_time)

print('Fastest ', fastest)

print('Slowest ', slowest)

print('Average ', average)

RESULT
Enter the lap times separated by space1.0 2.0 3.0 4.0 5.0

Fastest 1.0

Slowest 5.0

Average 3.0

You might also like