UAB – 2005: Problem 2: Searching 101


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.

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.


Enter a number: 10
Enter a number: 10
Enter a number: 10
Enter a number: 10
Enter a number: 10
Enter the number to be searched: 10
The number 10 appears in the first 5 numbers.

Enter a number: 10
Enter a number: 11
Enter a number: 12
Enter a number: 13
Enter a number: 14
Enter the number to be searched: 14
The number 14 appears in the first 5 numbers.

Enter a number: 20
Enter a number: 21
Enter a number: 22
Enter a number: 23
Enter a number: 24
Enter the number to be searched: 20
The number 20 appears in the first 5 numbers.

Enter a number: 10
Enter a number: 11
Enter a number: 12
Enter a number: 13
Enter a number: 15
Enter the number to be searched: 12
The number 12 appears in the first 5 numbers.

Enter a number: 20
Enter a number: 19
Enter a number: 18
Enter a number: 17
Enter a number: 16
Enter the number to be searched: 17
The number 17 appears in the first 5 numbers.


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main (int argc, char *argv [])
{
    int numbers [5];
    
    // taking inputs
    for ( int i = 0; i < 5; i++ ) {
        printf ("Enter a number: ");
        scanf ("%d", &numbers [i]);
    }
    
    printf ("Enter the number to be searched: ");
    
    int searchNumber;
    scanf ("%d", &searchNumber);
    
    bool isNumberFound = false;
    
    // searching number
    for ( int i = 0; i < 5; i++ ) {
        if (numbers [i] == searchNumber) {
            isNumberFound = true;
            break;
        }
    }
    
    if (isNumberFound)
        printf ("The number %d appears in the first 5 numbers.\n", searchNumber);
    else
        printf("The number %d does not appear in the first 5 numbers\n", searchNumber);

    return 0;
}

// @END_OF_SOURCE_CODE

a = []

for i in range (5) :
    a.append (int (input ('Enter a number : ')));

key = int (input ('Enter the number to be searched: '))

if key in a :
    print ('The number %d appears in the first 5 numbers.' % key)
else :
    print ('The number %d does not appear in the first 5 numbers.' % key)

13 thoughts on “UAB – 2005: Problem 2: Searching 101

  1. Solved in Python 3.1.2:

    x = []
    
    while len(x) < 5:
        z = input("Next number, dawg! ")
        x.append(z)
        
    y = input("Number to search for: ")
    
    if  y in x:
        print(y, " has been typed before: ", x)
    else:
        print("That number is obviously not one you have typed before. :/")
    
  2. #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;
    }

  3. 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();

  4. #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: “);
    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;
    }

  5. #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){
    printf(“Found it!”);
    }else(printf(“Not Found!”));

    return 0;
    }

  6. #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….

  7. 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: “);
    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;
    }

  8. #include
    int main()
    {
    int a[5],i,b;

    for(i=0;i<5;i+=1)
    {
    printf("Enter a number:");
    scanf("%d",&a[i]);

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

    }

  9. 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()

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

    }
    }
    }

  11. //by using only if else 🙂

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

    }

  12. num_list = []

    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))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s