In: Computer Science
User is asked to enter a series of numbers. That input will stop when user enters -9999. Find a maximum number from that series and a minimum number from that series. Output the location of Maximum number and minimum number.
Write a C++ program that asks the user to repeatedly input positive numbers until -1 is pressed. Your program should print the second largest number and the count of even and odd numbers.
Write a C++ program that asks the user to enter a positive number N, and print the following pattern. The pattern is shown for N = 5.
12345
23451
34521
45321
54321
Write a program that takes a set of N integers and count the frequency of ODD digits appeared in these N integers.
Sample Input:
How Many Numbers you want to input: 3
Number 0: 7809
Number 1: 1127
Number 2: 8381
Output:
Frequency of appeared ODD digits:
7
The digits 7809. 5127. 8381. So the count of
these ODD digits is 7.
1)
#include<iostream>
using namespace std;
int main()
{
int x,min=999999,max=-99999;
int minpos=0,maxpos=0,count=0;
cin>>x;
while(x!=-9999)
{
count++;
if(x > max)
{
max = x;
maxpos = count;
}
if( x < min)
{
min = x;
minpos = count;
}
cin>>x;
}
cout<<"Maximum number is :
"<<max<<" And its location is :
"<<maxpos<<endl;
cout<<"Minimum number is :
"<<min<<" And its location is :
"<<minpos<<endl;
return 0;
}
Input:
15
13
33
99
55
-9999
Output :
Maximum number is : 99 And its location is : 4
Minimum number is : 13 And its location is : 2
2)
#include<iostream>
using namespace std;
int main()
{
int x,max2=-999999,max=-999999;
int numodd=0,numeven=0;
cin>>x;
while(x!=-1)
{
if(x%2 == 0)
numeven++;
else
numodd++;
if(x > max)
{
max = x;
}
if( x < max && x >
max2)
{
max2 = x;
}
cin>>x;
}
cout<<"Second largest number is :
"<<max2<<endl;
cout<<"Count of odd numbers is :
"<<numodd<<" And count of even numbers is :
"<<numeven<<endl;
return 0;
}
Input :
15
13
33
99
55
-1
Output :
Second largest number is : 55
Count of odd numbers is : 5 And count of even numbers is : 0
3)
#include<iostream>
using namespace std;
int main()
{
int N,i,j,x;
cout<<"Enter a positive number (N) : ";
cin>>N;
for(x=1;x<=N;x++)
{
for(i=x;i<=N;i++)
{
cout<<i;
}
j=x-1;
while(j>0)
{
cout<<j;
j--;
}
cout<<endl;
}
return 0;
}
Input:
Enter a positive number (N) : 7
Output:
1234567
2345671
3456721
4567321
5674321
6754321
7654321
4)
#include<iostream>
using namespace std;
int main()
{
int N,i,num[50],odddigits=0;
cout<<"How many numbers you want to input :
";
cin>>N;
for(i=0;i<N;i++)
{
cout<<"Number"<<i<<": ";
cin>>num[i];
}
for(int i=0;i<N;i++)
{
int x=num[i];
while(x)
{
if((x%10)%2 !=
0)
odddigits++;
x /= 10;
}
}
cout<<"Frequency of appeared ODD digits :
"<<odddigits<<endl;
return 0;
}
Input :
How many numbers you want to input : 3
Number 0: 7809
Number 1: 1127
Number 2: 8381
Output:
Frequency of appeared ODD digits : 7