Need help on these questions
Guys I need a little help on these questions , all are of C language.
1.) If (condition)
printf(“are you”);
else
printf(“How”);
The question is to replace (condition) with something that will give output -
“How are you” in a single run.
2.)int a=10,b=20,c=30,d;
a=b==c;
if(a==0)
printf(“%d,%d,%d,%d”,a,b,c,d);
else
printf(“%d,%d,%d,%d”,d,c,b,a);
Find output of the program
Please also comment on the difficulty level of these for amateurs.
Thank you.
1.) Can’t be possible if you can just edit (condition)
Why ? Because If else fonction need, a boolean value, which can only be “true” or “false”. You can’t have both.
Or if you can edit more just do an elseif.
2.) Can’t work either.
For multiple reason :
D is Unassigned.
a = b == c; Mean nothing : You can’t convert like this bool to int.
Explanation : a = 13 // You assigne 13 to a that’s ok
b == c // It’s a boolean question, is b = c ? And the answer is false.
So you can’t say a = false because a is an integer wich means he understand number, false is a boolean value not a number.
Exemple : bool a = true; // We just assigne true to a.
a = b ==c // if b = c assigne true to a, else assigne false.
I’m still here to answer if you have any question.
Have fun !
Message me anytime!
Message me anytime!
I believe on C# that would also work. A bit rusty on my C# knowledge bit isn’t the bool just a typedef uint
And I think the value of d depends on the compiler how it initializes variables
I break stuff….. a lot
printf doesn’t exist, you should use Console.WriteLine(); as exemple in the console.
Message me anytime!
True enough, I was merely commenting on the bool part. Now at vacation and only have my phone but will test this when I get back home.
I break stuff….. a lot
Message me anytime!
I break stuff….. a lot
@sarrocks
For the first question, if you read carefully, you need to print “How are you”, however, the printf calls in the if/sel only displays “How” and “are you”, this should already ring a bell and helps you find an easy answer :)
As for the second one… If you cannot figure it out, why not try to run the code yourself?
The difficulty of these two exercises is pretty basic imo and would fit in the first week of C learning.
The question n1 could have been more complex but it doesn’t seem to be the case since you didn’t mention any restriction.
@DIDIx13
You actually can go into the if and the else on the same run in C, but it’s not needed in this case.