Module 1 · Meet C: Your First Programs, Symbol by Symbol
Problems: First Programs
In this lesson
- Read a problem written as Input, Output, Constraints and one sample.
- Write a program whose output matches the expected output exactly.
- Test your program on the edges of the constraints before you submit it.
Everything you have read so far is behind you. This page is ten problems, graded by a machine that compares what your program printed with what it should have printed, character by character.
Eight of them you have already met inside the lessons. Two are new. None of them needs a loop or a condition, because this module has not taught either.
How a judge reads your program
The judge is not clever, and that is the point. For each hidden test it does four things.
It runs your program. It feeds the test's input to the program's standard input, exactly as the input box in the Playground does. It collects everything your program printed. Then it compares that with the expected output.
The comparison ignores two things and nothing else: spaces at the end of a line, and newlines at the very end of your output. Everything else counts. A capital letter where a small one was expected is a wrong answer, and so is a missing space after a colon.
So a program that prints the right numbers in the wrong shape scores zero. That feels harsh for about a day, and then it becomes a habit.
The shape of every problem here
Every problem on this page has the same five parts, and so does every problem on a real judge.
- The story names who wants what. It is context, not a specification.
- Input says exactly what arrives on standard input, and in what order.
- Output says exactly what to print. Read this one twice.
- Constraints give the range of every value. They tell you which tests exist.
- The sample is one input with its output, so you can check your understanding before you write anything.
The hidden tests are not shown, and there are six or more of each problem. They always include the smallest allowed input, the largest, and one edge the story hints at.
So Constraints is the most useful section on the page: it is a list of the tests you should try by hand.
Reading input, before Module 3 teaches it
Every starter here already reads the input for you. scanf is properly taught in Module 3. Until then you can treat that line as furniture: it puts the input into the variables, and your job starts on the next line.
Two details are worth noticing now, because two problems depend on them.
scanf("%d", &n) reads one whole number and skips any spaces or newlines before it. scanf("%c", &ch) reads exactly one character, whatever it is, including a space.
So a format string with no space in it is a deliberate choice in the character problems. Changing it to " %c" breaks the test where the input is a space.
How to test before you submit
Four checks, in this order, catch almost every wrong answer on this page.
- Run the sample. If the sample fails, nothing else matters.
- Run the two ends of the constraints. The smallest allowed value and the largest.
- Read your output beside the expected output. Count the spaces. Check every capital letter.
- Check the last line. A program that prints no final newline is fine here; one that prints an extra word is not.
So testing is not optional work after the program is written. It is how you find out that the program is finished.
The hint ladder
Every problem below carries three steps you can open in order. Hint 1 says what to notice and Hint 2 describes the approach in words. Solution then explains the whole method in two paragraphs.
Opening a hint costs you nothing and is recorded, not punished. Type the program yourself even after reading the solution, because the reading is not the skill.
The read, compute, print skeleton
#include <stdio.h>
int main(void)
{
int n; declare what the input needs
scanf("%d", &n); read it, this line is given to you
printf("...", n); print exactly what Output asks for
return 0;
}
- The declaration comes first, so
scanfhas somewhere to put the value. &nis "the address of n", which is howscanfreaches the variable. Module 11 explains it.- Nothing else is printed. No prompt, no heading, no blank line.
return 0;tells the judge the program finished normally.
The problem: read two integers and print their sum. Input is one line with two integers, output is one line with one number.
#include <stdio.h>
int main(void)
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
return 0;
}
11
That output is for the input 7 4. Notice what is not in the program: no "Enter two numbers" prompt, no "The sum is" label, nothing the Output section did not ask for.
Half the problems here are exact-output problems, so this is the shape to be comfortable with. One tab, one quote, one percent sign, all in three lines.
#include <stdio.h>
int main(void)
{
printf("Name:\tZara\n");
printf("Note:\t\"first attempt\"\n");
printf("Score:\t80%%\n");
return 0;
}
Name: Zara
Note: "first attempt"
Score: 80%
Compare the source with the output, character by character. Three pairs in the source became one character each in the output, which is the whole of lesson 3's escape table in one program.
Run in CompilerThree of the problems read a character rather than a number. This is that read, and the two ways to print what came in.
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
printf("%c%c%c\n", ch, ch, ch);
printf("%d\n", ch);
return 0;
}
QQQ
81
That output is for the input Q. The same variable printed twice, once as a character and once as a number, which is lesson 4's one idea.
Where this is used
- Every programming contest. ICPC, Codeforces and the Progsity contest platform all judge exactly this way: hidden tests, standard input, an exact comparison of your output.
- Golden-file tests in real projects. Compilers and command line tools are tested by running them and comparing the output with a saved file. GCC's own test suite works like this.
- CI regression checks. A build that prints one extra line fails the diff, which is how a team notices an accidental change in a tool's output.
- Automated marking. The graded exercises later in this track, and the Skill Test's build section, use the same judge as these ten problems.
Common mistakes
1. Printing a prompt.
printf("Enter the stall number: ");
scanf("%d", &n);
No compiler message; the program looks friendlier and scores zero. The judge sees Enter the stall number: === PROGSITY === and compares it with === PROGSITY ===. Print nothing that Output did not ask for.
2. Spaces that are not in the expected output.
printf("Maths : %d\n", a);
One space too many, before the colon. This is the single most common wrong answer on exact-output problems, and the only cure is reading the sample beside your output.
3. Changing the given scanf line.
scanf(" %c", &ch);
The extra space tells scanf to skip whitespace, so the test whose input is a single space reads the newline instead. The program passes six tests and fails one, which is the most confusing kind of failure to debug.
4. Solving a different problem.
printf("Area is %d\n", 4 * s);
No message, and the sample even passes. Perimeter and area agree at side 4 and nowhere else. When a sample passes and the tests do not, read the Output section again, out loud.
Alice sells books at a school fair and needs a sign for her stall. The sign is two lines: a fixed banner, then the word Stall and her stall number.
Input. One line with one integer n, the stall number.
Output. Two lines. The first is exactly === PROGSITY ===. The second is Stall, one space, then n.
Constraints. 1 <= n <= 999.
Sample. Input 7 gives === PROGSITY === then Stall 7.
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
/* Your two printf calls go here. */
return 0;
}
Run in Compiler
Hint 1
The banner never changes, whatever the input is. Only one of the two lines has a number in it.
Hint 2
Two printf calls. The first takes a fixed string and no value; the second takes a string with one %d in it and the variable n.
Solution
Print the banner with a single fixed string, ending in \n. Then print the second line with a format string that reads Stall %d and pass n as the value. Nothing else is printed at all.
The one thing to count is the banner: three equals signs, a space, the word, a space, three equals signs. Copy it from the Output section rather than typing it from memory, because a fourth equals sign is a wrong answer on every test.
Maria wants her three subject marks printed as a small card, one subject per line, using three separate printf calls.
Input. One line with three integers a b c, the marks for Maths, Physics and Chemistry.
Output. Three lines: Maths: a, Physics: b, Chemistry: c. One space after each colon.
Constraints. 0 <= a, b, c <= 100.
Sample. Input 90 85 77 gives Maths: 90, Physics: 85, Chemistry: 77.
#include <stdio.h>
int main(void)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
/* Three printf calls, one per subject. */
return 0;
}
Run in Compiler
Hint 1
The three values are already in a, b and c when your first line runs. The order of your calls is the order of the output.
Hint 2
Each line is a subject name, a colon, a space and one %d. Write the first, then copy it twice and change two things in each copy.
Solution
Three calls, in the order Maths, Physics, Chemistry, each with a format string of the shape Subject: %d followed by \n, and each passing its own variable. Statements run top to bottom, so the order you write is the order the judge sees.
The trap is the space after the colon. Maths:%d and Maths : %d are both wrong, and both look almost right on your screen. Take the sample output and count.
Bob's report line has to carry a tab and a pair of quotation marks. A bare quote would end the string early, so it needs an escape.
Input. One line with one integer s, Bob's score.
Output. Two lines. The first is Bob wrote:, one tab, then "Hello, C." with the quotes printed. The second is Score:, one tab, then s.
Constraints. 0 <= s <= 100.
Sample. Input 95 gives Bob wrote:, a tab, "Hello, C.", then Score:, a tab, 95.
#include <stdio.h>
int main(void)
{
int s;
scanf("%d", &s);
/* Two printf calls, with two escape sequences in the first. */
return 0;
}
Run in Compiler
Hint 1
A tab is one character, and it is not four spaces. Look up its escape in lesson 3's table, then look up the one for a quote.
Hint 2
Inside a string, a quote needs a backslash in front of it. Without one the string ends there, and the compiler complains about the rest of the line.
Solution
The first line's format string holds the words Bob wrote:, then \t, then an escaped quote, the message, another escaped quote, and \n. The second line is Score:, \t, %d, \n, with s as its value.
If the build fails with a message about a missing terminating quote, you have written a bare " in the middle of the string. That is the error this problem exists to teach, so read it once before you fix it.
Bob's doubling program has three syntax errors and nothing else wrong. Repair it until it compiles, then check that it prints the right answer.
Input. One line with one integer n.
Output. One line: Double of n is 2n, with both numbers filled in.
Constraints. 0 <= n <= 1000.
Sample. Input 21 gives Double of 21 is 42.
#include <stdio.h>
int main(void)
{
int n
scanf("%d", &n);
Printf("Double of %d is %d\n", n, n * 2);
return 0;
Hint 1
Three mistakes, and the compiler will name two of them almost exactly. Fix the first message it gives you and build again.
Hint 2
One is a missing semicolon, and the message for it names the next line. One is about capital letters. One is about counting braces.
Solution
The declaration of n needs a semicolon. Printf needs a small p, because C is case sensitive and nothing defines the capitalised name. And the function needs its closing brace, which is what the "end of input" message is about.
The arithmetic was correct all along. That is the point of the problem: three messages, three one-character repairs, and no thinking about the answer itself.
This program compiles on the first try and is still wrong. Maria wants the area of a square tile, and the program prints the distance around it.
Input. One line with one integer s, the side of the square in centimetres.
Output. One line: Area is X, where X is the area in square centimetres.
Constraints. 1 <= s <= 100.
Sample. Input 4 gives Area is 16. The starter prints 16 for this input too, which is the trap.
#include <stdio.h>
int main(void)
{
int s;
scanf("%d", &s);
printf("Area is %d\n", 4 * s);
return 0;
}
Run in Compiler
Hint 1
Run the starter with the input 5 and work out on paper what the answer should be. The sample is the one input where the bug is invisible.
Hint 2
Four times the side is the distance around the tile. Area is the side multiplied by itself.
Solution
Change the expression from 4 * s to s * s and nothing else. The output text, the newline and the read are all already correct, which is what makes this a semantic bug rather than a syntax one.
Keep the lesson, not the fix: a sample that passes proves nothing about the other tests. Side 4 is the only length where perimeter and area agree. A test set with only side 4 in it would have called the broken program correct.
Zara was taking notes by hand and wrote the names of the symbols instead of the symbols. Put the real symbols back, so the program prints the expression and its value.
Input. One line with two integers a b.
Output. One line, exactly: (a + b) * 2 = X, with the two numbers and the answer filled in.
Constraints. 0 <= a, b <= 1000.
Sample. Input 7 4 gives (7 + 4) * 2 = 22.
#include <stdio.h>
int main(void)
{
int a, b;
scanf("%d %d", &a, &b);
/* Print: open-paren, a, space, plus, space, b, close-paren,
space, asterisk, space, 2, space, equals, space, the value. */
printf("%d\n", 0);
return 0;
}
Run in Compiler
Hint 1
Some of those symbols are text to be printed, and one of them is arithmetic the program has to do. Decide which is which before you type.
Hint 2
One printf, one format string with three %d in it, and three values after it. The brackets and the asterisk sit inside the string as ordinary characters.
Solution
The format string is an open parenthesis, %d, a space, a plus, a space, %d and a close parenthesis. Then a space, an asterisk, a space, 2, a space, an equals sign, a space, %d and a newline. The three values are a, b and the expression (a + b) * 2.
Notice that the parentheses appear twice in two different jobs: as characters inside the string, and as grouping in the expression. Nothing in C confuses the two, because one is inside quotes and one is not.
Maria is building a tool that shows the code of any key. Read one character and print its decimal ASCII code.
Input. One line holding exactly one character, which may be a space.
Output. One line with its decimal ASCII code.
Constraints. The character is printable ASCII, code 32 to 126.
Sample. Input A gives 65.
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
/* One printf. Which specifier do you need? */
return 0;
}
Run in Compiler
Hint 1
The character is already a number. You do not have to convert anything; you have to print the same byte through a different lens.
Hint 2
One printf, one specifier, one variable. Lesson 4's first example printed the same variable both ways.
Solution
Print ch with %d and a newline. That is the whole program: the byte the input put into ch is its ASCII code, and %d asks printf to show the number rather than the shape.
Leave the given scanf exactly as it is. A space before the %c would make the program skip whitespace, and one of the hidden tests hands you a space on purpose.
Amara is checking a form where three digits arrive as characters, with nothing between them. Add up what they stand for.
Input. One line with exactly three digit characters and no spaces.
Output. One line with the sum of the three digit values.
Constraints. Each character is '0' to '9'.
Sample. Input 407 gives 11.
#include <stdio.h>
int main(void)
{
char a, b, c;
scanf("%c%c%c", &a, &b, &c);
/* Trick two, three times. */
return 0;
}
Run in Compiler
Hint 1
Print one of the characters with %d first, to see what you are actually holding. The digit 4 does not arrive as 4.
Hint 2
Each character is 48 more than the digit it shows. Lesson 4 gives you a way to write that 48 without typing a number.
Solution
Subtract '0' from each of the three characters and add the three results, inside one printf with %d. Writing a - '0' rather than a - 48 is the readable form, and both give the same machine code.
The check that matters: input 000 must print 0. If it prints 144, you added the codes and not the values, which is the mistake this problem exists to catch.
Kenji prints a small receipt footer. It carries a Windows path, a percentage and a quoted note, which is three escapes in three lines.
Input. One line with one integer n, the discount percentage.
Output. Three lines. Path:, a tab, then C:\progsity\bin. Then Discount:, a tab, n, a percent sign. Then Note:, a tab, and "keep the receipt" in quotes.
Constraints. 0 <= n <= 100.
Sample. Input 15 gives Discount:, a tab, then 15% on the middle line.
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
/* Three printf calls. Watch the backslashes and the percent sign. */
return 0;
}
Run in Compiler
Hint 1
Two characters in this output cannot be typed straight into a format string. One is the backslash, and the other is the sign that starts a placeholder.
Hint 2
Both of them are written twice in the source to print once. Lesson 3's escape table has the backslash; the last row of it has the other one.
Solution
Three calls. The first prints Path:, a tab, and the path with each backslash doubled in the source. The second prints Discount:, a tab, %d for the number, then two percent signs, which printf turns into one. The third prints Note:, a tab, and the note with an escaped quote at each end.
A single backslash before an ordinary letter is the trap: \p is undefined and \b is a backspace that eats the character before it. If your path looks almost right, count the backslashes in your source, not in the output.
Zara is labelling shelves and needs two facts about a letter: where it sits in the alphabet, and how it looks in capitals. No library function, only arithmetic.
Input. One line with one small letter, 'a' to 'z'.
Output. Two lines. The first is the letter, then is letter number, then its position, with a counting as 1. The second is uppercase:, one space, and the letter in capitals.
Constraints. The input is always a small letter.
Sample. Input c gives c is letter number 3 then uppercase: C.
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
/* Two printf calls, and two of the three tricks. */
return 0;
}
Run in Compiler
Hint 1
Two of lesson 4's three tricks are in this problem. One gives you a position, and one changes the case.
Hint 2
'a' - 'a' is 0, and the problem wants a to be number 1. The capital of a letter is 32 below it.
Solution
The first line prints the character with %c and the position with %d, where the position is ch - 'a' + 1. The plus one is the whole difference between a position and an offset, and forgetting it is the most likely wrong answer here.
The second line prints ch - 32 with %c. Writing ch - ('a' - 'A') instead is longer, says what it means, and gives exactly the same result; either passes.
Common doubts
How many times can I submit?
As often as you like. A submission that fails costs you nothing except the run, and reading the failed test's number tells you which edge you missed.
Does the judge care about my variable names or my comments?
No. It reads only what your program printed. Write the names and comments for the next human, exactly as lesson 5 asks.
Do I need a newline at the end of the last line?
It is ignored either way here, because the comparison drops newlines at the very end. Print it anyway: most judges elsewhere expect it, and it costs one character.
Can I read the input differently from the starter?
You can, and in two problems you should not. The character problems depend on
%cwith no space before it, which is explained in the third section above.Is it cheating to read the solution?
No, and it is recorded rather than punished. Read it, close it, and type the program from your own understanding; the marks come from the judge, and the skill comes from the typing.
Key takeaways
- A judge feeds your program input, collects its output, and compares it exactly.
- Only trailing spaces and trailing newlines are forgiven. Everything else counts.
- A prompt, a label or a heading that Output did not ask for is a wrong answer.
- Constraints are the list of tests to try by hand, starting at both ends of the range.
- The given
scanfline is part of the problem; changing it can break one test in six. - Hints are free and recorded, and typing the program yourself is the part that teaches.
Next is the module test: ten questions on everything in Part Zero and this module, plus two of these problems again.
Module test
Ten questions on this module. Pass at 70%, and you can take it as many times as you like.
Take the module testEnd of lesson 7
Get every problem accepted, and the lesson is done.
0 of 10 problems accepted
Next: Module Test: Meet C