Surface area of a hexagon - c

I am currently attempting to learn C, and have made this program to calculate the area of a regular hexagon:
#include <stdio.h>
#include <math.h>
void main(){
int a;
float ans;
scanf("%d", &a); // get length of side
ans = ((pow(a, (1/3)))/2)*(a*a);
printf("%f", ans);
}
However, it outputs seemingly random numbers.

Firstly your code doesn't compile (Missing semicolon) and also you should use int main() instead of void main().
Secondly your formula also wrong, the area of a regular hexagon of side length a is calculated as ((3√3)/2)*a².
Thirdly Expression like 1/3 always yield zero as both are integer, to get expected behavior make one of them float/double. like 1.0/3 or (float)1/3 etc.
#include <stdio.h>
#include <math.h>
int main()
{
int a;
float ans;
scanf("%d", &a); // get length of side
ans = (3*sqrt(3)/2.0)*a*a;
printf("%f", ans);
}

Related

identifier mod is undefined | filename extension .c

#include <stdio.h>
#include <math.h>
main()
{
float x,y,z;
printf("Enter the value of x,y:");
scanf("%f%f",&x,&y);
z= 2.5*log(x)+cos(32)+ mod(x*x-y*y)+ sqrt(2*x*y);
printf(" The value of the expression is %f",z);
}
By the equation you are solving, I feel that you want to find the absolute value of x*x - y*y. Something like |x|.
In that case, you should use fabs(x*x -y*y). It returns double.

power int number while in loop

OK, so my task is to get a single digit from a natural number and sum the square numbers (Using function while, which means no arrays yet :S). For instance I type 123 so sum=1*100+2*10+3*1; However the problem is that the digit could be whatever. My problem is that the power rises with int but its like so - 1, 10, 99, 1000. The problem for me is 99. Also answer is looping but I'll fix it later. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int N,
number=0,
answer=0,
a=1,
i=0;
printf("Type natural number: ");
scanf("%d",&N);
while(N>i)
{
number=N%10;
N/=10;
a=10;
a=pow(a,i);
answer+=number*number*a;
printf("%d\n", answer);
i++;
}
return 0;
}
Try it the other way around. Don't make the input an integer. Start at the beginning of the stream, get the character, convert it to an int 'number'. Then do
answer = 10 * answer;
answer += (number * number);
This will build up your answer little by little. Note that I am not sure that this is what you are asking for due to your example not seeming to match the code.
Let me know if this is off-base and I will update it.

Programming C if statements

#include <stdio.h>
#include <math.h>
int main (void){
double a,b,c, x, y, z1, z2;
printf("Enter the coefficients of a polynomial(ax^2+bx+c): ");
scanf("%lfx^2+%lfx+%lf",&a,&b,&c);
z1=(-b+sqrt(b*b-4*a*c))/2*a;
z2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("%lf and %lf",z1,z2);
getch();
return 0;
}
I want an IF statement such that if any polynomial does not have a value, it would be 1.
Say that I want my polynomial is x^2+7x+6; I would have to input as 1x^2+7x+6 to have the output values.
How would I write an if statement that if a, b, or c doesn't have a value, then it will equal to 1?
Set values of a,b,c =1 by default..
Now create a condition that example if a user enter char rather than int or float . it does nothing {use error handling for that,}
Or take all a,b,c input as string then typecast to to double, and use error handler if error occur assign it value 1

How to run multiple for loops in C programming?

I am trying to find the cube of the numbers from 1 to 10 using various types of for loops. I was wondering why after my for loop evaluates, the program stops and does not go on to evaluate the while loop? Can someone help? The for loop and while loop are supposed to do the same thing by the way. Thanks.
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(void)
{
int num;
for ( num=1; num<11; num++){
printf("The cube of %d is %d\n", num, num*num*num);
}
getchar();
return 0;
}
#include <stdio.h>
int main1(void)
{
int num1;
scanf("%d", &num1);
while (num1<11) {
printf("The cube of %d is %d\n", num1, num1*num1*num1);
num1++;
}
getchar();
return 0;
}
PS My first programming language is Python, so I am confused why C stop after the first for loop...
:(
main() and main1() are two different functions. Your system calls main(), the entry point of your program. When main() ends, that is when your program terminates. main1 isn't touched at all.
Either concatenate the two functions (copy-paste main1 into main) or call main1() from main(). But you would need a forward-declaration first:
int main1(void);
int main(void)
{
// ...
main1();
}
int main1(void) { ... }
BTW, main1 isn't a really good name for a function since it resembles main. That can confuse people who maintain your code.
after declaring num and num1 you set num in the for loop to 1, but not num1 in the while loop. plus the main functions issues mentioned

Using pow() in C

I have this problem with this homework I'm supposed to do.
[ It says Create a program that's able to calculate and show the sum of S]
Like S=1+1/4+1/8+1/16 ... till 1/ [2 pow n]
So I worked on it and came up with this code
#include <stdio.h>
void main()
{
int n,i;
float p,s;
printf("Enter the maximum power n :");
scanf("%d",&n);
s=0;
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",&s);
}
But when I execute it is always like S=0.
What am I doing wrong?
You are printing an address ('&s) with%f` variable. Using a wrong specifier invokes undefined behavior. You may get anything.
Also, No need of variable s. Remove the line
s+=p;
It should be like:
#include <stdio.h>
int main(void)
{
int n,i;
float p;
printf("Enter the maximum power n :");
scanf("%d",&n);
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
printf("p = %f\n",p);
}
printf("The sum of this equation is :%f",p);
}
You need to include <math.h> to get the proper prototype of pow().
You might need to link to the math library too gcc main.c -Wall -lm
#include <math.h>
....
for (i=0;i<n;i++)
{
p=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",s);
Your program has multiple problems. Enabling compiler warnings should tell you about some of them.
You should include the C header which contains the declaration of the pow function.
You add each addend twice.
In your second printf statement, you pass a float. But the %f format specifier expects a double argument. In your third printf statement, you pass a pointer to a float.
Another cosmetic problem is that your main function should return an int.
just a guess, may you replace the line
p+=1/pow(2, i);
with
p+=1.0f/(float)pow(2, i);
and
printf("The sum of this equation is :%f",&s);
with
printf("The sum of this equation is :%f",s);
Typo may be.. but you will have say %f and s (not &s)
printf("The sum of this equation is :%f",s);
On side note:
Once you include <math.h> you will get compiler warning for using correct pow(..) prototype. Below code would be relevant.
p+=1.0f/(float)pow(2.0f, i);

Resources