-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.cpp
More file actions
201 lines (166 loc) · 3.41 KB
/
day1.cpp
File metadata and controls
201 lines (166 loc) · 3.41 KB
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
200
201
#include <iostream>
using namespace std;
int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
int fact(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
return n * (fact(n - 1));
}
int main()
{
// conditional statements
/*
// int a = 5;
// cout << typeid(a).name() << endl;
// cout << "Hello World1" << endl;
char ch;
cout << "enter a character : ";
cin >> ch;
// we can print the ascii characters based on the alphabets or vice-versa
// if (ch >= 'a' && ch <= 'z')
if (ch >= 65 && ch <= 98)
{
cout << "lowercase" << endl;
// cout << ch << endl;
}
// else
// {
// cout << "uppercase" << endl;
// }
*/
// loops
/*
// 3 loops for / while/ do while, same can be done by each just usage and effectiveness of each is different
int count = 1;
cout << "using while loop :" << endl;
while (count <= 10)
{
cout << count << endl;
count++;
}
// needs all values to be true to execute once
cout << "using do-while loop :" << endl;
int co1 = 1;
do
{
cout << co1 << endl;
co1++;
} while (co1 <= 5);
// executes at least one time before verifying the condition
cout << "using for loop :" << endl;
for (int i = 1; i <= 5; i++)
{
cout << i << endl;
}
// most efficient loop, all condition check
*/
// sum if n odd numbers
/*
int inp;
cout << "Enter a number: ";
if (!(cin >> inp))
{
cout << "Enter a valid number" << endl;
return 1;
}
int sum = 0;
for (int i = 1; i <= inp; i++)
{
if (i % 2 != 0)
{
sum += i;
}
}
cout << "Sum of odd numbers up to " << inp << " is " << sum << endl;
// using while loop
int winp;
cout << "Enter a number: ";
cin >> winp;
int sum1 = 0;
int i = 1;
while (i <= winp)
{
if (i % 2 != 0)
{
sum1 += i;
}
i++;
}
cout << "Sum of odd numbers up to " << winp << " is " << sum1 << endl;
*/
// prime number check
int num;
cout << "Enter a number to check" << endl;
cin >> num;
bool isPrime = true;
// for (int i = 2; i <= num - 1; i++)
// {
// if (num % i == 0)
// {
// isPrime = false;
// break;
// }
// isPrime = true;
// }
// if (isPrime)
// {
// cout << "is prime" << endl;
// }
// else
// {
// cout << "not prime" << endl;
// }
// optimized solution
for (int i = 2; i * i < num; i++)
{
// iterate upto small number
if (num % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
cout << "is prime" << endl;
}
else
{
cout << "not prime" << endl;
}
// star pattern printing
for (int i = 0; i <= 5; i++)
{
int n = 5;
for (int j = 0; j <= n; j++)
{
cout << "*";
}
// cout<<"*";
cout << endl;
}
// factorial code using c++ recursion
int num1;
cout << "Enter the number of terms: ";
cin >> num1;
cout << "Fibonacci Series: ";
for (int i = 0; i < num1; i++)
{
cout << fib(i) << " ";
}
cout << endl;
cout << "Factorial Series: " << fact(num1) << endl;
return 0;
}