+ 4

I have tried this problem but I'm getting errors.. Is there anyone who can help me in solving this one ??

Create a function that takes an array of numbers and returns "Hello World!" if the digit 5 appears in the array. Otherwise, return "there is no 5 in the array". Array of numbers is 12 65 4 67 0987 234 87 89 6745 567 987 476 708 36 5587 798 884 671 7889 We have to check single digit of array element and compare it with 5 if 5 then hello world else no 5 12 so element is 1 and 2 You will get this 2 elements by mod operator 12%10 so you will get 2 and and divide the number by 12/10 you will get 1 And compare 1 and 2 with 5 you get the desired output a=n%10 n=n/10 If(a==5) Hello world void fun(int arr, int n){ bool flag = 0; for(int i = 0; i < n; i++){ int cnt = 0; while(arr[i] > 0){ arr[i] = arr[i]/10; cnt++; } if(cnt == 5){ flag = 1; } } if(flag){ cout<<"Hello world"; } else{ cout<<"there is no five"; } } Can anyone help me in solving this problem in c or c++??

25th Apr 2021, 7:01 AM
Shobha Kumari
Shobha Kumari - avatar
17 Réponses
+ 4
Thnks everyone for ur help...
26th Apr 2021, 4:05 AM
Shobha Kumari
Shobha Kumari - avatar
+ 3
I modified something in that code void fun(int arr, int n){ bool flag = 0; int i = 0,a; while(i<n){ while(arr[i]>0){ a = arr[i]%10; if(a == 5){ flag = 1; break; } arr[i]=arr[i]/10; } if(flag ) break ; i++; } if(flag){ cout<<"Hello world"; } else{ cout<<"there is no five"; } } Hope this works
25th Apr 2021, 7:15 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 3
#include<iostream> #include<string> using namespace std; void fun(int arr[], int n) { int flag = 0; int i = 0,a; while(i<n) { while(arr[i]>0) { a = arr[i]%10; if(a == 5) { flag = 1; break; } arr[i]=arr[i]/10; } if(flag ) break ; i++; } if(flag) { cout<<"Hello World!"; } else { cout<<"there is no five in the array"; } } int main(){ int n; cin>>n; int arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; fun(arr,n); return 0; }
25th Apr 2021, 9:30 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 2
Thnks
25th Apr 2021, 8:05 AM
Shobha Kumari
Shobha Kumari - avatar
+ 2
#include<iostream> #include<string> std::string fun(int arr, int n) { bool flag = 0; int i = 0,a; while(i<n) { while(arr[i]>0) { a = arr[i]%10; if(a == 5) { flag = 1; break; } arr[i]=arr[i]/10; } if(flag ) break ; i++; } if(flag) { return"Hello world!"; } else { return"there is no five in the array"; } } Still it's showing compilation error Anyone plz give me the full solution so that I can check where I'm going wrong...
25th Apr 2021, 9:14 AM
Shobha Kumari
Shobha Kumari - avatar
+ 2
char* foo(int* nums, int len) { for (int i=0,j;i<len;i++) { for (j=nums[i];j;j/=10) { if (j % 10 == 5) { return "Hello World!"; } } } return "there is no five in the array"; } // Hope this helps
26th Apr 2021, 3:03 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
#include<iostream> #include<string> using namespace std; void fun(int arr[], int n) { int flag = 0; int i = 0,a; while(i<n) { while(arr[i]>0) { a = arr[i]%10; if(a == 5) { flag = 1; break; } arr[i]=arr[i]/10; } if(flag ) break ; i++; } if(flag) { cout<<"Hello World!"; } else { cout<<"there is no five in the array"; } } int main(){ int n; cin>>n; int arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; fun(arr,n); return 0; } This code executed successfully but it's showing wrong ans....why is it so?? Maybe because in the qs it's asking to return the expression and in this coding we r printing the expression....Maybe this could be the reason... I'm not sure....
25th Apr 2021, 9:45 AM
Shobha Kumari
Shobha Kumari - avatar
+ 1
Arun Ruban SJ Create a function that takes an array of numbers and returns "Hello World!" if the digit 5 appears in the array. Otherwise, return "there is no 5 in the array". Array of numbers is 12 65 4 67 0987 234 87 89 6745 567 987 476 708 36 5587 798 884 671 7889
25th Apr 2021, 11:29 AM
Shobha Kumari
Shobha Kumari - avatar
+ 1
25th Apr 2021, 2:58 PM
Shobha Kumari
Shobha Kumari - avatar
26th Apr 2021, 12:10 AM
Ciro Pellegrino
Ciro Pellegrino - avatar
0
What you did here is just counting digits in a number, for example 15 is made of two digits: 1 and 5, so 2 digits and cnt=2 in this example. What you have to do is check if one of these digits is equal 5, so in your while loop you should put something like that: cnt=arr[i]%10; if(cnt==5){ cout<<"Hello World!"; return 0; } arr[i]/=10;
25th Apr 2021, 7:39 AM
Michal Doruch
0
Thnks
25th Apr 2021, 9:37 AM
Shobha Kumari
Shobha Kumari - avatar
0
Show your exact question then maybe we can find it
25th Apr 2021, 9:57 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
0
In turbo c++ pluss output comes hello why can u explain this main() { for(;0;) { printf("hello"); } } Output in TC++ ??
27th Apr 2021, 3:55 AM
Abhishek
Abhishek - avatar
- 1
Hi
26th Apr 2021, 9:38 PM
Mohammad1 B
Mohammad1 B - avatar