# 1 2 3 4 Series Program in C

The 1 2 3 4 series is a simple programming exercise that prints numbers in a particular pattern. The pattern is straightforward: the first number in the series is 1, and each subsequent number is one more than the previous number. However, there is a twist: every other number in the series is negative. So, the series looks like this: 1, -2, 3, -4, 5, -6, and so on.

To write the program of this series in C, we can use a loop to generate the numbers and an if statement to determine whether each number should be positive or negative. Here is some example code that accomplishes this:

Example:

``````#include <stdio.h>
int main() {
int n = 1;
while (n <= 10) {
if (n % 2 == 0) {
printf("%d\n", -n);
} else {
printf("%d\n", n);
}
n++;
}
return 0;
}
``````

When this program is executed, it will give the following output:

Output:

``````1
-2
3
-4
5
-6
7
-8
9
-10
``````

Program Explanation:

In this code, we start by initializing the variable n to 1. We then use a while loop to generate the numbers in the series. The loop will continue if n is less than or equal to 10 (you can adjust this number to print out more or fewer numbers in the series).

We use an if statement inside the loop to determine whether each number should be positive or negative. We use the modulo operator (%) to check whether n is even or odd. If n is even (i.e., the remainder when n is divided by 2 is 0), we print out -n (i.e., n multiplied by -1). If n is odd, then we print out n as-is.

Finally, we increment n at the end of each loop iteration to generate the next number in the series.

And that's it! This program demonstrates how we can use basic programming constructs like loops and conditionals to generate a specific series of numbers. While the 1 2 3 4 series is a simple example, the same principles can also be applied to more complex programming problems.

Here are a few important things to note about the 1 2 3 4 series program in C:

• The series is generated using a loop: In this program, we use a while loop to generate the numbers in the series. The loop continues as long as the value of the variable n is less than or equal to a certain number (in our example, 10).
• The series alternates between positive and negative numbers: The key feature of the 1 2 3 4 series is that it alternates between positive and negative numbers. In this program, we use an if statement to determine whether each number should be positive or negative based on whether the current value of n is even or odd.
• The modulo operator is used to check for even/odd numbers: We use the modulo operator (%) to check whether the current value of n is even or odd. When n is divided by 2, the remainder will be 0 if n is even and one if n is odd. We can use this information to determine whether to print a positive or negative number.
• The program can be customized to generate different series: While the 1 2 3 4 series is a specific example, we can use similar programming techniques to generate other numbers with different patterns. For example, we could generate a series that increments by two and alternates between positive and negative numbers or a series that generates prime numbers.
• The program can be implemented using different loops: While loops are a common choice for this program because we know how many numbers we want to generate, it could also be implemented using a for loop or a do-while loop.
• The program can be modified to change the starting number and the increment value: The 1 2 3 4 series starts with one and increments by one each time, but we can modify the program to start with a different number and increment by a different value. For example, we could start with 0 and increment by 3 to generate the series 0, 3, -6, 9, -12, etc.

Overall, the 1 2 3 4 series program in C is a simple but useful exercise in basic programming concepts like loops and conditionals. By understanding how this program works, we can begin to build more complex programs and solve more challenging programming problems.

## Working of 1 2 3 4 Series Program in C

The 1 2 3 4 series program in C is a simple program that uses loops and conditional statements to print numbers 1, 2, 3, and 4. Here is how the program works:

• First, the program includes the necessary header files, ‘stdio.h’, for standard input/output operations.
• The program then defines the ‘main()’ function, which is the program's starting point.
• Inside the main() function, the program declares an integer variable ‘i’ to store the current number in the sequence.
• The program uses a for loop to iterate through the sequence. The loop starts at ‘i=1’, and continues as long as ‘i’ is less than or equal to 4, incrementing ‘i’ by 1 after each iteration.
• Inside the loop, the program uses the ‘printf()’ function to print the current value of ‘i’.
• The program then uses an if statement to check if ‘i’ equals 4. If it is, the program prints a newline character \n to start a new line.
• Once the loop has finished executing, the program exits by returning 0 from the main() function.

