Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Introduction to Programming GENG 200

Homework 1
Problem 1.
A. Draw a flowchart to get a number from the user that represents the total number of
seconds. Then find and display: hours, minutes and remaining seconds.
B. Write a C program to solve part A above. (Note that in this part you have to use / and
% operators, do not use any subtraction operation).

Start

Get number of the total


seconds, T.

Compute:
Hours = T / (3600)
Minutes = (T%3600)/60
Remaining seconds = (T
%3600)%60

End

B
/*
Name
: Hasna Saif Alshamsi
ID
: 201404246
Section : sec No.62
Homeworh# : 1
*/
#include <stdio.h>
int main()
{
//1.Declaring all the variables.
int T,Houres,Minutes,Remaining_Seconds;
//2.get the total number of seconds, T.
printf("Enter the total number of seconds: ");
scanf("%d", &T);
//3.calculate the number of Hours, Minutes and the Remaining Seconds.
Houres = T/(3600);
Minutes = (T%3600)/60;
Remaining_Seconds = (T%3600)%60;
//4.display hours, minutes and the Remaining Seconds.
printf("houres = %d\n", Houres);
printf("minutes = %d\n", Minutes);
printf("Remaining Seconds = %d\n", Remaining_Seconds);

return 0;
}

Problem 2. (This problem is from our book, ch2)

#include <stdio.h>
#define Gravity 9.80
#define Efficiency 90/100
int main()
{
//1.Declaring all the variables.

int h;
double f;
double gravityPower;
double electricalPower;
//2.enter the values of the haight dam and the water flow.
printf("enter the hight of the dam in meters:");
scanf("%d",&h);
printf("enter the water flow in Meters^3/Sec :");
scanf("%lf", &f);

//3.calculate the values of gravity power and electrical Power.


gravityPower = f*1000*Gravity*h;
electricalPower = gravityPower*Efficiency/1e6;
//4.display the electrical Power.
printf("\nelectrical Power =%f in Megawatts(MW)\n",electricalPower);
return 0;
}

Problem 3. (This problem is from our book, ch2)

#include <stdio.h>
int main()
{
//1.Declaring all the variables.
int n,m;
double side1,side2,hypotenuse;

//2.enter positve values for n and m (m > n), and display them.
printf("Enter positive integer for n: ");
scanf("%d",&n);

printf("Enter positive integer for m (m > n): ");


scanf("%d", &m);
//3.calculate the values of the three variables side 1, side 2 and
hypotenuse.
side1 = m * m - n * n;
side2 = 2 * m * n;
hypotenuse = m * m + n * n;

//4.display side 1, side 2 and hypotenuse.


printf("\nside1 = %f, side2 = %f, hypotenuse = %f\n",
side1,side2,hypotenuse);

return 0;
}

Problem 4. Write a program that gets an integer number between 0 and 999. Then the
program should display individual digits of the number and their sum.

Hint: Consider the number 123. To get ones digit use: 123%10 = 3 and to remove the
digit from the number use: 123/10 = 12. Repeat to get tens and hundreds digits.

#include <stdio.h>
int main()
{
//1.Declaring all the variables.
int
int
int
int

num;
x1,x2,x3;
Remaining_digit;
sum;

//2.eneter an integer number.


printf("eneter an integer number:");
scanf("%d", &num);
//3.find the the ones digit, tens digit, hundreds digit, the
//and the sum of all of them.

Remaining digit

x1 = num %10;
Remaining_digit = num /10;
x2 = Remaining_digit %10;
x3 = Remaining_digit /10;
sum = x1 + x2 + x3;
//4.display the ones digit, tens digit, hundreds digit,and the sum of all of
them.
printf("\nones digit = %d\n", x1);
printf("tens digit = %d\n", x2);
printf("hundreds digit = %d\n", x3);
printf("\nThe sum of the number one digit ten digit and hundred digit =
%d\n", sum);

return 0;
}

You might also like