Você está na página 1de 14

10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems

UAB – 2005: Problem 2:


Searching 101

JULY 27, 2009AUGUST 8, 2013 / SHAHAB

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.

All of the numbers that will be entered are integers.

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

The number 10 appears in the first 5 numbers.


https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 1/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems

The number 10 appears in the first 5 numbers.

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

The number 60 does not appear in the first 5 numbers.


?
Critical TestCases
1 Enter a number: 10
2 Enter a number: 10
3 Enter a number: 10
4 Enter a number: 10
5 Enter a number: 10
6 Enter the number to be searched: 10
7 The number 10 appears in the first 5 numbers.
8
9 Enter a number: 10
10 Enter a number: 11
11 Enter a number: 12
12 Enter a number: 13
13 Enter a number: 14
14 Enter the number to be searched: 14
15 The number 14 appears in the first 5 numbers.
16
17 Enter a number: 20
18 Enter a number: 21
19 Enter a number: 22
20 Enter a number: 23
21 Enter a number: 24
22 Enter the number to be searched: 20
23 The number 20 appears in the first 5 numbers.
24
25 Enter a number: 10
26 Enter a number: 11
27 Enter a number: 12
28 Enter a number: 13
29 Enter a number: 15
30 Enter the number to be searched: 12
31 The number 12 appears in the first 5 numbers.
32
33 Enter a number: 20
34 Enter a number: 19
35 Enter a number: 18
36 Enter a number: 17
37 Enter a number: 16
38 Enter the number to be searched: 17
39 The number 17 appears in the first 5 numbers.
?
Solution in C/C++
https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 2/14
Solution
10/23/2017 in C/C++ UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems

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

14 thoughts on “UAB – 2005: Problem


2: Searching 101”

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

DECEMBER 25, 2010 AT 5:29 PM


good

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

var x = prompt(“first number?”);


https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 5/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems

var x = prompt(“first number?”);


var a= prompt(“second number?”);
var b = prompt(“third number?”);
var c = prompt(“fourth number?”);
var d = prompt(“5th number?”);

var ad = prompt(“please enter an additional number.”);

var array = [x,a,b,c,d];


var func = function() {
for (i=0;i<array.length;i++) {
if (ad === array[i]) {return console.log("the number" + " " + ad + "
" + "appears in the first five numbers.");}
else
return console.log("The number" + " " + ad + " " + "does not
appear in the first 5 numbers.");
};

};

func();

i
Rate This

5. Adib Ahsan
FEBRUARY 28, 2015 AT 2:14 PM
#include

int main ()
{
int a,b,c,d,e,f;

printf(“Enter the number: “);


scanf(“%d”,&a);

printf(“Enter the number: “);


scanf(“%d”,&b);

printf(“Enter the number: “);


scanf(“%d”,&c);

printf(“Enter the number: “);


scanf(“%d”,&d);

printf(“Enter the number: “);


https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 6/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems

printf(“Enter the number: “);


scanf(“%d”,&e);

printf(“Enter the searching number: “);


scanf(“%d”,&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;

ei simple prob er jonno array tanar drkr ache naki? novice


prgrmr….

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

printf(“please enter a number: “);


scanf(“%d”,&a);
printf(“\n”);
printf(“please enter a 2nd number: “);
scanf(“%d”,&b);
printf(“\n”);
printf(“please enter a 3rd number: “);
scanf(“%d”,&c);
printf(“\n”);
printf(“please enter a 4th number: “);
scanf(“%d”,&d);
printf(“\n”);
printf(“please enter a 5th number: “);
scanf(“%d”,&e);
printf(“\n”);
printf(“please enter the number to be searched: “);
scanf(“%d”,&f);
printf(“\n”);
if(f==a||f==b||f==c||f==d||f==e){
printf(“the number %d appears in the first 5 numbers”,f);
}
else{
printf(“the number %d doesn’t appear in the first 5 numbers”,f);
}
printf(“\n”);
return 0;
}

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

if (sc == fr || sc == sec || sc == th || sc == fth || sc == fith)


puts “The number #{sc} appears in he first 5 numbers ”
end
end

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

11. H Taqui Ahmad


FEBRUARY 28, 2016 AT 11:20 AM
#include
int main(void)
{
int x[5];
int i,j;
for (i=0;i<=4;i++){
printf("Enter a number:");
scanf("%d", &x[i]);
}
printf("Enter the number to be searched:");
scanf("%d", &j);
for (i=0;i<=4;i++){
if (x[i]==j)
{printf("The number %d appears in the first 5 numbers.\n", j);
break;
}
else
{
if (i==4)
printf("The number %d does not appear in the first 5 numbers.",
j);
else
continue;

}
}
}

i
Rate This

12. hasan khan


JULY 23, 2016 AT 1:15 AM
//by using only if else

#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;

printf(“1st num : “);


scanf(“%d”,&a);
printf(“2nd num : “);
scanf(“%d”,&b);
printf(“3rd num : “);
scanf(“%d”,&c);
printf(“4th num : “);
scanf(“%d”,&d);
printf(“5th num : “);
scanf(“%d”,&e);

printf(“enter the number to be searched :\n”);


scanf(“%d”,&num);

if(num==a) {printf(“The number %d appears in the first 5


numbers.”,num);}else

if(num==b) {printf(“The number %d appears in the first 5


numbers.”,num);}else

if(num==c) {printf(“The number %d appears in the first 5


numbers.”,num);}else

if(num==d) {printf(“The number %d appears in the first 5


numbers.”,num);}else

if(num==e) {printf(“The number %d appears in the first 5


numbers.”,num);}

else {printf(“The number %d is not appears in the first 5


numbers.”,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)

response2 = input(“Please enter a number: “)

if response2 in num_list:
print (“The number {} appears in the first 5
numbers”.format(response2))

if response2 not in num_list:


print (“The number {} does not appear in the first 5
numbers”.format(response2))

i
Rate This

14. Pratik Advani


AUGUST 21, 2017 AT 2:03 AM
here’s one in java

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();
}

System.out.format("Enter the no to be searched:-");


j=s.nextInt();

for(i=0;i<5;i++)
{
if(a[i]==j)
{

System.out.format("No found at %d location",+i);


https://tausiq.wordpress.com/2009/07/27/uab-2005-problem-2-searching-101/ 13/14
10/23/2017 UAB – 2005: Problem 2: Searching 101 | Solved Programming Problems

System.out.format("No found at %d location",+i);


break;
}

}
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

Você também pode gostar