0% found this document useful (0 votes)
16 views

4

Uploaded by

tg270786
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)
16 views

4

Uploaded by

tg270786
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/ 3

a) Using the looping concept to find whether the given number is Armstrong Number

#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result = 0, n = 0;
// Input number
printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num;

// Count the number of digits


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

originalNum = num;

// Compute sum of each digit raised to the power of n


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

// Check if the number is an Armstrong number


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

return 0;
}

b) Use looping t to Generate the given Patterns


#include <stdio.h>

int main() {
int rows;

// Input number of rows


printf("Enter number of rows: ");
scanf("%d", &rows);

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


// Print spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print stars
for (int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
printf("\n");
}

return 0;
}

c) Using the looping concept to calculate the sum of n numbers


#include <stdio.h>

int main() {
int n, number, sum = 0;

// Input the count of numbers


printf("Enter the number of elements: ");
scanf("%d", &n);

// Input each number and add to sum


for (int i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &number);
sum += number;
}

printf("The sum of %d numbers is: %d\n", n, sum);

return 0;
}

d) Using Looping to list the prime numbers within the specified range
#include <stdio.h>

int main() {
int start, end, i, j, isPrime;

// Input range
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);
printf("Prime numbers between %d and %d are: ", start, end);

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


if (i < 2) continue; // Skip numbers less than 2

// Assume the number is prime


isPrime = 1;

// Check if i is a prime number


for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}

if (isPrime) {
printf("%d ", i);
}
}
printf("\n");

return 0;
}

You might also like