- 1

hey, I need Some help regarding a C program in C language

I am Trying to creat a program using for loop for find the perfect square but I getting set backs. #include <stdio.h> //Compiler version gcc 6.3.0 int main() { printf("Enter the Square......\n"); int a,b,i; scanf("%d",&a); b==a/2; for( i=1; i>=b; i++) { if(a/i==i) { printf("The No is perfect square square\n"); break; } else { printf("The No is not perfect square"); } } return 0; } This is my code And I wants that if the condition is full fill in the if Block. It should not go to If else blocks.

2nd Feb 2022, 5:23 AM
Priyanshu Raj
Priyanshu Raj - avatar
7 Antworten
0
Totally correct devanille well spotted! Change from: if(a/i=i) to become: if(i*i==a)
2nd Feb 2022, 8:57 AM
HungryTradie
HungryTradie - avatar
+ 2
HungryTradie Thanks for your help Man. I really appreciate that.. Mate
2nd Feb 2022, 11:08 AM
Priyanshu Raj
Priyanshu Raj - avatar
+ 1
what does that b==a/2 do?
2nd Feb 2022, 5:27 AM
CGM
CGM - avatar
+ 1
G'day Priyanshu Raj as Cristian Gabriel Mazzulla said: b==a/2; That is a check, not an assignment. When I see = I say "is", when I see == is say "is equal". you want: b=a/2; your for code starts at i=1, runs if i is greater or equal to b (not true, never runs) then increments by i+1. you want: for (i=1 ; i<=b ; i++) I'm not sure about the rest of the math in your code, is that gunna work?
2nd Feb 2022, 7:49 AM
HungryTradie
HungryTradie - avatar
+ 1
G'day again Priyanshu Raj I had a go with your code, it was pretty close to perfect. Nicely done! I added an else if to check for i==b before printing the failure message. I also changed the if - else if - else so it looped without printing anything when not finished incrementing. https://code.sololearn.com/cNg8Fp2YgBu6/?ref=app
2nd Feb 2022, 8:50 AM
HungryTradie
HungryTradie - avatar
0
It's Divide the the the square by 2.
2nd Feb 2022, 7:11 AM
Priyanshu Raj
Priyanshu Raj - avatar