Day 1 of 100 Days of DSA Challenge: Programming Fundamentals
Welcome to Day 1 of the 100 Days DSA (Data Structures and Algorithms) Challenge! Today, we will kick off with the fundamentals of programming. These basics will form the foundation for more complex concepts and problem-solving skills we will develop over the next 100 days. Here’s what we have on the agenda for today:
1. Checking if a Number is Even or Odd
One of the simplest yet essential tasks in programming is determining whether a number is even or odd. The logic behind this is straightforward—if a number is divisible by 2 with no remainder, it is even; otherwise, it is odd. This foundational exercise will reinforce your understanding of conditional statements and modulo operations.
Placeholder for Code Example:
#include <stdio.h>
int main() {
int num;
printf("enter an integer: ");
scanf("%d",&num);
if (num%2==0){
printf("even number");
}
else{
printf("odd number");
}
return 0;
}
2. Finding the Factorial of a Number Using a Loop
Factorials are a staple in mathematics and programming. Calculating the factorial of a number involves multiplying the number by each preceding integer until you reach one. This exercise will give you practice with loops and basic arithmetic operations.
Placeholder for Code Example:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d",&num);
int fact=1;
for(num;num>0;num--){
fact*=num;
}
printf("Factorial is: %d",fact);
return 0;
}
3. Printing the First n Fibonacci Numbers
The Fibonacci sequence is another fundamental concept that frequently appears in programming challenges. Each number in the sequence is the sum of the two preceding ones. Printing the first n Fibonacci numbers will help you get comfortable with loops and recursive logic.
Placeholder for Code Example:
#include <stdio.h>
int main() {
int num;
int fib1=0,fib2=1,fib;
printf("Enter the number of terms: ");
scanf("%d", &num);
printf("Fibonacci Series:\n");
if (num >= 1) {
printf("%d\n",fib1);
}
if (num >= 2) {
printf("%d\n",fib2);
}
for (int i = 3; i <= num; i++) {
fib = fib1 + fib2;
printf("%d\n", fib);
fib1 = fib2;
fib2 = fib;
}
return 0;
}
4. Checking if a Number is Prime
A prime number is only divisible by 1 and itself. Determining if a number is prime involves checking for divisibility up to its square root. This exercise will enhance your understanding of loops, conditional statements, and optimization techniques.
Placeholder for Code Example:
c
#include <stdio.h>
int main() {
int num, i;
int isPrime = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
5. Reversing a Number
Reversing the digits of a number is a fun and practical exercise. This task will require you to use loops and arithmetic operations to manipulate the digits of the number. It’s a great way to improve your problem-solving skills.
Placeholder for Code Example:
#include <stdio.h>
int main() {
int num, revnum = 0, rem;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
rem = num % 10;
revnum = revnum * 10 + rem;
num /= 10;
}
printf("Reversed number: %d\n", revnum);
return 0;
}
Conclusion
By completing these exercises, you'll reinforce your understanding of core programming concepts. These fundamental skills are crucial as they provide the building blocks for more advanced data structures and algorithms. Keep practicing, and you'll continue to improve and prepare yourself for more challenging problems ahead.
Subscribe to my newsletter
Read articles from Rishi Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by