Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

C Program: Prime Number Check

C Program: Prime Number Check

πŸ”· C Program: Prime Number Check

#include<stdio.h>
int main( )
{
    int num, count = 0;
    printf("Enter the number:\n");
    scanf("%d", &num);

    if(num <= 1)
    {
        printf("%d is not a prime number:\n", num);
    }
    else
    {
        for(int i = 2; i <= num / 2; i++)
        {
            if(num % i == 0)
            {
                count++;
                break;
            }
        }

        if(count == 0)
        {
            printf("%d is a prime number:\n", num);
        }
        else
        {
            printf("%d is not a prime number:\n", num);
        }
    }
}
  

πŸ“˜ Explanation:

This C program checks if a number is prime or not.

πŸ‘‰ A **prime number** is a number greater than 1 that is divisible only by 1 and itself.

πŸ”Έ First, the user enters a number.
πŸ”Έ If the number is less than or equal to 1, it's not prime.
πŸ”Έ If the number is greater than 1, the program checks if it has any divisors (from 2 to num/2).
πŸ”Έ If any divisor is found, `count` is incremented, and the loop breaks early for efficiency.
πŸ”Έ Finally, if no divisors are found (`count == 0`), it's a prime number.

πŸ” Sample Output:

Enter the number:
7
7 is a prime number:

Enter the number:
10
10 is not a prime number:

Enter the number:
1
1 is not a prime number:
    

🏷️ Keywords:

C program to check prime number, prime number logic in C, beginner C programs, isPrime function, modulus operator in C, number theory in C

Comments

Popular Posts

πŸŒ™