r/C_Programming • u/soul_sagee • Jun 17 '26
Question How many times printf will be executed?
For the following code:
void
The question should be answer in the form of n itself. n integer it may 9, 100, 300 anything so answer should be in ns format
I tried 2 hours on this and found out some patterns as outer loop runs log (base 3) n times and inner loop runs everytime 3^1..3^2... Less ..
How many times it will run
(Iam preparing for a competitive exam where some basic questions like these needs to be practiced. It was late night why my post was low efforts and I though everyone would understand that because such is a common pattern. I can use ChatGPT but I want a human though process while solving this question and not direct answer. That was the reason I used redit for but everyone instead praised my low efforts post and "diy". If anyone wants to solve this question pls help me out genuinely)
And someone pls tell how I format the code??? I don't use reddit much.
•
u/mikeblas Jun 17 '26
This is a re-post of a previously locked thread. You're on a short leash.
4
u/flyingron Jun 17 '26
Undefined behavior. n is read before it is set. This is an excusable defect in C, but reprehensible in C++.
Further, CONSARNIT main returns int. Nothing else is ever permitted in the language.
1
u/JJZinna Jun 17 '26 edited Jun 17 '26
In terms of n its
n * (log3(n)) - (3^(n+1)-1)/2 + log3(n)
if j started at 1 each iteration the sum would be n*log3(n)
but it starts at i, so we deduct 0, 2, 8… (3^x -1)
We can separate out the 3^x and the -1 so we have geometric sequence for 3^x and -1 which we add on at the end
I dont feel like dealing with the one-off portion but this works for clean powers of 3
7
u/No-Dentist-1645 Jun 17 '26
Can't you answer that question yourself by running the code with different values for n? Besides fixing the inconsistencies such as void main
1
u/dychmygol Jun 17 '26
How can you have run this code when it won't compile?
-1
u/Puzzleheaded_Study17 Jun 17 '26
Do you need an exact formula or just the order?
-2
u/soul_sagee Jun 17 '26
The question is asked in such format only.. and the answer needs to be in the format.
Eg.
Here, the loop runs n times Such way for my question in the format of n answer is needed
-4
u/soul_sagee Jun 17 '26
How do I format??
-6
u/soul_sagee Jun 17 '26
Pls someone tell me how to format the code?
0
u/[deleted] Jun 17 '26
[deleted] — view deleted comment
0
u/[deleted] Jun 17 '26
[deleted] — view deleted comment