Here's my code in Python
def sol(s):
a, b = 0, 0
# you've to write the given number in the form of
# n = 4*a + 7*b (it is obvious that the solution will contain all 4's as prefix and
# 7's as suffix)
while (s > 0):
if (s % 7 == 0):
b += 1
s -= 7
elif (s % 4 == 0):
a += 1
s -= 4
else:
a += 1
s -= 4
ans = ""
if (s < 0):
ans = "-1"
return ans
ans += "4" * a
ans += "7" * b
return ans
s = int(input())
print(sol(s))
//what is wrong in this program?? can anyone pls tell.
#include <bits/stdc++.h>
using namespace std;
int main()
{
//write your code here
int n;
int x=-1;
int y=4;
int z=7;
cin>>n;
if(n==4)
{
cout<<y;
}
if(n>4 && n<7)
{
cout<<x;
}
if(n==7)
{
cout<<z;
}
if(n>7)
{
if(n%4==0)
{
int k=n/4;
for(int h=0; h<k; h++)
cout<<y;
return 0;
}
if(n%7==0)
{
int k=n/7;
for(int h=0; h<k; h++)
cout<<z;
return 0;
}
if(n%4!=0 || n%7!=0)
{
int i=1;
int a[n];
a[0]=4;
int sum=4;
while(sum<n)
{
if(i%2==0)
{
sum=sum+4;
a[i]=4;
i++;
}
else
{
sum=sum+7;
a[i]=7;
i++;
}
}
if(sum!=n)
{
cout<<x;
}
else
{
for(int j=0; j<i; j++)
{
cout<<a[j];
}
}
}
}
if((n<4 && n>0) || n<0 )
cout<<x;
return 0;
}