Escolar Documentos
Profissional Documentos
Cultura Documentos
i
8 Votes
For this problem, you must allow the user to enter exactly 5
numbers as initial input.
The program must then ask for an additional number.
The output of the program will indicate whether the last number is
contained in the first 5 numbers.
Your output should resemble that provided in the example below.
Example 1
Enter a number: 1
Enter a number: 2
Enter a number: 5
Enter a number: 10
Enter a number: 15
Enter the number to be searched: 10
Example 2
Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: 40
Enter a number: 50
Enter the number to be searched: 60
1 // @BEGIN_OF_SOURCE_CODE
2
3 #include <cstdio>
4
5 using namespace std;
6
7 int main (int argc, char *argv [])
8 {
9 int numbers [5];
10
11 // taking inputs
12 for ( int i = 0; i < 5; i++ ) {
13 printf ("Enter a number: ");
14 scanf ("%d", &numbers [i]);
15 }
16
17 printf ("Enter the number to be searched: ");
18
19 int searchNumber;
20 scanf ("%d", &searchNumber);
21
22 bool isNumberFound = false;
23
24 // searching number
25 for ( int i = 0; i < 5; i++ ) {
26 if (numbers [i] == searchNumber) {
27 isNumberFound = true;
28 break;
29 }
30 }
31
32 if (isNumberFound)
33 printf ("The number %d appears in the first 5 numbers.\n"
34 else
35 printf("The number %d does not appear in the first 5 numbers\n"
36
37 return 0;
38 }
39
40 // @END_OF_SOURCE_CODE
?
Solution in Python
1 a = []
2
3 for i in range (5) :
4 a.append (int (input ('Enter a number : ')));
5
6 key = int (input ('Enter the number to be searched: '
7
8 if key in a :
9 print ('The number %d appears in the first 5 numbers.'
10 else :
11 print ('The number %d does not appear in the first 5 numbers.'
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 3/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
Advertisements
Easy Problems
1. shiki
MAY 10, 2010 AT 10:55 PM
Solved in Python 3.1.2:
1 x = []
2
3 while len(x) < 5:
4 z = input("Next number, dawg! ")
5 x.append(z)
6
7 y = input("Number to search for: ")
8
9 if y in x:
10 print(y, " has been typed before: ", x)
11 else:
12 print("That number is obviously not one you have typed before. :/"
i
Rate This
2. Shakil
DECEMBER 25, 2010 AT 5:29 PM
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 4/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
i
Rate This
3. Hassan Nomani
OCTOBER 22, 2012 AT 4:05 PM
#include
int main()
{
int a[100],i,j;
for(i=0;i<5;i++)
{
printf("Enter a number:");
scanf("%d",&a[i]);
}
printf("Enter the number to be searched:");
scanf("%d",&j);
for(i=0;i<5;i++)
{
if(a[i]==j)
{
printf("The number %d appears in first 5 numbers",j);
break;
}
}
if (j!=a[i])
printf("The number %d does not appear in the first 5
numbers.",j);
return 0;
}
i
Rate This
4. Bri
SEPTEMBER 20, 2014 AT 2:16 AM
};
func();
i
Rate This
5. Adib Ahsan
FEBRUARY 28, 2015 AT 2:14 PM
#include
int main ()
{
int a,b,c,d,e,f;
if (f==a||f==b||f==c||f==d||f==e)
{
printf(“%d is present in the 5 numbers”,f);
}
else printf(“%d is not present in the 5 numbers”,f);
return 0;
}
i
Rate This
6. Ray haq
MARCH 1, 2015 AT 6:56 PM
#include
#include
int main()
{
int numbers[5];
int searchNumber, i;
char * pC;
for (i = 1; i<=5; i++){
switch(i){
case 1: pC = "st";
break;
case 2: pC = "nd";
break;
case 3: pC = "rd";
break;
default: pC = "th";
}
printf("Enter the %d%s number: ", i, pC);
scanf(" %d", &numbers[i-1]);
}
printf("What number you wanna search?");
scanf(" %d", &searchNumber);
int found = 0;
for(i=0; i0){
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 7/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
for(i=0; i0){
printf(“Found it!”);
}else(printf(“Not Found!”));
return 0;
}
i
Rate This
7. Porag
MAY 11, 2015 AT 12:02 AM
#include
#include
void main()
{int a,b,c,d,e,f;
printf(“enter ur no…….\n”);
scanf(“%d %d %d %d %d”,&a,&b,&c,&d,&e);
printf(“entr ur additional no…..\n”);
scanf(“%d”,&f);
if ((f==a)||(f==b)||(f==c)||(f==d)||(f==e))
printf(“found in typed no\n”);
else printf(“not found in typed no\n”);
getch();
return 0;
i
Rate This
8. Lenz Lawson
JANUARY 10, 2016 AT 7:15 PM
A similar program has already been posted, but I believe this
one is a lot neater.
#include
int main(){
int a,b,c,d,e,f;
printf(“please enter a number: “);
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 8/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
i
Rate This
9. Rabbi hasan
FEBRUARY 7, 2016 AT 10:03 PM
#include
int main()
{
int a[5],i,b;
for(i=0;i<5;i+=1)
{
printf("Enter a number:");
scanf("%d",&a[i]);
}
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 9/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
}
printf("Enter the number to be searched:");
scanf("%d",&b);
for(i=0;i<5;i++){
if(a[i]==b){
printf("The number %d appears in the first %d numbers.",b,i+1);
break;
}
if(i==5)
printf("The number %d is not appears in the first 5 numbers.",b);
return 0;
i
Rate This
10. L7
FEBRUARY 15, 2016 AT 3:43 AM
Ruby
def umz
puts “Enter the first number: ”
fr = gets.chomp.to_i
puts “Enter the second number: ”
sec = gets.chomp.to_i
puts “Enter the third number: ”
th = gets.chomp.to_i
puts “Enter the forth number: ”
fth = gets.chomp.to_i
puts “Enter the fifth number: ”
fith = gets.chomp.to_i
puts “Enter number to be searched: ”
sc = gets.chomp.to_i
umz()
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 10/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
i
Rate This
}
}
}
i
Rate This
#include
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 11/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
#include
int main()
{
int a,b,c,d,e,num;
i
Rate This
13. JB
OCTOBER 22, 2016 AT 3:12 AM
num_list = []
for i in range(5):
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 12/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems
for i in range(5):
response = input(“Please enter a number: “)
num_list.append(response)
if response2 in num_list:
print (“The number {} appears in the first 5
numbers”.format(response2))
i
Rate This
import java.util.*;
class Search
{
public static void main(String…args)
{
int a[] = new int[10] ;
int i,j;
Scanner s = new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.format("Enter the %d no:-",i);
a[i]=s.nextInt();
}
for(i=0;i<5;i++)
{
if(a[i]==j)
{
}
if(a[i]!=j)
System.out.format("number not found");
}//End of main
}//End of class
i
Rate This
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 14/14