#include <stdio.h>
#include <stdlib.h>

void main()
{
    int n,i,p;
    printf("enter the no: ");
    scanf("%d",n);
    for(i=0;i<n;i++)
    {

 p=i*i

 if(p==n)
 {
     scanf("the square root of %d is %d",n,i);
 }
    }
}

its not working error at if(p==n)

Dani AI

Generated

The snippet posted by contains both syntax and logic issues (missing address in input, a missing semicolon, and using the input function where a print was intended). As and already noted, fix the scanf call and the punctuation. Algorithmically, looping i from 0 up to n is inefficient and can overflow when squaring — see the pointers to better methods from and (binary search or Newton).

A simple, robust integer square‑root using binary search (O(log n)) avoids multiplication overflow by comparing with division:

#include <stdio.h>

unsigned long long isqrt(unsigned long long n) {
    if (n < 2) return n;
    unsigned long long lo = 1, hi = n / 2, ans = 0;
    while (lo <= hi) {
        unsigned long long mid = lo + (hi - lo) / 2;
        if (mid <= n / mid) {   /* safe check, avoids mid*mid overflow */
            ans = mid;
            lo = mid + 1;
        } else {
            hi = mid - 1;
        }
    }
    return ans;                /* floor(sqrt(n)) */
}

int main(void) {
    unsigned long long n;
    if (scanf("%llu", &n) != 1) return 1;   /* always check scanf's return */
    unsigned long long r = isqrt(n);
    if (r * r == n)
        printf("square root of %llu is %llu\n", n, r);
    else
        printf("no integer square root; floor is %llu\n", r);
    return 0;
}

Notes and troubleshooting:

  • Use int main(void) and return a status.
  • Prefer unsigned types for nonnegative inputs; explicitly handle negatives if needed.
  • The division compare (mid <= n / mid) prevents overflow that mid * mid can cause.
  • For floating-point roots or decimal precision, Newton–Raphson or sqrt() from <math.h> are appropriate (but require handling of linking and precision).

Recommended Answers

All 5 Replies

scanf("%d",n);

Apparently you still don't understand how scanf() works. Allow me to elaborate a bit. Every argument in C is passed by value, which means that a copy of the value of an argument is made and assigned to a temporary variable represented by the corresponding function parameter. So this:

void foo(int arg)
{
    arg += 10;
}

int main(void)
{
    int x = 0;

    foo(arg);
    printf("%d\n", x);

    return 0;
}

Will print 0 because only the copy of x is incremented. You can think of it something like this functionally equivalent code, where the problem is far more obvious:

int main(void)
{
    int x = 0;

    {
        int arg = x;

        arg += 10;
    }

    printf("%d\n", x);

    return 0;
}

This totally goes against what scanf() is designed to do: read values from input and assign them to variables. So how does scanf() work? Well, you can simulate pass by reference by passing the address of a variable. This creates a pointer to the object which can then be dereferenced, and the original object can be modified:

void foo(int *arg)
{
    *arg += 10;
}

int main(void)
{
    int x = 0;

    arg(&x);
    printf("%d\n", x);

    return 0;
}

Now the output will be 10 because the original object is being incremended instead of a copy. The address is still passed by value, but the object stored at that address is what you really want.

This is how scanf() is able to assign values to variables, but only if you pass the expected pointer. So if the argument isn't already a pointer, you must use the address-of operator or bad things will happen:

scanf("%d", &n);

write
scanf("%d",n); like scanf("%d",&n);
& insert a semicolon after stmnt:p=i*i;

Member Avatar for Member #957352

the link is simply damn awesome

Read the wikipedia article shown above. For simple programming effort, use the high/low method. First, you have to decide how many decimals of precision you need, and whether or not it preferable for the final result to be low or high. Using a binary search method, you can find the square root, or close approximation, of most any number in a pretty short time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.