Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

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

๐ŸŒ™