0% found this document useful (0 votes)
16 views5 pages

C Programs for Common Tasks

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

C Programs for Common Tasks

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

C Programs Collection

1️⃣ Program to Perform Arithmetic Operation Using Switch Statement

Question: Write a program that takes two operands and one operator from the user, performs the
operation, and prints the result using a switch statement.

#include <stdio.h>

int main() {
double num1, num2;
char op;

printf("Enter first number: ");


scanf("%lf", &num1);

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


scanf(" %c", &op);

printf("Enter second number: ");


scanf("%lf", &num2);

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

return 0;
}

1
2️⃣ Program to Check Whether a Number is Armstrong or Not

Question: Write a program to check whether a given number is an Armstrong number or not.

#include <stdio.h>
#include <math.h>

int main() {
int num, originalNum, remainder, n = 0;
double result = 0.0;

printf("Enter an integer: ");


scanf("%d", &num);

originalNum = num;

while (originalNum != 0) {
originalNum /= 10;
++n;
}

originalNum = num;

while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if ((int)result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

return 0;
}

3️⃣ Program to Combine Digits to Form a Single Number

Question: Combine n digits entered by the user into a single number.

#include <stdio.h>

int main() {

2
int n, digit;
long long number = 0;

printf("Enter number of digits: ");


scanf("%d", &n);

printf("Enter %d space-separated digits: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &digit);
number = number * 10 + digit;
}

printf("The formed number is: %lld\n", number);

return 0;
}

4️⃣ Program to Generate Series: 1, 4, 9, 16, 25, …

Question: Generate and display the series up to n terms.

#include <stdio.h>

int main() {
int n;

printf("Enter number of terms: ");


scanf("%d", &n);

printf("Series: ");
for (int i = 1; i <= n; i++) {
printf("%d", i * i);
if (i < n) printf(", ");
}

printf("\n");
return 0;
}

5️⃣ Program to Find Factorial of a Number

Question: Calculate the factorial of a number entered by the user.

3
#include <stdio.h>

int main() {
int n;
unsigned long long factorial = 1;

printf("Enter a positive integer: ");


scanf("%d", &n);

if (n < 0) {
printf("Error: Factorial of a negative number doesn't exist.\n");
} else {
for (int i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
}

return 0;
}

6️⃣ Program to Find the Sum of Series 1 + 2 + ... + 100

Question: Find the sum of the series 1 + 2 + 3 + ... + 100.

Using Loop:

#include <stdio.h>

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

for (i = 1; i <= 100; i++) {


sum += i;
}

printf("Sum of series 1 + 2 + ... + 100 = %d\n", sum);

return 0;
}

Using Formula:

4
#include <stdio.h>

int main() {
int n = 100;
int sum = n * (n + 1) / 2;

printf("Sum of series 1 + 2 + ... + 100 = %d\n", sum);

return 0;
}

You might also like