Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

Sum of Numbers from 1 to n in C

Sum of Numbers from 1 to n in C

✅ C Program to Calculate Sum from 1 to n

#include <stdio.h>

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

    printf("Enter a positive number: ");
    scanf("%d", &n);

    if (n <= 0) {
        printf("Please enter a positive number.\n");
        return 1;
    }

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

    printf("Sum of numbers from 1 to %d is: %d\n", n, sum);

    return 0;
}
  

๐Ÿ“˜ Explanation:

✅ This program calculates the sum of all natural numbers from 1 to n.
✅ It uses a for loop to iterate from 1 to the given number n.
✅ On each iteration, it adds the value to a running sum variable.
✅ If the input is non-positive, it displays an error message.

๐Ÿงพ Sample Output:

Enter a positive number: 5
Sum of numbers from 1 to 5 is: 15
  

๐Ÿ”‘ Keywords:

Sum from 1 to n in C, C loop program, C addition logic, beginner C project, for loop in C, positive number sum

๐Ÿ“Œ Hashtags:

#CProgramming #ForLoop #BeginnerC #MathInC #SumOfNumbers #InterviewPrep #CodingBasics

Comments

Popular Posts

๐ŸŒ™