Docsity

It includes 17+ beginner programs, with: Question Logic C code Sample output Explanations, Study notes of Computer Science

Partial preview of the text

Download It includes 17+ beginner programs, with: Question Logic C code Sample output Explanations and more Study notes Computer Science in PDF only on Docsity!

C PROGRAMMING

Important Programs for Beginners

BTech CSE • First-Year Practice • Beginner Friendly

Question → Logic → C Program → Expected Output

Original practice notes for learning and revision.

1. How to Approach a C Program

Before writing code, understand the problem and break it into small steps.

Simple method

  • Read the question carefully.
  • Identify the inputs.
  • Identify the required output.
  • Choose variables and data types.
  • Write the logic in simple steps.
  • Convert the logic into C syntax.
  • Test the program with sample values.

Example: Add two numbers

Input: two numbers. Process: add them. Output: their sum.

int a, b, sum; scanf("%d %d", &a;, &b;); sum = a + b; printf("%d", sum);

This input-process-output approach works for many beginner programs.

3. Program 2 — Add Two Numbers

Question: Input two integers and print their sum.

Logic

  • Read a and b.
  • Calculate sum = a + b.
  • Print sum.

Code

#include

int main() { int a, b, sum; printf("Enter two numbers: "); scanf("%d %d", &a;, &b;);

sum = a + b; printf("Sum = %d", sum);

return 0; }

Sample output

Enter two numbers: 10 20 Sum = 30

4. Program 3 — Basic Arithmetic

Question: Input two integers and display addition, subtraction, multiplication, and remainder.

Code

#include

int main() { int a, b; scanf("%d %d", &a;, &b;);

printf("Addition = %d\n", a + b); printf("Subtraction = %d\n", a - b); printf("Multiplication = %d\n", a * b); printf("Remainder = %d\n", a % b);

return 0; }

Example: For 10 and 3 → Addition 13, Subtraction 7, Multiplication 30, Remainder 1.

Concept: arithmetic operators.

6. Program 5 — Positive, Negative or Zero

Question: Check whether a number is positive, negative, or zero.

Code

#include

int main() { int n; scanf("%d", &n;);

if (n > 0) printf("Positive"); else if (n < 0) printf("Negative"); else printf("Zero");

return 0; }

Concept: else-if ladder.

7. Program 6 — Pass or Fail

Question: Input marks and print Pass if marks are 40 or above; otherwise print Fail.

Code

#include

int main() { int marks; scanf("%d", &marks;);

if (marks >= 40) printf("Pass"); else printf("Fail");

return 0; }

Sample

Input: 67 Output: Pass

Concept: relational operator and if-else.

9. Program 8 — Largest of Three Numbers

Question: Find the largest among three integers.

Code

#include

int main() { int a, b, c, largest; scanf("%d %d %d", &a;, &b;, &c;);

largest = a;

if (b > largest) largest = b;

if (c > largest) largest = c;

printf("Largest = %d", largest);

return 0; }

Logic: Assume a is largest, then compare b and c with the current largest.

10. Program 9 — Multiplication Table

Question: Print the multiplication table of a number from 1 to 10.

Code

#include

int main() { int n; scanf("%d", &n;);

for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); }

return 0; }

Sample output for 5

5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50

Concept: for loop.

12. Program 11 — Even Numbers from 1 to 20

Question: Print all even numbers from 1 to 20.

Method 1 — Check each number

for (int i = 1; i <= 20; i++) { if (i % 2 == 0) printf("%d ", i); }

Method 2 — Start at 2

for (int i = 2; i <= 20; i += 2) { printf("%d ", i); }

The second method is shorter because it directly visits even numbers.

13. Program 12 — Sum of First N Numbers

Question: Input N and find 1 + 2 + ... + N.

Code

#include

int main() { int n, sum = 0;

scanf("%d", &n;);

for (int i = 1; i <= n; i++) { sum += i; }

printf("Sum = %d", sum); return 0; }

Example

If N = 5, sum = 1 + 2 + 3 + 4 + 5 = 15.

Concept: accumulator variable.

15. Program 14 — Factorial

Question: Find the factorial of a positive integer.

For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.

Code

#include

int main() { int n; long long fact = 1;

scanf("%d", &n;);

for (int i = 1; i <= n; i++) { fact *= i; }

printf("Factorial = %lld", fact); return 0; }

Concept: multiplication accumulator. This beginner version assumes a suitable positive input.

16. Program 15 — break

Question: Print numbers from 1 to 10 but stop when the number reaches 6.

#include

int main() { for (int i = 1; i <= 10; i++) { if (i == 6) break;

printf("%d ", i); } return 0; }

Output: 1 2 3 4 5

Remember: break completely exits the loop.

18. Program 17 — Nested Loops & Simple Pattern

Question: Print a simple triangle of stars.

Code

#include

int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { printf("* "); } printf("\n"); }

return 0; }

Output

The outer loop controls rows. The inner loop controls how many stars are printed in each row.

19. Practice Set — Try Yourself

Write these programs without looking at the previous pages.

  1. Find the smallest of two numbers.
  2. Check whether a number is divisible by 5.
  3. Print odd numbers from 1 to 25.
  4. Find the sum of even numbers from 1 to N.
  5. Print a number's table using while.
  6. Print numbers from N down to 1.
  7. Find the factorial of a number using while.
  8. Stop a loop when the input value becomes 0 using break.
  9. Skip multiples of 3 using continue.
  10. Print a rectangle of stars using nested loops.

Challenge

Write one program that uses a loop, if condition, continue, and break together.