-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2 Conditional Assignment Answer.txt
199 lines (138 loc) · 3.79 KB
/
2 Conditional Assignment Answer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Q1 - Write a program which takes the values of length and breadth from user and check if it is
a square or not.
#include<iostream>
using namespace std;
int main(){
int length;
cout<<"Enter the length: ";
cin>>length;
int breadth;
cout<<"Enter the breadth: ";
cin>>breadth;
if(length==breadth){
cout<<"square :";
}else{
cout<<"It is a rectangular: ";
}
return 0;
}
Q2 - Write a program to print absolute value of a number entered by the user
#include<iostream>
using namespace std;
int main(){
int num;
cout<<"Enter a number: ";
cin>>num;
if(num<0){
num=num*(-1);
cout<<"Absolute number: "<<num<<endl;
}
return 0;
}
Q3- Write a program to take input from user for Cost Price (C.P.) and Selling Price(S.P.) and
calculate Profit or Loss.
#include<iostream>
using namespace std;
int main(){
int cost_price;
cout<<"Enter the cost price: ";
cin>>cost_price;
int selling_price;
cout<<"Enter the selling price: ";
cin>>selling_price;
int profit = selling_price - cost_price;
int loss = cost_price - selling_price;
if(profit>0){
cout<<"You are in the profit: "<<profit<<endl;
}else{
cout<<"You loose: "<<loss<<endl;
}
return 0;
}
Q4- Write a program to print positive number entered by the user, if user enters a negative
number, it is skipped.
#include<iostream>
using namespace std;
int main(){
int num;
cout<<"Enter a number: ";
cin>>num;
if(num>0){
cout<<num<<endl;
}else{
cout<<"The number is nagetive skipped: "<<endl;
}
return 0;
}
Q5- Create a calculator using switch statement to perform addition, subtraction, multiplication
and division.
#include<iostream>
using namespace std;
int main(){
char operators;
cout<<"Enter the operators for calculate (+ - * /): ";
cin>>operators;
int num1;
cout<<"Enter a num1 value: ";
cin>>num1;
int num2;
cout<<"Enter a num2 value: ";
cin>>num2;
switch(operators){
case '+':
cout<< num1 << " + " << num2 <<" = "<< num1 + num2 <<endl;
break;
case '-':
cout<< num1 << " - " << num2 <<" = "<< num1 - num2 <<endl;
break;
case '*':
cout<< num1 << " * " << num2 <<" = "<< num1 * num2 <<endl;
break;
case '/':
cout<< num1 << " / " << num2 <<" = "<< num1 / num2 <<endl;
break;
default:
cout<<"Enter the given operators: + - * / "<<endl;
}
return 0;
}
Q6. Write a program to calculate marks to grades . Follow the conversion rule as given below :
Sample Input : Enter Your Marks: 98
Sample Output : Your Grade is A+
(Medium)
Cracking the Coding Interview in C++ - Foundation
100 – 90
90 – 80
80 – 70
70 – 60
60 – 50
50 – 40
40 – 30
30 – 0
#include<iostream>
using namespace std;
int main(){
int marks;
cout<<"Enter your marks: ";
cin>>marks;
if(marks>=90){
cout<<" A+ ";
}else if(marks>=80){
cout<<" A ";
}else if(marks>=70){
cout<<" B+ ";
}else if(marks>=60){
cout<<" B ";
}else if(marks>=50){
cout<<" C ";
}else if(marks>=40){
cout<<" D ";
}else if(marks>=30){
cout<<" E ";
}else if(marks>=0){
cout<<" F ";
}else{
cout<<marks<<endl;
}
return 0;
}