Вы находитесь на странице: 1из 20

1

LAB

Mariam Fazal Abbasi

SP19-MCS-033

Sir.Shafiq Ahmed

22-Sep-2019
1. If you want the user to input an integer value into your program for
a variable named number what are two lines of code you could write
to ask the user to do it and to input the value into your program?

Ans:
#include<iostream>

using namespace std;

main()

int number;

cout<<"Enter any number:\n";

cin>>number;

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

}
Output

2. What is \n called and what purpose does it serve?

Ans:
The \n stands for New Line. It is use for move cursor from above to new line.

3. How would you write?

cout << "Hello, ";

cout << first_ name;

cout << "!\n";

as a single line of code?

Ans:
#include<iostream>
using namespace std;

main()

cout<<"Hello, "<<"first_name!\n";

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

Output

4. What is a literal?

Ans:
A literal is refers to a fixed values in programming that cannot be alter in any
program.
5. What kinds of literals are there?

Ans:
There are five types of literals that are following:

1. Integer literals
2. Boolean literals
3. Character literals
4. String literals
5. Floating Point Literals

6 .What effect do tabs, spaces, and new lines have on the program?

Ans:
Tabs and spaces create spaces between words and new line move cursor from
above line to next line.

7. What are typical sizes for a char, an int, and a double?

Ans:
Data types Sizes
 Char 1 byte
 Int 4 bytes
 Double 8 bytes

8. What measures do we use for the size of small entities in memory,


such as ints and strings?

Ans:
In programing every literals has its own size whether it will be int, char, double or
float. The size define the amount of space require by the literal to store it in a memory.
A byte is used to measure the size of small entities. One byte is equivalent to the 8 bits.
9. What is the difference between = and ==?

Ans:
= is the assignment operator. b = 1 will set the variable b equal to the value 1.
== is the equality operator. It returns true if the left side is equal to the right side, and
returns false if they are not equal.

10. What is an initialization and how does it differ from an


assignment?

Ans:
Initialization is giving a variable its initial value. It differs from assignment, because
assignment is giving a variable a new value. You can assign an initial value to something
else, but you can't initialize something that's been assigned.

11. Write a program that doesn't do anything, but declares a number


of variables with legal and illegal names (such as int double = 0; ), so
that you can see how the compiler reacts.

Ans:
If we use illegal names for different variables it gives us error.

For example:

#include<iostream>

using namespace std;

main()

int double=0;

float class;

char name='A';
cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

If we use legal names for variable than it doesn’t show any output for these variables.

For example:

#include<iostream>

using namespace std;

main()

int a=0;

float b;

char name='A';

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";


return 0;

Output

12. Write a program in C++ that converts from miles to kilometers.


Your program should have a reasonable prompt for the user to enter
a number of miles. Hint: There are 1.609 kilometers to the mile.

Ans:
#include<iostream>

using namespace std;


main()

float miles, kilometers;

cout<<"Enter miles :"<<endl;

cin>>miles;

kilometers = miles/1.609;

cout<<"Kilometers :"<<kilometers<<endl;

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

Output
13. Write a program that prompts the user to enter two integer
values. Store these values in int variables named val1 and val2. Write
your program to determine the smallest, largest, sum, difference,
product, and ratio of these values and report them to the user.

Ans:
#include<iostream>

using namespace std;

main()

int val1,val2;

cout<<"Enter two integer values:\n";

cin>>val1>>val2;

if (val1>val2)

cout<<val2<<" is smaller than "<<val1<<" and "<<val1<<" is bigger than


"<<val2<<" \n";

if (val1<val2)

cout<<val1<<" is smaller than "<<val2<<" and "<<val2<<" is bigger than


"<<val1<<" \n";

cout<<"The sum of "<<val1<<" and "<<val2<<" is


"<<val1<<'+'<<val2<<"="<<val1+val2<<" \n";
cout<<"The difference of "<<val1<<" and "<<val2<<" is "<<val1<<'-
'<<val2<<"="<<val1-val2<<" \n";

cout<<"The product of "<<val1<<" and "<<val2<<" is


"<<val1<<'*'<<val2<<"="<<val1*val2<<" \n";

cout<<"ratio of "<<val1<<" and "<<val2<<" is "<<val1<<':'<<val2<<"="<<val1/val2<<"


\n";

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

Output
14. Modify the program above (Q13) to ask the user to enter floating-
point values and store them in double variables. Compare the outputs
of the two programs for some inputs of your choice. Are the results
the same? Should they be? What's the difference?

Ans:
#include<iostream>

using namespace std;

main()

float val1,val2;

cout<<"Enter two values:\n";

cin>>val1>>val2;

if (val1>val2)

cout<<val2<<" is smaller than "<<val1<<" and "<<val1<<" is bigger than


"<<val2<<" \n";

if (val1<val2)

cout<<val1<<" is smaller than "<<val2<<" and "<<val2<<" is bigger than


"<<val1<<" \n";

cout<<"The sum of "<<val1<<" and "<<val2<<" is


"<<val1<<'+'<<val2<<"="<<val1+val2<<" \n";
cout<<"The difference of "<<val1<<" and "<<val2<<" is "<<val1<<'-
'<<val2<<"="<<val1-val2<<" \n";

cout<<"The product of "<<val1<<" and "<<val2<<" is


"<<val1<<'*'<<val2<<"="<<val1*val2<<" \n";

cout<<"ratio of "<<val1<<" and "<<val2<<" is "<<val1<<':'<<val2<<"="<<val1/val2<<"


\n";

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

}
Output

The value of both programs are different with changing the data types.

15. Write a program to test an integer value to determine if it is odd


or even. As always, make sure your output is clear and complete. In
other words, don't just output “yes” or “no." Your output should
stand alone, like "The value 4 is an even number”.

Ans:
#include<iostream>

using namespace std;

main()

int num;

cout<<"Enter a number\n";

cin>>num;

if(num%2==0)

cout<<"The number "<<num<<" is even\n";

else

cout<<"The number "<<num<<" is odd\n";

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

}
Output

16. Write a program that generates the following output:

10

20

19

Use an integer constant for the 10, an arithmetic assignment operator


to generate the 20, and a decrement operator to generate the 19.

Ans:
#include<iostream>
using namespace std;

main()

int a =10;

cout<<a<<endl;

int b=a+10;

cout<<b<<endl;

int c=--b;

cout<<c<<endl;

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

}
Output

17. Write a program that generates the following table:

Year Result

-----------------

1990 135

1991 7290

1992 11300

1993 16200
Use a single cout statement for all output .Also, revise the program to
show the same table by using cin object .Which takes all the values
from user and draw the same table.

Ans:
#include<iostream>

using namespace std;

main()

cout<<"Years\tResult\n";

cout<<"-------------\n";

cout<<"1990 \t 135 \n1991 \t 7290\n1992 \t 11300\n1993 \t 11300\n";

cout<<"Mariam Fazal Abbasi\n"<<"SP19-MCS-033\n"<<"MCS";

return 0;

}
Output

--------------- The end ---------------

Вам также может понравиться