Square root program in C without using sqrt function [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to make a program in C to find the approximate square root of a number using the formula NG = 0.5( LG + N/ LG). So far i have:
#include <stdio.h>
#include <math.h>
int main(){
double n;
double LG=1;
double NG;
printf("Enter number");
scanf_s("%lf",&n);
do{
NG=(.5*(LG+n/LG));
LG=NG;
}while((NG*NG-n)<.005);
printf("The root is %lf",NG);
}
This structure works fine in java, but the loop doesn't seem to be executing in C.
Thanks for any advice.

You do not want to loop while NG*NG-n is less than .005. You want to loop while NG*NG is farther from n than desired.
The distance between NG*NG and n is fabs(NG*NG - n).

Related

C math on user input [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I'm new to c and I'm trying to preform maths on a user input, specifically the year of a date.
When I try to divide , or do any maths on the variable 'y' and store it in 'fpy' it will always print the value '6422032' or close to it. Any help would be great, I've been trying for hours now.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int d,m,f,y,spY;
//char y[3];
printf("Day from date calculator \n");
printf("Please enter the date in DD/MM/YYYY formate. For example , 1/7/1440 \n");
printf(">");
scanf("%d/%d/%i",&d,&m,&y);
int fpy = y/10;
printf("%d",&fpy);
return(0);
}
this
printf("%d",&fpy);
should be
printf("%d",fpy);

I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user.
//program to print numbers 0 to n where n is input from user
#include<stdio.h>
int main()
{
int i=0,num;
printf("Enter number: ");
scanf("%d",&num);
while(i<=num)
{
printf('%d',i);
i++;
}
return 0;
}
Im getting error saying expected const char
I tried to get solution over several websites
since im new to this language I'm facing trouble in such simple code
I tried running this code on several online compilers but everywhere I get the same issue
In line 11 in the printf statement you have used single quotes - '%d' which does is giving you problems here, change it to a "%d". Hope that helps.

Program to find the area of a circle in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm a new learner to C and I have found this program which is to find the area of a circle. However my compiler keeps throwing this error at me:
[Error] C:\Users\Jiachenn\Documents\C-Free\Projects\HelloWorld\main.c:11: error: syntax error before "printf"
This is my code:
#include <stdio.h>
#include <conio.h>
main()
{
int radius;
float pi =3.14f, area;
clrscr();
printf("Input the radius:");
scanf("%d",&radius);
area = pi*radius*radius
printf("\n Area of circle= %f", area);
getch();
}
Do help!
You are just missing the semicolon on the end of line
area = pi*radius*radius ;

Dev C keeps returning a wrong value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
main()
{
int n;
printf("Introduce un número entero\n");
scanf("%d", &n);
printf("Has introducido el número: %d", &n);
}
Every time I run this C code I get 6487628 for n, I have uninstalled and installed it over and over again and it keeps doing that, I don´t know what else to do.
You don't want the &n in the printf(), you want n. You are displaying the memory location n is stored in

c float to int conversion acting weird [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to convert a float value to an int in C. I'm using print statements to see what's happening and making sure I'm getting the desired results, but something is not working correctly. Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void){
float changeOwed = -1.00;
while(changeOwed < 0.00){
printf("How much change is owed?\n");
changeOwed = GetFloat();
}
printf("%f\n", changeOwed);
int centsOwed = roundf(changeOwed*100);
printf("%o\n", centsOwed);
If user input is, lets say 0.49, here is the output:
0.490000
61
I don't understand why the cast result is 61. I would expect normal errors to be a result of 0, 48 or 50, but I don't get this weird result and can't figure out the logic of it.
In case you don't get it yet ...
"061" is octal for "49".
Use printf("%d") instead of "%o" if you want to see a decimal "49".
Here is a good list of "printf" format options:
http://www.cplusplus.com/reference/cstdio/printf/

Resources