1.) Here is my program, the error message follows. Supposedly the % is for modulus and is supposed to give me a remainder?
2.) If I did a scanf, and entered any interger or float value, how would I make the program display the last character of the interger. ex: enter a number: 12 "the last character is 2" . How would I do this with simple code (as simply as possible)?

#include <stdio.h>

int main(){
float c;
printf("Enter any integer");
scanf("%f", &c);
printf("%f",c%2)
return 0;
}

here is the error message when I try to compile it: "program2.c:9: error: invalid operands to binary" Do I need to include another .h file?

so say I had this script with the same problem, how would I fix this script if I have already defined variables a and b as regular integers?

if ((a%2)==0)
{ if ((b%2)==0)
printf("both%1.0f and %1.0f are even\n",a,b);
else
printf("1.0f is even, but %1.0f is odd\n",a,b);}
else
{if((b%2)==0)
printf("%1.0f is odd, but %1.0f is even\n",a,b);
else
printf("both %1.0f and %1.0f are odd\n",a,b);}

I get the following error message: program2.c:9: error: expected expression before '%' token

0

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

By the way C is a programming language, not a scripting language. The difference is that C is translated to actual machine instructions whereas a scripting language is obeyed line by line by some kind of interpreter. Java sits half-way: it's compiled to byte code (the *same* byte code on *any* platform) but then needs a Java Virtual Machine (jvm, or "run machine") to run it.
Before I answer part 2, please confirm whether this is homework. We experts are not allowed to do your homework for you, but can give you assistance in how you can do it yourself.

But that is not what I want anybody to do. I was hoping I could use this website I pay for to ask questions about things I am getting stuck on. Thank you very much.

So, If I can only use the % (modulus) operator on integers, how do I switch a float variable to an integer variable and keep the variables the same? Say, to change a float a; to an int a; where a keeps the same value?

I have no idea even how to go about the second question with regular addition, subtraction, multiplication, or division. Or even if it is possible.

>how do I switch a float variable to an integer variable and keep the variables the same? Say, to change a float a; to
>an int a; where a keeps the same value?
Why do you need to use a float? The operations you are performing are valid on an int, your assignment asks you to use ints ... I dont see the need to use floats ...
If you still insist you can simply assign a float to an int
float b = 2.2;
int a = b;
now a holds value 2. The fraction part of the float is simply dropped - this is all assuming that float is not large enough to overflow the int.
Apart from that you can use functions like floor() ciel() round() etc. Check their man pages for more information

Question 2 is simple number system maths

Given any number ab, it can be represented as a*10 + b ... 12 = 1*10 + 2
abc = 100*a + 10*b + c .... 123 = 1*100 + 2*20 + 3
abcd = 1000*a + 100*b + 10*c + d 1234 = 1*1000 + 2*100 + 3*10 + 4

kth digit here is (counting from 1 starting from least significant)
%10^k / 10^(k-1)

Replace 10 with base of any number system that you wish to work with.

I think q1 gives a clue to q2. Use the % operator - a number % 10 is the last digit of that number (to the base 10). % 8 gives the last digit if expressed in octal (to the base 8). % 16 gives the value of the last digit if expressed in hex - but you would need to convert the values 10 - 15 to a -f (or A - F) to get the last digit as a hex digit.
Try changing your original program to use %9 instead of %2 so you can prove it for yourself
As sunnycoder said - why use floats at all? But if you want to input a float and convert it to an integer then possibilities include:

1. cast: this will discard any fractional part. i = (int)f;
2. Round to nearest integer i = (int)rint(f);
Here rint() returns a double which has a value of the nearest integer to its argument. Integer arithmetic is exact even in floating point, so the (int) cast does not lose precision (unless the value is too big to fit: double can hold a 48-bit int exactly (i.e. with exponent zero) but int is only 32 bits).
I suspect you haven't reached these topics in your coursework yet.

ok, if I use %10d it doesn't work, it puts in 10 spaces. If I use %10 it just prints %10 instead of the variable. If I use %d10 it just puts 10 at the end of the number?

Holy crap guys I got it working, thanks for the help. I couldn't have gotten it done without SOME questions answered. The only thing that I did that I wasn't supposed to do is use more than 2 variables. I used int variables so my % operator would work. But I had to make those float variables too so if they averaged out to a number and a half it would actually show it. I wouldn't know how to do this with just int or float variables. I answered my own previous question and I'll assign points in a few days

>But I had to make those float variables too so if they averaged out to a number and a half it would actually show it.
Numbers themselves should be int ... The average that you calculate would be a float. float = (int + int)/2 is valid.

I think (int + int) / 2 will use integer arithmetic and thus lose the .5, even though you then assign the result to a float. You have to do (int + int) / 2.0 so everything is floated before doing the arithmetic.
Stay with int for everything except the result.. Total the ints as you read them and keep an integer count of how many you have read. when you go to do the division, cast one of them, e.g. if you have int total for the total and int nr for the number read and float avg for the average:

avg = float(total) / nr

(That's for a putative assignment where you read an arbitrary number of integers terminated by a zero and print the average. Might be best to have total as a float, to avoid integer overflow during the addition).

Usually people use double rather than float, because float is only 32 bits in total so has limited integral range. The hardware only ever does double arithmetic, and converts to the short float format on storing. (May not apply to all architectures)

0

Featured Post

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention. Check out this how-to article for more information.

with

printf("%f", %2);