forked from dscmbm/GSoCPrimer-Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCP answers
221 lines (173 loc) · 2.96 KB
/
CP answers
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
ADD Competitive Programming answers here.
#include<iostream>
using namespace std;;
Q.1
int main(){
int a,b,x;
cout<<"Enter number: ";
cin>>a>>b;
x=a*b;
cout<<"multi. is: "<<x<<endl;
return 0;
}
Q.2
int main(){
int a;
cout<<"Enter number: ";
cin>>a;
if(a%2==0){
cout<<"number is even"<<endl;
}
else{
cout<<"Number is odd"<<endl;
}
return 0;
}
// Q3.
int fact(int n){
if(n==0){
return 1;
}
else if(n<0){
return -1;
}
else{
return n*fact(n-1);
}
}
int main(){
cout<<fact(6)<<endl;
}
//Q4.
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string s;
cin>>s;
int count;
int len=s.size();
for(int i=0; i<len; i++){
if(s[i]=='a' || s[i]=='e' || s[i]=='i'|| s[i]=='o'|| s[i]=='u'){
count++;
}
}
cout<<"Number of vowels is: "<<count;
}
Q5.
#include<stdio.h>
int max(int arr[],int n){
int max,i;
max=arr[0];
for(i=1; i<n; i++)
{
if(max<arr[i]){
max=arr[i]; }
}
cout<<"maximum of array is : "<<max<<endl;
}
//Q6.
#include <iostream>
using namespace std;
int min(int arr[], int n) {
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main() {
int n;
int arr[100];
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int minimum = min(arr, n);
cout << "The minimum of the array is " << minimum << endl;
return 0;
}
int main(){
int n;
int arr[100];
cout<<"enter the size of arry";
cin>>n;
cout<<"enter the element of arry";
for(int i=0;i<n;i++){
cin>>arr[i];
}
max(arr,n);
}
//Q.7
int main(){
int a,b,temp;
cout<<"enter numbers: ";
cin>>a>>b;
cout<<"before: "<<endl;
cout<<a<<" "<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"after: "<<endl;
cout<<a<<" "<<b<<endl
}
//Q8.
int main(){
int year;
cout<<"enter year: "<<endl;
cin>>year;
if(year%4==0){
cout<<"leap year"<<endl;
}
else if(year%100==0){
cout<<"not a leap year"<<endl;
}
else if(year%400==0){
cout<<"leap year"<<endl;
}
else{
cout<<"leap year"<<endl;
}
}
//Q9.
void reverseStr(string& str)
{
int n = str.length();
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
}
int main()
{
string str = "gdsc";
reverseStr(str);
cout << str;
return 0;
}
// Q10.
int main(){
int a,b,c;
cin>>a>>b>>c;
if(a>b && b>c){
cout<<"number 1 is maximum "<<a;
}
else if(b>c && b>a){
cout<<"number 2 is maximum "<<b;
}
else{
cout<<"number 3 is maximum "<<c;
}
}
//MCQ 1
A) O(N)
// MCQ2
B) Stack
//MCQ3
B)exit the loop
//MCQ4
A) sorted
//MCQ5
B) The number of operations it performs