Uploaded by yo_nnes

Worksheet Ans

advertisement
1) Find the area of a triangle, A, having base and height as b and h respectively as a floating
data types of inputs
#include <iostream>
using namespace std;
int main() {
int height, base;
float ans;
//ans may come in fractions
cout<<"Enter height and base : ";
cin>>height>>base;
ans= (0.5)*height*base;
//area of triangle formula
cout<<"Area of triangle is : "<<ans;
return 0;
2) Write a C++ program that converts a temperature value accepted from the user in degree
Celsius in to faharanite scale. Hint F=9/5*C+32
#include <iostream>
using namespace std;
int main() {
float fahren, celsius;
cout << "Enter the temperature in celsius\n";
cin >> celsius;
// convert celsius to fahreneheit
// Multiply by 9, then divide by 5, then add 32
fahren =(9.0/5.0) * celsius + 32;
cout << celsius <<"Centigrade is equal to " << fahren <<"Fahrenheit";
return 0;
3) A C++ program that runs as an arithmetic operation on two inputs as Addition , Subtraction,
Multiplication , Division, Modulus (Remainder) of the division integers and performs to the
following x and y from the keyboard as
#include <iostream>
using namespace std;
int main() {
float fahren, celsius;
cout << "Enter the temperature in celsius\n";
cin >> celsius;
// convert celsius to fahreneheit
// Multiply by 9, then divide by 5, then add 32
fahren =(9.0/5.0) * celsius + 32;
cout << celsius <<"Centigrade is equal to " << fahren <<"Fahrenheit";
return 0;
4) A C++ program that manipulate on the logical operation of teo inputs given from the key as
integer data types and operate as; Bitwise Logical AND operation, Bitwise Logical OR
operation, Bitwise Logical XOR operation
<?php
/*
* Ignore the top section,
* it is just formatting to make output clearer.
*/
$format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)'
. ' %3$s (%4$2d = %4$04b)' . "\n";
echo <<<EOH
--------result
--------EOH;
--------value
---------
-- --------op test
-- ---------
/*
* Here are the examples.
*/
$values = array(0, 1, 2, 4, 8);
$test = 1 + 4;
echo "\n Bitwise AND \n";
foreach ($values as $value) {
$result = $value & $test;
printf($format, $result, $value, '&', $test);
}
echo "\n Bitwise Inclusive OR \n";
foreach ($values as $value) {
$result = $value | $test;
printf($format, $result, $value, '|', $test);
}
echo "\n Bitwise Exclusive OR (XOR) \n";
foreach ($values as $value) {
$result = $value ^ $test;
printf($format, $result, $value, '^', $test);
}
?>
5) Write program to execute the following structure to you. xxxxxx,x,xxxx,x,xxxxxx
#include <stdio.h>
int main()
{
int i,j;
for(i=0;i<5;++i)
{
for(j=5;j>0;--j)
{
printf("X");
}
printf("\n");
}
return 0;
}
6) Assuming there is 7.481 gallons in cubic foot write a program that asks the user to enter a
number of gallons and display the equivalent in cubic feet
#include <iostream>
using namespace std;
int main()
{
float gallons, cufeet;
cout << "\nEnter quantity in gallons : ";
cin >> gallons;
cufeet = gallons / 7.481;
cout << "Equivalent in cublic feet is " << cufeet << endl;
return 0;
}
7) Write a program that generates the following table.
1990 135
1991 7290
1992 11300
1993 16200
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << 1990 << setw(8) << 135 << endl
<< 1991 << setw(8) << 7290 << endl
<< 1992 << setw(8) << 11300 << endl
<< 1993 << setw(8) << 16200 << endl;
return 0;
}
8) Write a program that generates the following output using 10 as initial input and
recommended operators.
10
20
19
#include <iostream>
using namespace std;
int main()
{
int var = 10;
cout << var << endl;
var *= 2;
cout << var-- << endl;
cout << var << endl;
return 0;
//
//
//
//
var is 10
var becomes 20
displays var, then decrements it
var is 19
}
9) On a certain day the British pound as equivalent to $1.487 us dollar, he French frame was $0.12,
the German Dutch was $0.584 and Japanese yen was $0.00955. Write a program that allows
the user to enter amount in dollar and display this value converted to these four monetary
units
#include <iostream>
using namespace std;
int main()
{
float ammount=0, british = 1.487, franc = 0.172, deutsche = 0.584, yen = 0.00955;
cout << "Enter the ammount in US Dollar == ";
cin >> ammount;
cout << "British == " << ammount*british << endl
<< "French == " << ammount*franc << endl
<< "German == " << deutsche*ammount << endl
<< "Japanese == " << ammount*yen << endl;
return 0;
}
10) Write a program that assigns value of 5 for both variable, a and b post and pre increment the
variables and assign them for variables d and e;
I.e. initialize as
a=5;
b=5;
d=a++;
e=++b;
Display the result of a, b, d, e
Repeat the above for decrement operator for post and pre cases:
Ans: https://www.geeksforgeeks.org/pre-increment-and-post-increment-in-c/
11) Accept an alphabetical character from the key board and convert(east) it to equivalent ASCII
value.
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
12) Accept a number from the key board and find its square root as a double data type.
package MyPackage;
public class SquareRoot2 {
public static void main(String args[])
{
double a = 100;
System.out.println(Math.sqrt(a));
// Input positive value, Output square root of x
double b = -81.00;
System.out.println(Math.sqrt(b));
// Input negative value, Output NaN
double c = 0.0/0;
// Input NaN, Output NaN
System.out.println(Math.sqrt(c));
double d = 1.0/0;
// Input positive infinity, Output positive infinity
System.out.println(Math.sqrt(d));
double e = 0.0;
// Input positive Zero, Output positive zero
System.out.println(Math.sqrt(e));
}
}
Download