You are on page 1of 15

NATIONAL UNIVERSITY OF COMPUTER AND

EMERGING SCIENCES
DEPARTMENT: SOFTWARE ENGINEERING

PROGRAMMING FUNDAMENTAL
LAB
ASSIGNMENT# 01

SUBMITTED BY: Ahmed Ali


ROLL NUMBER: 22P-9318
SECTION: BS (SE) 1-B
INSTRUCTOR NAME: SIR MOHAMMAD USMAN
SUBMISSION DEADLINE: 22/10/2022
A DEPARTMENT OF COMPUTER SCIENCE
Problem: 1
Write a program that converts Celsius to Fahrenheit through a
function.
● Ask the user to enter the celsius value in main function.
● Create a function named celsiusToFahrenheit(int celsius) which
would take celsius
value as input paramter.
● The function will calculate the Fahrenheit value and return it to
main.
● The program should print value of Fahrenheit in main
CODE:
#include<stdio.h>
int celsiusToFahrenheit(int celsius);
int main()
{
int celsius,fahrenheit;
printf("Enter the value in celsius:");
scanf("%d",&celsius);
fahrenheit=celsiusToFahrenheit(celsius);
printf("Fahrenheit value is:%d",fahrenheit);
return 0;
}
int celsiusToFahrenheit(int celsius)
{
int fahrenheit;
fahrenheit=(celsius*9/5)+32;
return fahrenheit;
}
RESULT

PROBLEM: 2
Write a program that check whether a year is leap year or not?
create a function named isLeap has an formal parameter, year,
determines whether the year is a leap year, or not and print the
message to that effect. A year is a leap year if it is divisible by 4
but is not divisible by 100 except when divisible by 400.You
should ask user to enter year in main and pass its value to
function.

CODE:
#include<stdio.h>
int isLeap(int year);
int main()
{
int year;
printf("Enter any year: ");
scanf("%d",&year);
if(isLeap(year))
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
return 0;
}
int isLeap(int year)
{
if (year%400 == 0)
return 1;
if (year%100 == 0)
return 0;
if (year%4 == 0)
return 1;
return 0;
}
RESULT
PROBLEM: 3
Write a program that prompts the user to enter the total number of
cookies, the number of cookies in a box, and the number of cookie
boxes in a container. The program then outputs the number of
Boxes and the number of containers to ship the cookies. Note that
each box must contain the specified number of cookies, and each
container must contain the specified number of boxes. If the last
box of cookies contains less than the number of specified cookies,
you can discard it and output the number of leftover cookies.
Similarly, if the last container contains less than the number of
specified boxes, you can discard it and output the number of
leftover boxes.
CODE:
#include <stdio.h>
int main()
{
int total_cookies, cookies_in_a_box, cookie_boxes_in_a_container;
printf("Enter total number of cookies: ");
scanf("%d", &total_cookies);
printf("Enter the number of cookies in a box: ");
scanf("%d", &cookies_in_a_box);
printf("Enter the number of cookie boxes in a container: ");
scanf("%d", &cookie_boxes_in_a_container);
int num_boxes = total_cookies / cookies_in_a_box;
int num_containers = num_boxes / cookie_boxes_in_a_container;
int leftover_cookies = total_cookies % cookies_in_a_box;
int leftover_boxes = num_boxes % cookie_boxes_in_a_container;
printf("Number of Boxes: %d\n", num_boxes);
printf("Number of Containers: %d\n", num_containers);
printf("Leftover Cookies: %d\n", leftover_cookies);
printf("Leftover Boxes: %d\n", leftover_boxes);
return 0;
}

RESULT

