[go: up one dir, main page]

0% found this document useful (0 votes)
88 views7 pages

Lecture-IF and For Statement in C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views7 pages

Lecture-IF and For Statement in C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Here are 10 C language programs that involve the use of if and for loop statements.

These
examples cover various scenarios to provide variety and enhance learning.

1. Check Odd or Even Numbers in a Range


#include <stdio.h>
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);

for (int i = start; i <= end; i++) {


if (i % 2 == 0) {
printf("%d is Even\n", i);
} else {
printf("%d is Odd\n", i);
}
}
return 0;
}

2. Print Prime Numbers in a Range


#include <stdio.h>
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);

for (int i = start; i <= end; i++) {


int isPrime = 1;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime && i > 1) {
printf("%d is Prime\n", i);
}
}
return 0;
}

3. Calculate Factorial of a Number


#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);

for (int i = 1; i <= num; i++) {


factorial *= i;
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}

4. Check Palindrome Number


#include <stdio.h>
int main() {
int num, reversed = 0, original, remainder;
printf("Enter a number: ");
scanf("%d", &num);

original = num;
for (; num != 0; num /= 10) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
}
if (original == reversed) {
printf("The number is a palindrome\n");
} else {
printf("The number is not a palindrome\n");
}
return 0;
}

5. Find Sum of Odd and Even Numbers in a Range


#include <stdio.h>
int main() {
int start, end, oddSum = 0, evenSum = 0;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);

for (int i = start; i <= end; i++) {


if (i % 2 == 0) {
evenSum += i;
} else {
oddSum += i;
}
}
printf("Sum of even numbers: %d\n", evenSum);
printf("Sum of odd numbers: %d\n", oddSum);
return 0;
}
6. Check Armstrong Numbers in a Range
#include <stdio.h>
#include <math.h>
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);

for (int num = start; num <= end; num++) {


int sum = 0, temp = num, digits = 0;

while (temp != 0) {
digits++;
temp /= 10;
}

temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += pow(digit, digits);
temp /= 10;
}

if (sum == num) {
printf("%d is an Armstrong number\n", num);
}
}
return 0;
}

7. Find Maximum Number in an Array


#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int arr[n], max;


printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("Maximum element is: %d\n", max);
return 0;
}
8. Generate Fibonacci Series
#include <stdio.h>
int main() {
int n, a = 0, b = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);

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


printf("%d ", a);
next = a + b;
a = b;
b = next;
}
return 0;
}

9. Print Multiplication Table


#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

for (int i = 1; i <= 10; i++) {


printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

10. Find Numbers Divisible by Both 3 and 5 in a Range


#include <stdio.h>
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);

for (int i = start; i <= end; i++) {


if (i % 3 == 0 && i % 5 == 0) {
printf("%d is divisible by 3 and 5\n", i);
}
}
return 0;
}
These programs demonstrate how to use if and for loops in different scenarios. Let me know if
you need further explanations!

Here are three C programs that demonstrate the use of the switch case statement in different
contexts.

1. Simple Calculator

This program performs basic arithmetic operations based on the user's choice.

#include <stdio.h>
int main() {
char operator;
float num1, num2, result;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);

switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operator.\n");
}

return 0;
}

2. Days of the Week


This program prints the name of the day based on the day number entered by the user.

#include <stdio.h>
int main() {
int day;

printf("Enter a number (1-7) for the day of the week: ");


scanf("%d", &day);

switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day number. Please enter a number between 1 and
7.\n");
}

return 0;
}

3. Grade System

This program determines the grade of a student based on their marks.

#include <stdio.h>
int main() {
int marks;

printf("Enter your marks (0-100): ");


scanf("%d", &marks);

switch (marks / 10) {


case 10:
case 9:
printf("Grade: A\n");
break;
case 8:
printf("Grade: B\n");
break;
case 7:
printf("Grade: C\n");
break;
case 6:
printf("Grade: D\n");
break;
default:
if (marks >= 0 && marks < 60) {
printf("Grade: F\n");
} else {
printf("Invalid marks entered.\n");
}
}

return 0;
}

You might also like