How this formal charge calculator works
Give it one atom out of your Lewis structure: the element symbol (or its valence electron count, if you would rather supply that yourself), how many nonbonding electrons sit on it, and how many bonds it makes. You get the formal charge with the arithmetic written out, the same way you would show it on paper. Textbooks split on how to teach the formula: some subtract half the bonding electrons, some just count bonds. A switch above accepts either, because they are the same calculation wearing two outfits, and the steps show the divide-by-two whenever it happens so you can see why.
Or switch to whole structure mode and tally every atom, up to 8 rows, grouping identical atoms in the same bonding situation. The page then runs the one check every Lewis structure has to pass: the formal charges must add up to the overall charge of the molecule or ion. When they do not, the gap tells you exactly how many electrons your drawing is missing, which is a far more useful thing to hear than "wrong".
The formula
equivalently: formal charge = valence electrons - lone electrons - bonds
Valence electrons are what the free atom brings: for a main-group element that is its group number, minus 10 for groups 13 to 18, so carbon brings 4 and oxygen brings 6. Nonbonding electrons are the ones sitting on the atom in lone pairs, counted as electrons, so one lone pair is 2. Bonding electrons are the ones in bonds touching the atom, and since every bond holds exactly 2 electrons and formal charge splits each bond evenly, dividing them by 2 is the same as counting bonds: a single bond contributes 1, a double bond 2, a triple bond 3.
Worked example
Ammonium, NH4+. Nitrogen sits in group 15, so it brings 5 valence electrons. In ammonium it holds no lone pairs and makes 4 bonds: formal charge = 5 - 0 - 4 = +1.
Each hydrogen brings 1 valence electron, keeps no lone pairs, and makes 1 bond: 1 - 0 - 1 = 0, four times over.
Add them up: +1 from the nitrogen and four zeros from the hydrogens is +1, which is exactly the charge on the ion. The plus sign in NH4+ is not decoration; it lives on the nitrogen, at least as far as the bookkeeping is concerned.
Zero is the answer you want
Run CO2 and every atom comes back 0. That can feel like the calculator shrugged, but zeros are the whole point: an atom with formal charge 0 owns exactly as many electrons in your drawing as it would own on its own, and a structure full of zeros is a structure with nothing strained about it. When your answer is a row of zeros, you are probably done.
Then there is carbon monoxide, the famous exception. The best structure of CO is a triple bond with a lone pair on each atom, and the arithmetic says carbon is 4 - 2 - 3 = -1 while oxygen is 6 - 2 - 3 = +1. That looks backwards, because oxygen is the more electronegative atom and you would expect the minus sign to sit on it. The structure is still the best one on offer: it is the only drawing that fills both octets, and the two charges sum to the molecule's actual charge of zero. The lesson CO teaches is the one to carry everywhere: formal charge describes your drawing, not the molecule. (Delightfully, the real molecule takes carbon's side: CO's measured dipole is tiny, with the carbon end negative, so the strange-looking bookkeeping points the right way after all.)
The sum is the built-in error check
Formal charges across a structure must add up to the overall charge: 0 for a neutral molecule, +1 for ammonium, -2 for sulfate. This is not a convention; it is arithmetic. Every valence electron in the structure is assigned to exactly one atom or split evenly between two, so nothing can leak out of the total. Whole structure mode checks it for you, and when the sum misses, it can tell you something precise: the gap equals the number of electrons your drawing is missing or has extra. Off by one unit of charge means off by exactly one electron. So do the thing every instructor keeps saying: count the total valence electrons first (every atom's valence electrons, plus one per unit of negative charge, minus one per unit of positive), and make the drawing place exactly that many.
Sulfate: one ion, two drawings, two sets of charges
Draw sulfate, SO42-, with two double bonds and an expanded octet on sulfur, and the charges come out: sulfur 0, the two double-bonded oxygens 0, the two single-bonded oxygens -1 each. Draw it with four single bonds and a tidy octet, and they become: sulfur +2, all four oxygens -1. Both drawings sum to -2. Both are legal. The atoms did not move; only the bookkeeping did, and that is the whole reason chemists compute formal charges at all: they are how you choose between different drawings of the same thing.
The selection rules you are taught: prefer the structure with the smallest formal charges, and put any negative charge on the more electronegative atom. By those rules the expanded-octet drawing wins, which is why textbooks print it. Honesty compels a footnote on the tiebreaker, though: computational chemists have spent decades showing that sulfur's d orbitals barely participate in the bonding, so the "expanded octet" is itself more drawing than reality. Formal charge picks the better picture; it does not settle what the electrons are actually doing, and no bookkeeping scheme can.
Formal charge is not oxidation state
They are opposite fictions run on the same drawing. Formal charge pretends every bond splits evenly, no matter who is on the other end. Oxidation state pretends the more electronegative atom takes both electrons of every bond, no sharing at all. Carbon in CO2 is the cleanest demonstration you will ever get: formal charge 0, oxidation state +4, from the same molecule on the same afternoon. Neither number is where the electrons actually are; the truth sits in between, closer to even sharing when the two atoms have similar electronegativities and closer to the oxidation-state picture when they do not. Use formal charge to judge Lewis structures, use oxidation state to track electrons through redox reactions, and use neither to settle an argument about "the real charge".
Why the transition metals are not in here
Type Fe or Cu and this page declines, on purpose. For a main-group atom the valence electron count is settled and formal charge is pure arithmetic. For a d-block center it depends on decisions: how to count the d electrons, what to do with the bonds ligands donate, whether you are using the ionic or the covalent counting scheme. Inorganic chemists keep several formalisms precisely because different jobs need different bookkeeping, and a page that returned one confident number for iron would be faking a judgment call. Faking is the one thing a calculator must never do. If you are counting atoms and grams rather than charges, our molar mass calculator handles any formula, d-block and all, and the mole calculator and percent yield calculator carry the chemistry on from there.