Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this code that will print numbers from 0 to 9 in english words (like one for 1, two for 2, etc.). What if I wanted to print 374? Or something much larger, like 7549846451?
#include <stdio.h>
int main()
{
double sum;
if(scanf("%1f",&num)!=0)
{
if(num=(int)num)
{
switch((int)sum)
{
case 0:printf("zero\n");break;
case 1:printf("one\n");break;
case 2:printf("two\n");break;
case 3:printf("three\n");break;
case 4:printf("four\n");break;
case 5:printf("five\n");break;
case 6:printf("six\n");break;
case 7:printf("seven\n");break;
case 8:printf("eight\n");break;
case 9:printf("nine\n");break;
default:printf("not a digit"); break;
}
}else
{
printf("Invalid")
return 0;
}
}
return 0;
}
This is a good start, but it would take a lot more to complete your program:
Start by expanding your code to printing numbers 10..99. There would be a special case for 11..19, but after that it's pretty regular. The lower 20 can be addressed with a lookup table. In fact, making a look-up table for the whole range wouldn't be too bad, either.
With a routine that writes out numbers 0..99 in hand you can expand into hundreds by looking at the third digit the right, writing it out, adding "hundred", and proceeding to writing out the number 0..99
Now that you have a routine for writing out three-digit numbers all you need is to split your number into groups of tree, calling this routine for non-zero groups, and adding "billion", "million", and "thousand" corresponding to the rank of the group.
Here you have the solution to your problem. It is even the same example as you have pasted here, so if you have read the comments below, you'd have seen the comment form Bheema in which he posted the whole code for it.
Also, you can try writing your own code, it's not that hard. dasblinkenlight gave you instructions how to do it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Greeting.
I am doing the following exercise: Run a program to determine an approximate value of π using the series shown below. The calculation is performed by generating a certain number of terms in the series. The number of terms to be considered is read from the standard input (keyboard) (greater than or equal to 30000).
Note: In the resolution of this issue, you cannot use functions from the math.h library of the C programming language.
example: input a value enter total terms >=30000: entering 30000 should give you the result o pi:3.141559
The prolem I'm having: Uppon entering the same value(30000)I am not getting the corret value o pi=3.14.... but instead it's something like:0.0067...
heres my code:
#include<stdio.h>
#include<math.h>
int main(void){
double numerator, denominator, pi=0.0;
int k;
printf("input a total number o terms >=30000:");
for ( k=1;k<=30000;k++){
scanf("%d",&k);
if(k>=30000){
if(k%2==0){
numerator=1;
}
else {
numerator=-1;
}
}
denominator=2.0*k+1.0;
pi+=numerator/denominator;
pi=4*pi;
printf("value of PI is= %lf",pi);
}
return 0;
}
Can someone point out what I am doing wrong and how can I solve it pls?
Your time and attention are deeply appreciated.
Thank You.
There are many problems with what your implementation of the algorithm:
Try avoiding scanf ad printf inside the for loop.
Instead of getting the k variable from the user try and get the maximum value of k.
denominator=2*k+1 is wrong if you follow the algorithm that you gave in your question and should be changed to denominator=2*k-1.
You repeat pi*=4 every iteration.
I applied all those improvement and i get pi=3.141926 for just 3000 iterations.
Here a little help on how your for loop should look like:
for (int k=1;k<iterations;k++){
numerator*=-1;
denominator=2.0*k-1.0;
pi+=numerator/denominator;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So, the question was find the greatest number using "if" statement between three numbers. I wrote the program myself and I'm a newbie to programming so, I tried to run this program but it showed 2 errors repeatedly.
error 1: x is undefined
error 2: y is undefined
So, I understand that it wants me to define the value of x,y but what I want to do is to firstly Compare the value of a and b and then compare the value of c with the greater number I got by comparing a and b.
I'm pasting my code below:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("enter any three number::");
scanf("%d%d%d",&a,&b,&c);
if(x=(a>b)||(b>a));
printf("\n(a>b)||(b>a)=%d",&x);
{
if(y=(x>c)||(c>x));
printf("\n(y>c)||(c>y)=%d",&y);
}
getch();
return 0;
}
How can I improvise this? Hoping for positive response!
The problem is, you never defined x and y, as your compiler pointer out. Just define them like you did for other variables.
int a,b,c,x,y;
would do the job.
That said
you don't really need those if statements, as you used here.
The result of the relational operators (< / >) are not the numbers, it's an integer value, either 0 or 1.
For %d, you don't need to supply the address of the variable, just the variable is enough.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am writing a code for solving arithmetic expression like:4+3-2*6*(3+4/2)
For that I need to compare the operators in the string with precedence like:
1. ( or )
2. * or /
3. + or -
Can someone tell me how to compare two characters. As their ASCII values are not in the order which I want!
Use a lookup table. If using ASCII, it is just a 256 element table. You index it with the char.
For that I need to compare the operators in the string with precedence
like:
You can do something like this to get a precedence for each character:
int get_precedence (char c) {
switch (c) {
case '+':
case '-': return 3;
case '*':
case '/': return 2;
case '(':
case ')': return 1;
default: return -1;
}
}
That makes it very easy to add in additonal characters as you need.
However, you'll still have some issues. For example, how will you tell unary negation from subtraction? You'll need to look at context for that, because the answer will depend on what comes before. Dealing with infix notation is hard.
To do this yourself, you'll want to write a recursive descent parser or use the shunting yard algorithm. Either way, it'll be several hundred lines of hard code. Then you'll need to decide if you evaluate from the abstract syntax tree or compile to byte code.
I am writing a code for solving arithmetic expression
like:4+3-2*6*(3+4/2)
If you want to solve that in an easy way, you should find a pre-existing library. TinyExpr is one such solution. It's open-source and contained in a single C source code file. Code for that would look like:
#include "tinyexpr.h"
double answer = te_interp("4+3-2*6*(3+4/2)", 0);
Even in ASCII, the order of the integers are still the same. You can compare single digits. For example:
if ('7' > '2') will evaluate to true
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am starting to learn C. Today I was trying a little program that just do a average point starting from 3 input.
After all I wanted to print the number of the averages done in the session, so I insert a simple
counter=counter+1;
into the main while loop and a
printf("you done the average %d times", counter);
before the return 0.
The problem is: if I do the average for just 1 or 2 times, the counter show
every time a different number, never the right, but ever around the int maximum. I tried everything, but it don't work. Where is my mistakes?
This is my first post on this site, i read the rules but i'm sorry if i'm breaking just one. The variable "counter" is declared.
int main()
{
int vote1, vote2, vote3, tot, media, contatore, err;
char opz;
do{
after this, i start an while loop, and this is its end:
contatore=contatore+1;
} while(opz!='n');
printf("hai eseguito la media %d volte", contatore);
return 0;
obviously the code is in italian, where counter=contatore
You have to initialise the variable:
int contatore = 0;