/**
C program that prompts user to enter two floating point
type values and print the product of two values upt to 3
decimal places
*/
//program1.c
//header files
#include
#include
//main function
int main()
{
//declare two double type variables
double num1;
double num2;
//set a double variable,product=0
double product=0;
printf(\"Enter num1 and num2: \");
//prompt for num1 and num2 values from keyboard
scanf(\"%lf %lf\",&num1,&num2);
//calculate product
product=num1*num2;
//print product of two numbers and limit product to 3 decimal places
printf(\"The product of %lf and %lf is %5.3lf \ \",num1,num2,product);
getch();
return 0;
}
Sample output:
Enter num1 and num2: 2.25 3.25
The product of 2.250000 and 3.250000 is 7.313
------------------------------------------------------------------------------------
//2.
/**
C program that prompts user to enter integer values
until user enters 999 to stop reading. Then prit
number of even and odd to console.
*/
//program2.c
//header files
#include
#include
//main function
int main()
{
//set SENTINAL=999;
int SENTINAL=999;
//declare integer variable
int value;
//set even and odd=0
int even=0;
int odd=0;
printf(\"Enter a vlaue or 999 to stop reading input \");
scanf(\"%d\",&value);
//read until value is not 999
while(value!=SENTINAL)
{
//check if value is even
if(value%2==0)
//increment even by one
even++;
else
//otherwise increment odd by one
odd++;
printf(\"Enter a vlaue or 999 to stop reading input \");
//read a value
scanf(\"%d\",&value);
}
printf(\"Even numbers : %d\ \",even);
printf(\"Odd numbers : %d\ \",odd);
//pause program output on console
getch();
return 0;
}
Sample output:
Enter a vlaue or 999 to stop reading input 1
Enter a vlaue or 999 to stop reading input 2
Enter a vlaue or 999 to stop reading input 3
Enter a vlaue or 999 to stop reading input 4
Enter a vlaue or 999 to stop reading input 5
Enter a vlaue or 999 to stop reading input 6
Enter a vlaue or 999 to stop reading input 7
Enter a vlaue or 999 to stop reading input 8
Enter a vlaue or 999 to stop reading input 9
Enter a vlaue or 999 to stop reading input 10
Enter a vlaue or 999 to stop reading input 999
Even numbers : 5
Odd numbers : 5
------------------------------------------------------------------------------------------------------------
//3.
/**
C program that prompts length in inches and prints the length
in miles, yards, feet to console.
*/
//program3.c
//header files
#include
#include
//function prototype
void measure(int inch,int &m,int &y,int &f, int &i);
//main function
int main()
{
int miles,yards,feet,inches=0;
int len;
printf(\"Enter the length (inches): \");
//prompt for len
scanf(\"%d\",&len);
//calling measure method
measure(len,miles,yards,feet,inches);
//print len
printf(\"Total length : %d\ \", len);
//print miles,yards, feet and inches
printf(\"Miles : %d\ \", miles);
printf(\"Yards : %d\ \", yards);
printf(\"Feet : %d\ \", feet);
printf(\"Inches : %d\ \", inches);
//pause program output on console
getch();
return.
C program that prompts user to enter two floating point t.pdf
1. /**
C program that prompts user to enter two floating point
type values and print the product of two values upt to 3
decimal places
*/
//program1.c
//header files
#include
#include
//main function
int main()
{
//declare two double type variables
double num1;
double num2;
//set a double variable,product=0
double product=0;
printf("Enter num1 and num2: ");
//prompt for num1 and num2 values from keyboard
scanf("%lf %lf",&num1,&num2);
//calculate product
product=num1*num2;
//print product of two numbers and limit product to 3 decimal places
printf("The product of %lf and %lf is %5.3lf ",num1,num2,product);
getch();
return 0;
}
Sample output:
Enter num1 and num2: 2.25 3.25
The product of 2.250000 and 3.250000 is 7.313
------------------------------------------------------------------------------------
//2.
/**
C program that prompts user to enter integer values
2. until user enters 999 to stop reading. Then prit
number of even and odd to console.
*/
//program2.c
//header files
#include
#include
//main function
int main()
{
//set SENTINAL=999;
int SENTINAL=999;
//declare integer variable
int value;
//set even and odd=0
int even=0;
int odd=0;
printf("Enter a vlaue or 999 to stop reading input ");
scanf("%d",&value);
//read until value is not 999
while(value!=SENTINAL)
{
//check if value is even
if(value%2==0)
//increment even by one
even++;
else
//otherwise increment odd by one
odd++;
printf("Enter a vlaue or 999 to stop reading input ");
//read a value
scanf("%d",&value);
}
printf("Even numbers : %d ",even);
3. printf("Odd numbers : %d ",odd);
//pause program output on console
getch();
return 0;
}
Sample output:
Enter a vlaue or 999 to stop reading input 1
Enter a vlaue or 999 to stop reading input 2
Enter a vlaue or 999 to stop reading input 3
Enter a vlaue or 999 to stop reading input 4
Enter a vlaue or 999 to stop reading input 5
Enter a vlaue or 999 to stop reading input 6
Enter a vlaue or 999 to stop reading input 7
Enter a vlaue or 999 to stop reading input 8
Enter a vlaue or 999 to stop reading input 9
Enter a vlaue or 999 to stop reading input 10
Enter a vlaue or 999 to stop reading input 999
Even numbers : 5
Odd numbers : 5
------------------------------------------------------------------------------------------------------------
//3.
/**
C program that prompts length in inches and prints the length
in miles, yards, feet to console.
*/
//program3.c
//header files
#include
#include
//function prototype
void measure(int inch,int &m,int &y,int &f, int &i);
//main function
int main()
{
int miles,yards,feet,inches=0;
4. int len;
printf("Enter the length (inches): ");
//prompt for len
scanf("%d",&len);
//calling measure method
measure(len,miles,yards,feet,inches);
//print len
printf("Total length : %d ", len);
//print miles,yards, feet and inches
printf("Miles : %d ", miles);
printf("Yards : %d ", yards);
printf("Feet : %d ", feet);
printf("Inches : %d ", inches);
//pause program output on console
getch();
return 0;
}
/**
The method measure that takes inches and four address (reference) of variables
m,y,f and i and calcualtes the miles, yards,feet and inches.
*/
void measure(int inch,int &m,int &y,int &f, int &i)
{
int total;
total = inch;
m = total/ 63360;
total = total%63360;
y = total /36;
total = total%36;
f = total/12;
total = total%12;
i = total;
}
sample outout:
5. Enter the length (inches): 835
Total length : 835
Miles : 0
Yards : 23
Feet : 0
Inches : 7
Solution
/**
C program that prompts user to enter two floating point
type values and print the product of two values upt to 3
decimal places
*/
//program1.c
//header files
#include
#include
//main function
int main()
{
//declare two double type variables
double num1;
double num2;
//set a double variable,product=0
double product=0;
printf("Enter num1 and num2: ");
//prompt for num1 and num2 values from keyboard
scanf("%lf %lf",&num1,&num2);
//calculate product
product=num1*num2;
//print product of two numbers and limit product to 3 decimal places
printf("The product of %lf and %lf is %5.3lf ",num1,num2,product);
getch();
return 0;
6. }
Sample output:
Enter num1 and num2: 2.25 3.25
The product of 2.250000 and 3.250000 is 7.313
------------------------------------------------------------------------------------
//2.
/**
C program that prompts user to enter integer values
until user enters 999 to stop reading. Then prit
number of even and odd to console.
*/
//program2.c
//header files
#include
#include
//main function
int main()
{
//set SENTINAL=999;
int SENTINAL=999;
//declare integer variable
int value;
//set even and odd=0
int even=0;
int odd=0;
printf("Enter a vlaue or 999 to stop reading input ");
scanf("%d",&value);
//read until value is not 999
while(value!=SENTINAL)
{
//check if value is even
if(value%2==0)
//increment even by one
even++;
else
7. //otherwise increment odd by one
odd++;
printf("Enter a vlaue or 999 to stop reading input ");
//read a value
scanf("%d",&value);
}
printf("Even numbers : %d ",even);
printf("Odd numbers : %d ",odd);
//pause program output on console
getch();
return 0;
}
Sample output:
Enter a vlaue or 999 to stop reading input 1
Enter a vlaue or 999 to stop reading input 2
Enter a vlaue or 999 to stop reading input 3
Enter a vlaue or 999 to stop reading input 4
Enter a vlaue or 999 to stop reading input 5
Enter a vlaue or 999 to stop reading input 6
Enter a vlaue or 999 to stop reading input 7
Enter a vlaue or 999 to stop reading input 8
Enter a vlaue or 999 to stop reading input 9
Enter a vlaue or 999 to stop reading input 10
Enter a vlaue or 999 to stop reading input 999
Even numbers : 5
Odd numbers : 5
------------------------------------------------------------------------------------------------------------
//3.
/**
C program that prompts length in inches and prints the length
in miles, yards, feet to console.
*/
//program3.c
//header files
#include
8. #include
//function prototype
void measure(int inch,int &m,int &y,int &f, int &i);
//main function
int main()
{
int miles,yards,feet,inches=0;
int len;
printf("Enter the length (inches): ");
//prompt for len
scanf("%d",&len);
//calling measure method
measure(len,miles,yards,feet,inches);
//print len
printf("Total length : %d ", len);
//print miles,yards, feet and inches
printf("Miles : %d ", miles);
printf("Yards : %d ", yards);
printf("Feet : %d ", feet);
printf("Inches : %d ", inches);
//pause program output on console
getch();
return 0;
}
/**
The method measure that takes inches and four address (reference) of variables
m,y,f and i and calcualtes the miles, yards,feet and inches.
*/
void measure(int inch,int &m,int &y,int &f, int &i)
{
int total;
total = inch;
m = total/ 63360;
9. total = total%63360;
y = total /36;
total = total%36;
f = total/12;
total = total%12;
i = total;
}
sample outout:
Enter the length (inches): 835
Total length : 835
Miles : 0
Yards : 23
Feet : 0
Inches : 7