Skip to content

Commit 546823f

Browse files
authored
Merge pull request #438 from mirajkarsaurabh/mirajkarsaurabh-patch-2-1
Calculator in C++
2 parents ebac5ee + 751ab1a commit 546823f

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

CalculatorInC++.c++

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// C++ program to create calculator
2+
3+
#include <iostream>
4+
5+
using namespace std;
6+
7+
8+
// Main program
9+
10+
int main()
11+
12+
{
13+
14+
char oper;
15+
16+
float a, b;
17+
18+
// It allow user to enter the operands
19+
20+
cout<<"Enter two operands: ";
21+
22+
cin>> a >> b;
23+
24+
// It allows user to enter operator i.e. +, -, *, /
25+
26+
cout<<"Enter operator: ";
27+
28+
cin>> oper;
29+
30+
// Switch statement begins
31+
32+
switch (oper)
33+
34+
{
35+
36+
// If operator is '+'
37+
38+
case '+':
39+
40+
cout << a + b;
41+
42+
break;
43+
44+
// If operator is '-'
45+
46+
case '-':
47+
48+
cout << a - b;
49+
50+
break;
51+
52+
// If operator is '*'
53+
54+
case '*':
55+
56+
cout << a * b;
57+
58+
break;
59+
60+
// If operator is '/'
61+
62+
case '/':
63+
64+
cout << a / b;
65+
66+
break;
67+
68+
// If any other operator display error message
69+
70+
default:
71+
72+
cout << "Error! Incorrect operator";
73+
74+
break;
75+
76+
}
77+
78+
return 0;
79+
80+
}

SeparateEvenAndOddNumbers.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void swap(int * x, int * y){
5+
int t = * x;
6+
* x = * y;
7+
* y = t;
8+
}
9+
10+
void segregate(int array[], int n){
11+
int left = 0, right = n - 1;
12+
while (left < right){
13+
while (array[left] % 2 == 0 && left < right)
14+
left++;
15+
while (array[right] % 2 == 1 && left < right)
16+
right--;
17+
if (left < right){
18+
swap( & array[left], & array[right]);
19+
left++;
20+
right--;
21+
}
22+
}
23+
}
24+
25+
int main(){
26+
27+
int array[100], n, i;
28+
cout << "Enter number of elements: ";
29+
cin >> n;
30+
cout << "\nEnter elements: ";
31+
32+
for (i = 0; i < n; i++)
33+
cin >> array[i];
34+
35+
cout << "Original array: ";
36+
for (int i = 0; i < n; i++)
37+
cout << array[i] << " ";
38+
39+
segregate(array, n);
40+
cout << "\nArray after divided: ";
41+
42+
for (int i = 0; i < n; i++)
43+
cout << array[i] << " ";
44+
45+
return 0;
46+
47+
}

SortAnUnsortedArrayInAWaveForm.c++

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
5+
void swap(int * p, int * q){
6+
int temp = * p;
7+
* p = * q;
8+
* q = temp;
9+
}
10+
11+
void array_in_wave(int array[], int n){
12+
sort(array, array + n);
13+
for (int i = 0; i < n - 1; i += 2)
14+
swap( & array[i], & array[i + 1]);
15+
}
16+
17+
int main(){
18+
int array[100], n, i;
19+
cout << "Enter number of elements: ";
20+
cin >> n;
21+
cout << "\nEnter elements: ";
22+
23+
for (i = 0; i < n; i++)
24+
cin >> array[i];
25+
26+
cout << "Original array: ";
27+
28+
for (int i = 0; i < n; i++)
29+
cout << array[i] << " ";
30+
31+
array_in_wave(array, n);
32+
33+
cout << "\nWave form of the array is: ";
34+
35+
for (int i = 0; i < n; i++)
36+
cout << array[i] << " ";
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)