#include<stdio.h>
//Q-- First and Last Digit ||Problem Code: FLOW004
int main(){
int a,n,x,y,sum;
sum=0;
scanf("%d\n",&a);
for(int i=0;i<a;i++){
scanf("%d",&n);
y=n;
while(n!=0){
x=n%10;
if (y==n||n<10){
sum+=x;
}
n=n/10;
}
printf("%d\n",sum);
sum=0;}
return 0;}
Have you considered single digit numbers?
1 Like
I’ve considered single digit numbers and all digit numbers but still it’s not accepting my answer.
Can you please help me on these?
#include <stdio.h>
int first(int o)
{
int jjj, q;
q = o;
while (q > 9)
{
q = q / 10;
if (q <= 9)
{
jjj = q;
}
}
return jjj;
}
int main()
{
int t, n, p[1000];
scanf("%d", &t);
for (int i = 0; i < t; i++)
{
int q, jj;
scanf("%d", &n);
q = n % 10;
if (n > 9)
{
jj = first(n);
}
else
{
jj = 0;
}
p[i] = q + jj;
}
for (int j = 0; j < t; j++)
{
printf("%d\n", p[j]);
}
return 0;
}
ssjgz
4
Compiler warnings!
[simon@simon-laptop][19:13:09]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling vivekratr-FLOW004.cpp
Executing command:
g++ -std=c++17 vivekratr-FLOW004.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
vivekratr-FLOW004.cpp: In function ‘int main()’:
vivekratr-FLOW004.cpp:37:10: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
37 | scanf("%d", &t);
| ~~~~~^~~~~~~~~~
vivekratr-FLOW004.cpp:45:14: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
45 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
vivekratr-FLOW004.cpp: In function ‘int first(int)’:
vivekratr-FLOW004.cpp:27:12: warning: ‘jjj’ may be used uninitialized in this function [-Wmaybe-uninitialized]
27 | return jjj;
| ^~~
Successful
Edit:
Ah - as it happens, the warnings here are irrelevant.
You are, however, getting the wrong answer for single-digit numbers.
2 Likes
Is it correct now?
#include <stdio.h>
int main()
{
int t,n,p[1000];
scanf("%d", &t);
for (int i = 0; i < t; i++)
{
int q;
int e = 0;
scanf("%d", &n);
q = n;
while (q > 0)
{
int p;
p = q % 10;
q = q / 10;
if (p == 4)
{
e++;
}
}
p[i] = e;
}
for (int j = 0; j < t; j++)
{
printf("%d\n",p[j]);
}
return 0;
}