C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Problem Statement
  • Hints
  • Algorithm
  • Pseudocode
  • C Program
  • Explanation

Was this helpful?

  1. Arrays

Program: Prime Number Generator

Problem Statement

In this challenge, your task is to create a C program that will find all prime numbers from 3 to 100. The program should output each prime number separated by a space on a single line. You are required to create an array to store each prime number as it is generated. The first two prime numbers (2 and 3) can be hard-coded into the primes array. Utilize loops to find prime numbers and another loop to print out the primes array.

Hints

  • The criteria to identify a prime number is that it is not evenly divisible by any other previous prime numbers.

  • You can use the following as an exit condition in the innermost loop: p / primes[i] >= primes[i]. This is a test to ensure that the value of p does not exceed the square root of primes[i].

  • Your program can be more efficient by skipping any checks for even numbers (as they cannot be prime).

Feel free to attempt the challenge. When you're ready for guidance or if you have questions, let me know!

Algorithm

  1. Initialize an array primes with the first two prime numbers: 2 and 3.

  2. Initialize a counter count to 2 (representing the number of primes found).

  3. Loop through odd numbers from 5 to 100.

  4. For each odd number num: a. Assume it is prime (isPrime = 1). b. Check divisibility by previous prime numbers up to the square root of num. c. If divisible by any previous prime, set isPrime to 0 and break. d. If isPrime is still 1, add num to the primes array and increment count.

  5. Print the prime numbers stored in the primes array.

Pseudocode

Initialize primes array with [2, 3]
Initialize count to 2

For num = 5 to 100, incrementing by 2:
    isPrime = 1

    For i = 0 to count - 1:
        If num % primes[i] == 0:
            Set isPrime to 0
            Break

    If isPrime == 1:
        Add num to primes array
        Increment count

Print prime numbers stored in the primes array

C Program

#include <stdio.h>

int main() {
    // Declare and initialize the primes array with the first two prime numbers
    int primes[50] = {2, 3};
    int count = 2; // Number of primes found, starting with the first two primes

    // Loop through odd numbers from 5 to 100 to find primes
    for (int num = 5; num <= 100; num += 2) {
        int isPrime = 1; // Assume the current number is prime

        // Check divisibility by previous primes
        for (int i = 0; i < count && primes[i] * primes[i] <= num; i++) {
            if (num % primes[i] == 0) {
                isPrime = 0; // Not prime if divisible by any previous prime
                break;
            }
        }

        // If the current number is prime, add it to the primes array
        if (isPrime) {
            primes[count++] = num;
        }
    }

    // Print the prime numbers separated by a space
    printf("Prime numbers from 3 to 100: ");
    for (int i = 0; i < count; i++) {
        printf("%d ", primes[i]);
    }

    return 0;
}

Explanation

The program initializes an array primes with the first two prime numbers (2 and 3) and a counter count to keep track of the number of primes found. It then loops through odd numbers from 5 to 100, checking each number's divisibility by previous prime numbers. If a number is prime, it is added to the primes array, and the counter is incremented. Finally, the program prints the prime numbers stored in the primes array.

This algorithm efficiently finds prime numbers within the specified range by utilizing the properties of prime numbers and optimizing the checking process.

Feel free to run the program and observe the output. If you have any questions or if there's anything you'd like to discuss, feel free to ask!

PreviousIntroduction to ArraysNextMultidimensional Arrays

Was this helpful?