-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7.cpp
More file actions
168 lines (142 loc) · 2.8 KB
/
day7.cpp
File metadata and controls
168 lines (142 loc) · 2.8 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
//WAP to display multiplication table upto 10
[200~#include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter a number:";
cin>>n;
for(int i=1; i<=10; ++i){
cout<<n<<"*"<<i<<"="<<n*i<<endl;
}
return 0;
}
//WAP to add only positive nos.
#include<iostream>
using namespace std;
int main(){
int number;
int sum=0;
cout<<"enter a number:";
cin>>number;
while(number>=0){
sum+=number;
cout<<"enter a number:";
cin>>number;
}
cout<<"\n the sum is"<<sum<<endl;
return 0;
}
//WAP to print all odd nos. til n
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter a number:";
cin>>n;
for (int i-1; i<=n; i++){
if(n%2==0){
continue;
}
cout<<i<<endl;
}
return 0;
}
//WAP to find whether an alphabet ia a vowel or a consonan#include<iostream>
using namespace std;
int main(){
char c;
cout<<"enter a alphabet:";
cin>>c;
switch(c)
{
case'a':
cout<<"It is a vowel"<<endl;
break;
case'e':
cout<<"It is a vowel"<<endl;
break;
case'i':
cout<<"It is a vowel"<<endl;
break;
case'o':
cout<<"It is a vowel"<<endl;
break;
case'u':
cout<<"It is a vowel"<<endl;
break;
}
return 0;
}
#include<stdio.h>
int main()
{
int a = 5;
a = 1, 2, 3;
printf("%d", a);
return 0;
}
//(Priority for the values assigned to any variable is given from left to right)
#include<stdio.h>
int main()
{
int a;
a = (1, 2, 3);
printf("%d", a);
return 0;
}
//(Priority for the values inside a brackets () assigned to any variable is
//given from right to left)
#include<stdio.h>
int main()
{
int x = 2;
(x & 1) ? printf("true") : printf("false");
return 0;
}
//Ans. False
//(As & is a unary operator we have to assume all decimal values to
//binary(0's and 1's)
//210 = 000000102
//Now we go for condition (00000010 & 00000001)
//Clearly, condition false as it leads to 0 when multiplied)
#include <iostream>
using namespace std;
int main() {
int n;
long double factorial = 1.0;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
return 0;
}