Skip to content

Commit 815ec44

Browse files
authored
Merge pull request #690 from Mayuri-cell/master
Arithmetic , Geometric progression .
2 parents d47fa0a + 8f0a801 commit 815ec44

File tree

2 files changed

+374
-0
lines changed

2 files changed

+374
-0
lines changed

Arrays/Missing_integer.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Title : User will enter the unsorted array .
2+
You will need to sort it and has to
3+
find out the first missing positive integer from the sorted array .
4+
*/
5+
#include<iostream>
6+
#include<math.h>
7+
#include<stdlib.h>
8+
using namespace std;
9+
int main()
10+
{
11+
int n ,j=0, i,k=1 , val;
12+
cout<<" Enter the total number of elements "<<endl;
13+
cin>>n;
14+
int a[n];
15+
cout<<" Enter the elements : \n"<<endl;
16+
for(i=0;i<n;i++)
17+
{ cin>>a[i]; }
18+
19+
for(i=0;i<n;i++)
20+
{ for(j=0;j<n-1;j++)
21+
if(a[j]>a[j+1])
22+
{ val=a[j];
23+
a[j]=a[j+1];
24+
a[j+1]=val; }
25+
}
26+
cout<<" Sorted array : \n"<<endl;
27+
for(i=0;i<n;i++)
28+
{
29+
cout<<a[i]<<" ";
30+
}
31+
cout<<" \n"<<endl;
32+
cout<<" Missing element : ";
33+
if(a[n-1]<=0)
34+
{
35+
cout<<"1";
36+
37+
}
38+
else {
39+
40+
for(i=0;i<n;i++)
41+
{
42+
if(a[i]>0)
43+
{
44+
for(j=0+i, k=1;j<=n && k<=n+1;k++,j++)
45+
{
46+
if(a[j]==k)
47+
{
48+
continue ;
49+
}
50+
else
51+
{
52+
53+
cout<<k;
54+
exit(0);
55+
} }
56+
} }
57+
}}
58+
59+
60+
61+
62+
63+
64+
/*
65+
Enter the total number of elements
66+
5
67+
Enter the elements :
68+
69+
12
70+
-43
71+
-5
72+
0
73+
2
74+
Sorted array :
75+
76+
-43 -5 0 2 12
77+
78+
Missing element : 1
79+
*/

