I am writing a c program to read two fractions and enter the sum of the fractions. I keep on getting 6487612 and 6487608 as the answer. please help thank you
#include <stdio.h>
int main ()
{
int num1, num2, denom1, denom2, result_num, result_den;
printf("enter first fraction\n");
scanf("%d/%d", &num1, &denom1);
printf("enter second fraction\n");
scanf("%d/%d", &num2, &denom2);
result_num = (denom2 * num1) + (denom1 * num2);
result_den = (denom1 * denom2);
printf("the result is %d/%d", &result_num, &result_den);
return 0;
}
just wanted sum of the two fractions
Related
i just started studying programming at school, and i am so lost already. Here's the task:
Answer all 3 tasks in a separate file and return it.
What is the result if the format string (control character) of the printf function is %.3f and the number to be printed is
456.87654321
0.17023
443.14159
How do i even do this? My code is this but it's obviously wrong.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Give a number 1\n");
scanf("%i", &num1);
printf("Answer is on %.3f", &num1);
return 0;
}
It gave me 0 as answers or 0.000 what ever. Only 0's.
I don't know what to do really, my teacher is already on another subject and has no time helping me much.
This source code:
#include <stdio.h>
int main(void)
{
printf("%.3f\n", 456.87654321);
printf("%.3f\n", 0.17023);
printf("%.3f\n", 443.14159);
}
produces this output:
456.877
0.170
443.142
You declared num1 to be an int (integer... whole numbers only).
Then you read that number from the keyboard.
I'm guessing you're entering 456.87654321.
(hint, that is not a whole number. it will not fit in an int)
Then you try to print it out with:
printf("Answer is on %.3f", &num1);
This has several problems.
%.3f is for printing out doubles, not ints.
You passed the "address" (& sign) of the number you wanted to print. Just pass the variable directly
Fixing up your code, I get:
#include <stdio.h>
int main() {
double num1; // Declare DOUBLE, not INT
printf("Give a number 1\n");
scanf("%f", &num1); // Get a DOUBLE from input, not an INT
printf("Answer is on %.3f", num1); // Remove the & (address)
return 0;
}
I am working on a lab assignment which asks me to take a given number from a user, and output that many Fibonacci numbers, test case being 20. I am also asked to have all program output be put into a file named csis.txt. I think that I have done everything correctly in regards to the syntax, but I get a blank csis.txt file. Does anyone see why I am getting blank output files? Does anyone know how I can get each iteration of the for loop to be included in the output? Thanks.
#include <stdio.h>
FILE *fp;
int fib(int num);
int main(void) {
int num, num1 = 0, num2 = 1;
fopen_s(&fp, "csis1.txt", "w");
printf("How many Fibonacci numbers would you like to output?\n");
fprintf(fp, "How many Fibonacci numbers would you like to output?\n");
scanf_s("%d", &num);
printf("The Fibonnaci sequence up to %d numbers:\n%d\n%d\n", num, num1, num2);
fprintf(fp, "The Fibonnaci sequence up to %d numbers:\n%d\n%d\n", num, num1, num2);
printf(fib(num));
fprintf(fp, fib(num));
fclose(fp);
return 0;
}
int fib(num) {
int fibnum, counter;
int num1 = 0, num2 = 1;
for (counter = 3; counter <= num; counter++) {
fibnum = num1 + num2;
printf("%d\n", fibnum);
fprintf(fp, "%d\n", fibnum);
num1 = num2;
num2 = fibnum;
}
}
I have started an intro to programming course that uses the C language and we have an assignment to make a program that takes a 5 digit number from the user such as 12345 and it prints it out as 1 2 3 4 5.
I tried to google around for help but all the answers given used code way too complicated for my understanding considering the course just started and we have only learned printf and scanf, if and switch statements and while and for loops.
I tried putting all the numbers given into separate int variables which made the program stop and then tried to put them into chars but the testing program said it was wrong since we are supposed to use int.
Is there a simple way to do this?
EDIT:
What I have tried:
#include <stdio.h>
int main(void) {
int num1,
num2,
num3,
num4,
num5;
printf("Give 5 digit number > ");
scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5);
printf("Seperated number is %d %d %d %d %d", num1, num2, num3, num4, num5);
return (0);
}
Also tried that code but with char variable type but that wasn't allowed it has to be int.
The testing program gives an expected output which for 00001 is Given number 1 seperated is 0 0 0 0 1 or for -12321 is Given number -12321 seperated is -1 -2 -3 -2 -1
You can read user input into a char array using scanf.
Then using for loop you can loop through each char (1,2,3,4,5)
Use printf in the loop to print from array and a space. printf("%c ", input_from_user[i]);
I suppose the point here is understanding how C stores things. You're reading in characters (that is a set of 8-byte values that are commonly displayed as letters) and converting each one to an integer (commonly a 32-bit value that stores a number)
So you want to read the input using scanf to get a character string of your 5 digits, then use a for loop to go over each one, reading a single character from the string and converting it into an integer variable. then printf that variable plus a space, and move on to repeat the next digit.
If you read the digits in one by one then its even easier as you only need the for loop to read, convert, and print each digit as its read.
According to the question, you would be given a number of 5 digits and then you have to print the digits of that number as well.
So, in your program, you have taken five digits individually as input, to me it seems wrong. I am giving my solution below:
#include <stdio.h>
int main(void)
{
int num,idx=0,i=0;
int arr[5]; // for storing the five digits;
scanf("%d",&num);
while(num){ //loop will terminate when the num = 0
int digit = num%10;
num = num/10;
arr[idx] = digit;
idx++;
}
//Printing from the last, as the digit are stored in reverse order.
for(int i=4;i>=0;i--){
printf("%d ",arr[i]);
}
return 0;
}
I don't really understand the purpose of this exercise. If you are learning C, the last function you should learn is scanf. It boggles the mind that so many people teach it early. Try very hard not to use scanf at all until you understand the language well enough to realize that you probably never need it. That said, I suppose the point of the exercise is to do something like:
#include <stdio.h>
int
main(void)
{
unsigned num;
char buf[6];
printf("Give 5 digit number > ");
if( scanf("%u", &num) != 1 || num > 99999 ) {
fprintf(stderr, "Invalid input\n");
return 1;
}
printf("Separated number is");
snprintf(buf, sizeof buf, "%d", num);
for( int i = 0; buf[i]; i++ ) {
printf(" %c", buf[i]);
}
putchar('\n');
return 0;
}
Although likely beyond OP's stage, code can use recursion to print the value from most significant to least.
#include <stdio.h>
void print_digit(int n, int depth) {
if (depth > 1) print_digit(n/10, depth - 1); // print "left" digits first
printf(" %d", n%10);
}
void print_5(int n) {
// To do: checking for correct spelling
printf("Given number %d seperated", n); // or separated
print_digit(n, 5);
printf("\n");
}
int main(void)
{
print_5(1);
print_5(-12321);
return 0;
}
Output
Given number 1 seperated 0 0 0 0 1
Given number -12321 seperated -1 -2 -3 -2 -1
You could use your approach by limiting the number of bytes you read for each number:
#include <stdio.h>
int main(void) {
int num1, num2, num3, num4, num5;
printf("Give 5 digit number > ");
if (scanf("%1d%1d%1d%1d%1d", &num1, &num2,&num3, &num4, &num5) == 5) {
printf("Separated number is %d %d %d %d %d\n", num1, num2, num3, num4, num5);
}
return 0;
}
Note however that this is probably not what you are expected to do. You should instead read a single number and decompose it into its five digits:
#include <stdio.h>
int main(void) {
int num;
printf("Give 5 digit number > ");
if (scanf("%d", &num) == 1 && num >= 10000 && num <= 99999) {
printf("Separated number is %d %d %d %d %d\n",
num / 10000, num / 1000 % 10, num / 100 % 10, num / 10 % 10, num % 10);
}
return 0;
}
Im trying to write a calculator program. i have wrote the first part of it but i keep get the same error: invalid operands of types unsigned int*' andchar[80]' to binary `operator&'
Please help me
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned int num1, num2, num3;
char s[80];
int main (){
printf("type in an expression: ");
scanf(" %x %s %x\n", &num1 &s &num2);
if(strcmp ("add", s) == 0){
num3 = num1 + num2;
}
if(strcmp("subtract", s) == 0){
num3 = num2 - num1;
}
printf("the answer is: %x", num3);
}
try:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned int num1, num2, num3;
char s[80];
int main (){
printf("type in an expression: ");
scanf(" %x %s %x", &num1, s, &num2);
if(strcmp ("add", s) == 0){
num3 = num1 + num2;
}
if(strcmp("subtract", s) == 0){
num3 = num2 - num1;
}
printf("the answer is: %x\n", num3);
system("pause");
}
note: notice that I remove \n in the scanf..
As Yohanes mentions, you need to have the commas in between the arguments in the scanf, otherwise the compiler is trying to do: get the address of num1 (&num1) and logically AND it with the address of the array s (address is implied here because it is an array) and logically AND that with the value contained in num2.
I would suggest that you add an else between the two if statements since they are mutually exclusive.
Furthermore, you probably want to add a \n to the printf statement
printf("the answer is: %x\n", num3);
to flush the output.
#include <stdio.h>
#include <conio.h>
main() {
float num1, num2, num3, num4, num5, sum;
printf("Enter a Number between");
fflush;
scanf("%f",&num1);
fflush;
printf("Enter a Number between");
scanf("%f",&num2);
fflush;
printf("Enter a Number between");
scanf("%f",&num3);
fflush;
printf("Enter a Number between");
scanf("%f",&num4);
fflush;
printf("Enter a Number between");
scanf("%f",&num5);
fflush;
sum = num1 + num2 + num3 + num4 + num5;
printf("The sum of the five numbers you have entered is %f",sum);
getch();
}
I am a newbie in c programming. We have an assignment and I have created the above code. But we need a shorter solution. The user must input five numbers and display the sum. Can you please help me to translate this code using do while function or post test loop. Thank you very much in advance!
You can use a cycle to read 5 values and accumulate their sum. I prefer to leave you with this hint only because this seems like a homework assignment. You may reuse the same variable reading 5 different inputs and have a separate variable in which you accumalate the sum. You can also use a for cycle instead of the do... while you seem to be using.
Use a for loop to input numbers (say 5 in this case) and add it with value stored in sum in each iteration.
int num , sum = 0;
for(int i = 0; i < 5; i++)
{
scanf("%d", &num);
sum += num;
}
When someone asks me to do their homework for them, I enjoy coming up with a slightly convoluted, but functionally correct answer. :)
#include <stdio.h>
#include <conio.h>
int main()
{
float numbers[5] = {0.0F};
float sum = 0.0F;
int count = 5;
while(count --> 0)
{
printf("Enter a number for entry %d: ", 5-count);
scanf("%f",numbers+count);
sum += numbers[count];
}
printf("The sum is %f\n", sum);
getch();
return 0;
}