Here is the code for the 1 2 3 4 series program in C:

Example:

``````#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 4; i++) {
printf("%d ", i);
if (i == 4) {
printf("\n");
}
}
return 0;
}
``````

When this program is executed, it will give the following output:

Output:

`1 2 3 4`

This output is the sequence of numbers 1, 2, 3, 4, separated by spaces and followed by a newline character. The program achieves this output using a for loop to iterate through the sequence and printf() statements to print the numbers and the newline character.

• Simplicity: The 1 2 3 4 series program in C is a simple program that can be easily understood by beginners who are learning how to code in C.
• Fundamentals of loops and conditional statements: This program introduces the fundamentals of loops and conditional statements, which are essential concepts in programming.
• Practice: The program allows beginners to practice coding and gain experience with the syntax and structure of C programs.
• Versatility: Although this program is a simple example, its concepts can be applied to more complex programs.

• Limited scope: While the program provides a basic understanding of loops and conditional statements, it may not prepare beginners for more advanced programming concepts they may encounter.
• Lack of real-world application: The 1 2 3 4 series program is not useful in real-world applications and may not motivate beginners to continue learning and exploring programming.
• Repetitive: The program is repetitive and provides little variety or complexity.
• Potential for boredom: Due to its simplicity and repetitive nature, some beginners may become bored or lose interest in programming after working on this program for an extended period.

While the 1 2 3 4 series program in C has some advantages in simplicity, practice, and introduction to basic programming concepts, it also has limitations in scope, real-world application, and potential for boredom. It is important to use this program as a starting point and continue to explore more advanced programming concepts to build on this foundation.

## Some Other Examples

Here are a few examples of how the 1 2 3 4 series program in C can be modified to produce different outputs:

### Printing the sequence in reverse order

To print the sequence in reverse order, we can modify the for loop to start at 4 and decrement i by 1 after each iteration. Here's the modified code:

Example:

``````#include <stdio.h>
int main() {
int i;
for (i = 4; i >= 1; i--) {
printf("%d ", i);
if (i == 1) {
printf("\n");
}
}
return 0;
}
``````

When this program is executed, it will give the following output:

Output:

`4 3 2 1`

### Printing only even numbers

To print only even numbers in the sequence, we can modify the for loop to increment i by 2 after each iteration and use an if statement to check if ‘i’ is even. Here's the modified code:

Example:

``````#include <stdio.h>
int main() {
int i;
for (i = 2; i <= 4; i += 2) {
if (i % 2 == 0) {
printf("%d ", i);
}
if (i == 4) {
printf("\n");
}
}
return 0;
}``````

When this program is executed, it will give the following output:

Output:

`2 4`

### Printing the sequence multiple times

To print the sequence multiple times, we can use another for loop to iterate through the number of times we want to print the sequence and place the first for loop inside it. Here's the modified code:

Example:

``````#include <stdio.h>
int main() {
int i, j;
for (j = 1; j <= 3; j++) {
for (i = 1; i <= 4; i++) {
printf("%d ", i);
if (i == 4) {
printf("\n");
}
}
}
return 0;
}
``````

When this program is executed, it will give the following output:

Output:

``````1 2 3 4
1 2 3 4
1 2 3 4 ``````

These examples demonstrate how the 1 2 3 4 series program in C can be modified to produce different outputs depending on the program's specific requirements.

## Conclusion

In conclusion, the 1 2 3 4 series program in C prints the sequence of numbers 1, 2, 3, 4 using loops and conditional statements. While the program itself is straightforward, it can be modified to produce a variety of outputs by changing the loop conditions and adding conditional statements. The program can be a starting point for beginners learning to code in C, as it introduces fundamental concepts such as loops, variables, and conditional statements. By building on this program and experimenting with different modifications, learners can better understand programming concepts and improve their problem-solving skills.