General Questions/AP_GP.cpp

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
/* Title : To find whether the given series format is following AP or GP .
2+
Description : User need to provide the terms present and also need to mention
3+
how many number of terms user is providing .
4+
You can also find the ' Mean' , ' Sum ' and the nth the term if
5+
it is following any one of the of progression .
6+
Need to enter atleast three minimun number .
7+
***** For A.P and G.P , here the common difference , common ratio should be greater than 1 .******
8+
9+
*/
10+
#include<iostream>
11+
#include<stdlib.h>
12+
#include<math.h>
13+
using namespace std ;
14+
15+
int main()
16+
{ int opt ;
17+
float s;
18+
cout<<" **** Welcome *** "<<endl;
19+
cout<<" Arithematic Progression : Common difference between two adjacent terms is constant ."<<endl;
20+
cout<<" Geometric Progression : Common ratio between two adjacent terms is constant .\n( For G.P here we can perform the operations for common difference greater than 1 only.) \n "<<endl;
21+
do
22+
{
23+
int i=0,j=0 ,n ;
24+
float sum=0, mean ,nth ,d ,k, s=1;
25+
26+
float first_term, AP ,GP;
27+
int count=1 , co=0;
28+
cout<<"\n Enter the number elements of the given series you wish to enter : \n **** You need to enter atleast 3 terms **** \n"<<endl; /* User need tor provide minimum three terms . Otherwise system will exit */
29+
cin>>n;
30+
float a[n];
31+
cout<<"\n Enter the elements : \n"<<endl;
32+
for(i=0;i<n;i++)
33+
{ cin>>a[i] ; }
34+
35+
AP=a[1]-a[0];
36+
GP=a[1]/a[0];
37+
38+
first_term=a[0];
39+
// To define the operation of A.P and G,P:
40+
41+
for(i=0;i<n-1;i++)
42+
{
43+
// a. AP operations :
44+
d=a[i+2]-a[i+1];
45+
46+
if (AP==d)
47+
{ count++ ;
48+
49+
50+
if (count==n-1)
51+
{
52+
do {
53+
cout<<"\n Given series is in Arithematic Progression " <<endl;
54+
cout<<"\n 1.Mean \n 2.Sum \n 3.To find the nth element of the given series \n 4.Exit : "<<endl;
55+
cin>>opt;
56+
57+
58+
switch(opt)
59+
{
60+
case 1 : cout<<"\n Mean of the given A.P series : "<<endl;
61+
i=0 , sum=0;
62+
while(i<n)
63+
{
64+
sum=sum+a[i];
65+
i++;
66+
}
67+
mean=sum/n;
68+
cout<<" Arithematic Mean : "<<mean<<endl;
69+
break ;
70+
case 2 :
71+
cout<<"\n Sum of the first "<< n<< " AP elements entered : "<<endl;
72+
sum=0;
73+
sum=(n*((2*first_term) + (n - 1)*(AP)))/2;
74+
cout<<sum<<endl;
75+
break ;
76+
case 3 : cout<<" Enter the nth element : "<<endl;
77+
cin>>nth;
78+
nth= (first_term + (nth - 1)*AP );
79+
cout<<" Nth element of the given AP : "<<nth<<endl;
80+
break ;
81+
case 4 : exit(0);
82+
break ;
83+
default : cout<<"\n Invalid option , Try again later !"<<endl;
84+
break;
85+
}
86+
cout<<"\n Wish to continue the AP operations on the given series ?\n1.Yes 2.No \n"<<endl; /* 1. To continue or press any to stop */
87+
cin>>opt;
88+
}while(opt==1);
89+
}
90+
}
91+
}
92+
//2.GP Operations :
93+
for(i=0;i<n-2;i++)
94+
{
95+
// a. AP operations :
96+
d=a[i+2]/a[i+1];
97+
if (GP==d)
98+
{ co++ ;
99+
100+
if(co==n-2)
101+
{
102+
do
103+
{
104+
cout<<" \n Given series is in Geometric Progression " <<endl;
105+
cout<<"\n 1.Mean \n 2.Sum \n 3.To find the nth element of the given series \n 4.Exit : "<<endl;
106+
cin>>opt;
107+
108+
109+
switch(opt)
110+
{
111+
case 1 : cout<<"\n Enter the number of elements to find the geometric mean : \n"<<endl;
112+
float t;
113+
cin>>t;
114+
cout<<"\n the first "<<t<<" given G.P series elements have the "<<endl;
115+
i=0;
116+
while(i<n)
117+
{
118+
s=s*a[i];
119+
i++; }
120+
mean=pow(s, (1/t));
121+
cout<<" Geometrical Mean : "<<mean<<endl;
122+
break ;
123+
case 2 : cout<<"Enter the number of elements to find the mean of them : "<<endl;
124+
int tt;
125+
cin>>tt;
126+
cout<<"\n Sum of the first "<<tt<<"GP elements : "<<endl;
127+
sum=first_term*((pow(GP, tt))-1)/(GP-1);
128+
cout<<abs(sum)<<endl;
129+
cout<<" If the series belongs to infinity : "<<endl;
130+
cout<<" sum : "<<abs(first_term/(1 - GP))<<endl;
131+
break ;
132+
case 3 : cout<<" Enter the nth element : "<<endl;
133+
cin>>nth;
134+
nth=first_term*pow(GP , nth-1);
135+
cout<<" Nth term of the given G.P : "<<nth<<endl;
136+
break ;
137+
case 4 : exit(0);
138+
break ;
139+
default : cout<<"\n Invalid option , Try again later !"<<endl;
140+
break;
141+
}
142+
143+
cout<<" \n Wish to continue the GP operations on the given series ?\n1.Yes 2.No \n"<<endl; /* 1. To continue or press any to stop */
144+
cin>>opt;
145+
}while(opt==1);
146+
}
147+
}
148+
}
149+
cout<<"\n Do you wish to continue further or not \n1.Yes 2.No \n"<<endl; /* 1. To continue or press any to stop */
150+
cin>>opt;
151+
} while(opt==1);
152+
153+
}
154+
155+
156+
/*
157+
158+
**** Welcome ***
159+
Arithematic Progression : Common difference between two adjacent terms is constant .
160+
Geometric Progression : Common ratio between two adjacent terms is constant .
161+
162+
163+
Enter the number elements of the given series you wish to enter :
164+
**** You need to enter atleast 3 terms ****
165+
166+
3
167+
Enter the elements :
168+
1
169+
1.02
170+
1.04
171+
172+
Given series is in Arithematic Progression
173+
174+
1.Mean
175+
2.Sum
176+
3.To find the nth element of the given series
177+
4.Exit :
178+
1
179+
180+
Mean of the given A.P series :
181+
Arithematic Mean : 1.02
182+
Wish to continue the AP operations on the given series ?
183+
1.Yes 2.No
184+
185+
1
186+
187+
Given series is in Arithematic Progression
188+
189+
1.Mean
190+
2.Sum
191+
3.To find the nth element of the given series
192+
4.Exit :
193+
2
194+
195+
Sum of the first 3 AP elements entered :
196+
3.06
197+
198+
Wish to continue the AP operations on the given series ?
199+
1.Yes 2.No
200+
201+
1
202+
203+
Given series is in Arithematic Progression
204+
205+
1.Mean
206+
2.Sum
207+
3.To find the nth element of the given series
208+
4.Exit :
209+
3
210+
211+
Enter the nth element :
212+
5
213+
214+
Nth element of the given AP : 1.08
215+
216+
Wish to continue the AP operations on the given series ?
217+
1.Yes 2.No
218+
219+
2
220+
221+
Do you wish to continue further or not
222+
1.Yes 2.No
223+
1
224+
225+
Enter the number elements of the given series you wish to enter :
226+
**** You need to enter atleast 3 terms ****
227+
228+
4
229+
230+
Enter the elements :
231+
2
232+
4
233+
8
234+
16
235+
236+
Given series is in Geometric Progression
237+
238+
1.Mean
239+
2.Sum
240+
3.To find the nth element of the given series
241+
4.Exit :
242+
1
243+
244+
Enter the number of elements to find the geometric mean :
245+
4
246+
247+
the first 4given G.P series elements have the
248+
Geometrical Mean : 5.65685
249+
250+
Wish to continue the GP operations on the given series ?
251+
1.Yes 2.No
252+
253+
1
254+
Given series is in Geometric Progression
255+
256+
1.Mean
257+
2.Sum
258+
3.To find the nth element of the given series
259+
4.Exit :
260+
2
261+
262+
Enter the number of elements to find the mean of them :
263+
3
264+
265+
Sum of the first 3 GP elements :
266+
14
267+
268+
If the series belongs to infinity :
269+
sum : 2
270+
271+
Wish to continue the GP operations on the given series ?
272+
1.Yes 2.No
273+
274+
1
275+
Given series is in Geometric Progression
276+
277+
1.Mean
278+
2.Sum
279+
3.To find the nth element of the given series
280+
4.Exit :
281+
3
282+
283+
Enter the nth element :
284+
5
285+
286+
Nth term of the given G.P : 32
287+
Wish to continue the GP operations on the given series ?
288+
1.Yes 2.No
289+
290+
2
291+
Do you wish to continue further or not
292+
1.Yes 2.No
293+
2
294+
295+
*/

0 commit comments

Comments
 (0)