0% found this document useful (0 votes)
20 views6 pages

IC Assignment 1

The document contains four programming assignments in C: one for printing prime numbers within a range, one for checking if a number is an Armstrong number, one for finding the GCD and LCM of two numbers, and one for calculating the power of a number using a loop. Each section includes the code, sample outputs, and user prompts. The assignments demonstrate fundamental programming concepts such as loops, functions, and conditionals.

Uploaded by

kotana.abhiram
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)
20 views6 pages

IC Assignment 1

The document contains four programming assignments in C: one for printing prime numbers within a range, one for checking if a number is an Armstrong number, one for finding the GCD and LCM of two numbers, and one for calculating the power of a number using a loop. Each section includes the code, sample outputs, and user prompts. The assignments demonstrate fundamental programming concepts such as loops, functions, and conditionals.

Uploaded by

kotana.abhiram
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
You are on page 1/ 6

Intermediate Coding – 2

Assignment - 1
K. Bhargav Abhiram

(VU22CSEN0101047)

1. Program to print prime numbers in between two numbers.

#include <stdio.h>

int isPrime(int num) {


if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}

int main() {
int start, end;

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:\n", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");

return 0;
}

Output:
(base) abhiramkotana@Abhirams-MacBook-Air-2 Ic 2 % cd
"/Users/abhiramkotana/Documents/Ic 2/" && gcc 1.c -o 1 &&
"/Users/abhiramkotana/Documents/Ic 2/"1

Enter the start of the range: 2

Enter the end of the range: 50

Prime numbers between 2 and 50 are:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

2.Program to find the given number is Armstrong number or not.


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

int main() {
int num, sum = 0, temp, remainder, digits = 0;

printf("Enter a number: ");


scanf("%d", &num);

temp = num;

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

temp = num;

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

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

return 0;
}

Output:
(base) abhiramkotana@Abhirams-MacBook-Air-2 Ic 2 % cd
"/Users/abhiramkotana/Documents/Ic 2/" && gcc 2.c -o 2 &&
"/Users/abhiramkotana/Documents/Ic 2/"2
Enter a number: 153
153 is an Armstrong number.

3.Program to find the GCD and LCM of two numbers.


Code:
#include <stdio.h>

int findGCD(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int findLCM(int a, int b, int gcd) {


return (a * b) / gcd;
}

int main() {
int num1, num2;

printf("Enter the first number: ");


if (scanf("%d", &num1) != 1) {
printf("Invalid input. Please enter an integer.\n");
return 1;
}

printf("Enter the second number: ");


if (scanf("%d", &num2) != 1) {
printf("Invalid input. Please enter an integer.\n");
return 1;
}

if (num1 <= 0 || num2 <= 0) {


printf("Please enter positive integers only.\n");
return 1;
}

int gcd = findGCD(num1, num2);


int lcm = findLCM(num1, num2, gcd);

printf("GCD of %d and %d is: %d\n", num1, num2, gcd);


printf("LCM of %d and %d is: %d\n", num1, num2, lcm);
return 0;
}

Code:
Enter the first number: 3
Enter the second number: 39
GCD of 3 and 39 is: 3
LCM of 3 and 39 is: 39
4.Program to calculate the power of a number using a loop.
Code:
#include <stdio.h>

int main() {
int base, exponent, result = 1;

printf("Enter base: ");


scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);

if (exponent == 0) {
if (base == 0) {
printf("0 raised to the power of 0 is undefined.\n");
} else {
printf("%d raised to the power of %d is: 1\n", base, exponent);
}
} else {
for (int i = 1; i <= exponent; i++) {
result *= base;
}
printf("%d raised to the power of %d is: %d\n", base, exponent, result);
}

return 0;
}

Output:
(base) abhiramkotana@Abhirams-MacBook-Air-2 Ic 2 % cd "/Users/abhiramkotana/Documents/Ic 2/"
&& gcc 4.c -o 4 && "/Users/abhiramkotana/Documents/Ic 2/"4
Enter base: 4
Enter exponent: 4
4 raised to the power of 4 is: 256

You might also like