Skip to content

Commit 9aa9b06

Browse files
Coding BlocksCoding Blocks
authored andcommitted
updated code
1 parent 5cfbf66 commit 9aa9b06

20 files changed

+603
-15
lines changed

01 Getting Started with C++/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 0 additions & 1 deletion
This file was deleted.

02 Programming Fundamentals-I/square_root.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@
22
using namespace std;
33

44

5-
int main(){
5+
int main() {
66

77
// Give a Number N
88
// Square root of the number without using any predefined function
9-
10-
9+
int N;
10+
int P;
11+
cin >> N >> P;
12+
13+
14+
float ans = 0;
15+
float inc = 1.0;
16+
17+
for (int times = 0; times <= P; times++) {
18+
19+
// Finalise the correct digit for the current place
20+
while (ans * ans <= N) {
21+
ans = ans + inc;
22+
}
23+
ans = ans - inc ;
24+
// Updates the increment for the next position
25+
inc = inc / 10;
26+
}
27+
28+
cout << ans << endl;
1129

1230
return 0;
1331
}

02 Programming Fundamentals-I/while_do_while.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
using namespace std;
33

44

5-
int main(){
5+
int main() {
66

77
int cliff_end = 10;
88
int x = 10;
99

10-
while(x<cliff_end){
10+
while (x < cliff_end) {
1111
x = x + 1;
12-
cout<<"Taking 1 step reaching :"<<x <<endl;
12+
cout << "Taking 1 step reaching :" << x << endl;
1313
}
1414

15-
cout<<"Final X "<<x<<endl;
15+
cout << "Final X " << x << endl;
1616

17-
if(x==cliff_end){
18-
cout<<"We are standing at cliff edge!"<<endl;
17+
if (x == cliff_end) {
18+
cout << "We are standing at cliff edge!" << endl;
1919
}
2020

21-
else if(x>cliff_end){
22-
cout<<"Falling from the cliff!"<<endl;
21+
else if (x > cliff_end) {
22+
cout << "Falling from the cliff!" << endl;
2323
}
2424

2525

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
5+
int main() {
6+
7+
//Comma Operator
8+
int a, b, c;
9+
10+
// Assignment Operator
11+
a = 10;
12+
b = 20;
13+
c = 30;
14+
15+
//Logical Operator
16+
if (c > a and c > b) {
17+
cout << "C is largest" << endl;
18+
}
19+
20+
21+
// Ternary Operator
22+
int x = c % 2 == 0 ? 1 : 0;
23+
cout << x << endl;
24+
c % 2 == 0 ? cout << "Even" : cout << "Odd";
25+
cout << endl;
26+
27+
// Bitwise Operator
28+
x = 5;
29+
int y = 7;
30+
cout << "AND " << (x & y) << endl;
31+
cout << " OR " << (x | y) << endl;
32+
cout << "XOR " << (x ^ y) << endl;
33+
34+
// Shift Operator
35+
x = x << 2;
36+
37+
cout << x << endl; // 20
38+
cout << (y >> 1) << endl; //3
39+
// Unary Operator
40+
41+
// Address Of
42+
cout << (&x) << endl;
43+
44+
//Post Increment / Decrement Operator
45+
a = 10;
46+
int z = a++; //z = 10, a = 11
47+
cout << z << endl; // 10
48+
z = ++a; // a = 12, z = 12
49+
cout << z << endl; // 12
50+
51+
// Compound Assingment Operator
52+
a = 10;
53+
a *= 3;
54+
cout << "A after multiply " << a << endl; // 30
55+
a %= 5;
56+
cout << "A after modulo " << a << endl; //0
57+
58+
b = 5;
59+
b <<= 1 ;
60+
cout << "B after left shift" << b << endl; //10
61+
62+
63+
64+
return 0;
65+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*Write program to calculate digits,
2+
whitespace,alphabets and other characters terminated by $ */
3+
4+
#include<iostream>
5+
using namespace std;
6+
7+
8+
int main() {
9+
10+
int digits = 0;
11+
int alphabets = 0;
12+
int spaces = 0;
13+
int other = 0;
14+
15+
char ch;
16+
ch = cin.get();
17+
18+
while (ch != '$') {
19+
20+
if (ch >= '0' and ch <= '9') {
21+
digits++;
22+
}
23+
else if ( (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')) {
24+
alphabets++;
25+
}
26+
else if (ch == ' ' or ch == '\n' or ch == '\t') {
27+
spaces++;
28+
}
29+
else {
30+
other++;
31+
}
32+
ch = cin.get();
33+
}
34+
35+
cout << "Digits " << digits << endl;
36+
cout << "alphabets " << alphabets << endl;
37+
cout << "spaces " << spaces << endl;
38+
cout << "Others " << other << endl;
39+
40+
41+
return 0;
42+
}

03 Programming Fundamentals-II/cinget.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using namespace std;
33

44

5-
int main(){
5+
6+
int main() {
67

78
// Problem - Read a list of characters (sentence)
89
// cin.get()
@@ -12,9 +13,9 @@ int main(){
1213
//reads the first char
1314
ch = cin.get(); //this method reads any single char including spaces/newlines from the input buffer
1415

15-
while(ch!='.'){
16+
while (ch != '.') {
1617
//Print the last character that we have read
17-
cout<<ch;
18+
cout << ch;
1819
// Update my char in the memory (read the next character)
1920
//reads the rest of the characters
2021
ch = cin.get();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
newprog
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Pattern Problem
3+
N = 5
4+
- For ith line print i numbers
5+
- odd line start with 0, and value alternates btw 0 and 1
6+
- even line start with 1, and value alternate btw 0 and 1
7+
- print n lines
8+
9+
0
10+
10
11+
010
12+
1010
13+
01010
14+
101010
15+
*/
16+
17+
#include<iostream>
18+
using namespace std;
19+
20+
21+
int main() {
22+
23+
int n;
24+
cin >> n;
25+
26+
for (int i = 1; i <= n; i++) {
27+
int val = i % 2 == 0 ? 1 : 0;
28+
29+
//Print i values in ith line
30+
for (int cnt = 1; cnt <= i; cnt++) {
31+
cout << val;
32+
val = 1 - val; // val = 1, val = 0, val = 1, ....
33+
}
34+
cout << endl;
35+
}
36+
37+
return 0;
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main() {
5+
6+
//Switch Case
7+
/* Design a Menu - where pressing a button gives you the corresponding item
8+
9+
B - Burger
10+
M - Maggi
11+
P - Pizza
12+
C - Coke
13+
D - Dosa
14+
.
15+
.
16+
and so on!
17+
*/
18+
char ch;
19+
cin >> ch;
20+
int pizza_orders = 0;
21+
22+
switch (ch) {
23+
case 'B' : cout << "Burger" << endl;
24+
break;
25+
26+
case 'm' :
27+
case 'M' : cout << "Maggi" << endl;
28+
break;
29+
30+
case 'p' :
31+
case 'P' : cout << "Pizza" << endl;
32+
pizza_orders++;
33+
34+
break;
35+
36+
default : cout << "Item not available!" << endl;
37+
}
38+
39+
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)