PROBLEM: 4
Write a program that reads magnitude from the user and displays
the appropriate descriptor as part of a meaningful message. For
example, if the user enters 5.5 then your program should indicate
that a magnitude 5.5 earthquake is considered to be a moderate
earthquake.
CODE:
#include<stdio.h>
int main()
{
float magn;
printf("Enter Magnitude:");
scanf("%f", &magn);
if(magn<2.0)
{
printf("micro");
}
else if(magn>=2.0 && magn<=3.0)
{
printf("Very Minor");
}
else if(magn>=3.0 && magn<=4.0)
{
printf("Minor");
}
else if(magn>=4.0 && magn<=5.0)
{
printf("Light");
}
else if(magn>=5.0 && magn<=6.0)
{
printf("Moderate");
}
else if(magn>=6.0 && magn<=7.0)
{
printf("Strong");
}
else if(magn>=7.0 && magn<=8.0)
{
printf("Major");
}
else if(magn>=8.0 && magn<=10.0)
{
printf("Great");
}
else if(magn>=10.0)
{
printf("Meteoric");
}
return 0;
}

RESULT:
PROBLEM: 5
Write a program that examines three variables—x, y, and z—and
prints the largest odd number among them. If none of them are
odd, it should
print a message to that effect.
Note: You have to take three values from user.
CODE:
#include <stdio.h>
int main()
{
int x, y, z;
printf("Enter value for x: ");
scanf("%d", &x);
printf("Enter value for y: ");
scanf("%d", &y);
printf("Enter value for z: ");
scanf("%d", &z);
if (x % 2 == 1 && y % 2 == 1 && z % 2 == 1)
{
if (x > y && x > z)
printf("%d is the largest odd number.", x);
else if (y > z)
printf("%d is the largest odd number.", y);
else
printf("%d is the largest odd number.", z);
}
else if (x % 2 == 1 && y % 2 == 1)
{
if (x > y)
printf("%d is the largest odd number.", x);
else
printf("%d is the largest odd number.", y);
}
else if (x % 2 == 1 && z % 2 == 1)
{
if (x > z)
printf("%d is the largest odd number.", x);
else
printf("%d is the largest odd number.", z);
}
else if (y % 2 == 1 && z % 2 == 1)
{
if (y > z)
printf("%d is the largest odd number.", y);
else
printf("%d is the largest odd number.", z);
}
else
printf("No odd number among x, y and z");
return 0;
}
RESULT

PROBLEM: 6
Write a C Function to check a triangle is equilateral, isosceles or
scalene. Your program should ask the user to input x, y, z values
Note:
An equilateral triangle is a triangle in which all three sides are
equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.
CODE:
#include <stdio.h>
int checkTriangle(int, int, int );
int main()
{
int x, y, z;
printf("Input value for x: ");
scanf("%d", &x);
printf("Input value for y: ");
scanf("%d", &y);
printf("Input value for z: ");
scanf("%d", &z);

int result = checkTriangle(x, y, z);


if (result == 1)
printf("Equilateral Triangle");
else if (result == 2)
printf("Isosceles Triangle");
else if (result == 3)
printf("Scalene Triangle");
return 0;
}

int checkTriangle(int a, int b, int c)


{
if (a==b && b==c)
return 1;
else if (a==b || b==c || c==a)
return 2;
else
return 3;
}
RESULT:

PROBLEM: 7
Suppose that the cost of sending an international fax is calculated
as follows:
Service charges $3.00; $.20 per page for the first 10 pages; and
$0.10 for each additional page. Design a function that takes the
number of pages to be faxed as a parameter. The Function then
uses the number of pages to be faxed to calculate and return the
amount due.
Ask user to enter number of pages and pass it to function
CODE:
#include<stdio.h>
float fax_price(int page);
int main()
{

int page;
float price;
printf("Enter number of pages: ");
scanf("%d",&page);
price=fax_price(page);
printf("The total cost is %.2f",price);
return 0;
}
float fax_price(int page)
{
float price;
if(page<=10)
price=3.00+(0.20*page);
else
price=3.00+(0.20*10)+(0.10*(page-10));
return price;
}
RESULT:

END OF
ASSIG
NMENT

You might also like