fork download
  1. //Xinnuo Wanng CS1A Chapter 8 #1 Page.487
  2. /*******************************************************************************
  3.  * Determine Numbers Validation
  4.  * _____________________________________________________________________________
  5.  * The program lets users enter a number and determines if the number is valide
  6.  * by checking in given list.
  7.  * _____________________________________________________________________________
  8.  * Input
  9.  * num : The accepted number
  10.  *
  11.  * ****************************************************************************/
  12.  
  13. #include <iostream>
  14. using namespace std;
  15. int check(int arr[],int size,int num);
  16. int main()
  17. {
  18. int arr[]={5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152,
  19. 4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231, 3852085, 7576651,
  20. 7881200, 4581002};
  21. cout << "Enter a number: " ;
  22. int num;
  23. cin>> num;
  24. cout << num << endl;
  25. int size = sizeof(arr)/ sizeof (arr[0]);
  26. int c = check(arr, size, num);
  27. if (c == -1)
  28. {
  29. cout << "Invalide Input." << endl;
  30. }
  31. return 0;
  32. }
  33. int check(int arr[], int size, int num)
  34. {
  35. bool found = false;
  36. int position = -1;
  37. int index = 0;
  38. while(!found && index < size)
  39. {
  40. if(arr[index] = num)
  41. {
  42. found = true;
  43. cout << "Valide Number" << endl;
  44. position = index;
  45. }
  46. index++;
  47. }
  48. return position;
  49. }
Success #stdin #stdout 0s 5348KB
stdin
7881200
stdout
Enter a number: 7881200
Valide Number