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

Assignment 3 - Colaboratory

Uploaded by

sanatabasum2005
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)
4 views

Assignment 3 - Colaboratory

Uploaded by

sanatabasum2005
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/ 9

11/18/23, 7:52 PM Assignment 3 - Colaboratory

1) Print even and odd numbers in a given range

%%file assi3.c

#include <stdio.h>

// Function to print even numbers in a given range


void printEvenNumbers(int start, int end) {
printf("Even numbers in the range %d to %d are: ", start, end);
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
printf("\n");
}

// Function to print odd numbers in a given range


void printOddNumbers(int start, int end) {
printf("Odd numbers in the range %d to %d are: ", start, end);
for (int i = start; i <= end; i++) {
if (i % 2 != 0) {
printf("%d ", i);
}
}
printf("\n");
}

int main() {
int startRange, endRange;

// Input the range from the user


printf("Enter the start of the range: ");
scanf("%d", &startRange);

printf("Enter the end of the range: ");


scanf("%d", &endRange);

// Call the functions to print even and odd numbers


printEvenNumbers(startRange, endRange);
printOddNumbers(startRange, endRange);

return 0;
}

output Writing assi3.c

!gcc assi3.c
!./a.out

Enter the start of the range: 6


Enter the end of the range: 20
Even numbers in the range 6 to 20 are: 6 8 10 12 14 16 18 20
Odd numbers in the range 6 to 20 are: 7 9 11 13 15 17 19

2.Find power of a number

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 1/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory
%%file assi3.c

#include <stdio.h>

// Function to calculate the power of a number


double power(double base, int exponent) {
double result = 1.0;

// Calculate power using a loop


for (int i = 0; i < exponent; ++i) {
result *= base;
}

return result;
}

int main() {
double base;
int exponent;

// Input the base and exponent


printf("Enter the base: ");
scanf("%lf", &base);

printf("Enter the exponent: ");


scanf("%d", &exponent);

// Call the function to calculate the power and display the result
double result = power(base, exponent);
printf("%.2lf raised to the power of %d is: %.2lf\n", base, exponent, result);

return 0;
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

Enter the base: 9


Enter the exponent: 2
9.00 raised to the power of 2 is: 81.00

3.Return maximum of given two numbers

%%file assi3.c

#include <stdio.h>

// Function to find the maximum of two numbers


int findMax(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}

int main() {
int num1, num2;

// Input two numbers


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

// Call the function to find the maximum and display the result
int max = findMax(num1, num2);
printf("The maximum of %d and %d is: %d\n", num1, num2, max);

return 0;
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 2/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory

Enter the first number: 34


Enter the second number: 22
The maximum of 34 and 22 is: 34

4.To print all strong numbers between given interval using functions.

%%file assi3.c

#include <stdio.h>

// Function to calculate the factorial of a number


int factorial(int num) {
if (num == 0 || num == 1) {
return 1;
} else {
return num * factorial(num - 1);
}
}

// Function to check if a number is a strong number


int isStrong(int num) {
int originalNum = num;
int sum = 0;

while (num > 0) {


int digit = num % 10;
sum += factorial(digit);
num /= 10;
}

return (sum == originalNum);


}

// Function to print strong numbers in a given interval


void printStrongNumbers(int start, int end) {
printf("Strong numbers in the interval [%d, %d]:\n", start, end);

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


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

int main() {
int start, end;

// Input the interval


printf("Enter the starting number of the interval: ");
scanf("%d", &start);

printf("Enter the ending number of the interval: ");


scanf("%d", &end);

// Print strong numbers in the given interval


printStrongNumbers(start, end);

return 0;
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

Enter the starting number of the interval: 3


Enter the ending number of the interval: 10
Strong numbers in the interval [3, 10]:

5.Check whether a number is prime, Armstrong or perfect number using functions.

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 3/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory
%%file assi3.c

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

int checkPrime(int num);


int checkArmstrong(int num);

int main(){
int num;

// Asking for Input


printf("Enter an integer: ");
scanf("%d", &num);

// Check Prime Number


if (checkPrime(num) == 0){
printf("%d is a Prime Number.\n", num);
}
else{
printf("%d is not a Prime Number.\n", num);
}

// Check Armstrong Number


if (checkArmstrong(num) == 0){
printf("%d is an Armstrong Number.\n", num);
}
else{
printf("%d is not an Armstrong Number.\n", num);
}
return 0;
}

// Function To Check Prime Number


int checkPrime(int num){
int i, count = 0;
for (i = 2; i <= num/2; i++){
if (num % i == 0){
count = 1;
break;
}
}
if (num == 1)
count = 1;
return count;
}

// Function To Check Armstrong Number


int checkArmstrong(int num){
int lastdigit = 0, power = 0, sum = 0;

int n = num;
while (n != 0){
lastdigit = n % 10;
power = lastdigit * lastdigit * lastdigit;
sum = sum + power;
n = n / 10;
}
if (sum == num)
return 0;
else
return 1;
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

Enter an integer: 153


153 is not a Prime Number.
153 is an Armstrong Number.

6.Demonstrate call by value and call by reference mechanisms.

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 4/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory
%%file assi3.c

#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

Before swapping the values in main a = 10, b = 20


After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

6. call by reference mechanisms.

%%file assi3.c

#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters do change in call by reference, a =
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20, b = 10
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

Before swapping the values in main a = 10, b = 20


After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

7. Find power of any number using recursion.

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 5/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory
%%file assi3.c

#include <stdio.h>

// Function to calculate power using recursion


double power(double base, int exponent) {
// Base case: Any number raised to the power of 0 is 1
if (exponent == 0) {
return 1.0;
}

// Recursive case: base^exponent = base * base^(exponent-1)


return base * power(base, exponent - 1);
}

int main() {
double base;
int exponent;

// Input the base and exponent


printf("Enter the base: ");
scanf("%lf", &base);

printf("Enter the exponent: ");


scanf("%d", &exponent);

// Call the function to calculate the power and display the result
double result = power(base, exponent);
printf("%.2lf raised to the power of %d is: %.2lf\n", base, exponent, result);

return 0;
}

!gcc assi3.c
!./a.out

Enter the base: 5


Enter the exponent: 2
5.00 raised to the power of 2 is: 25.00

8. Generate Fibonacci series using recursion

%%file assi3.c
#include <stdio.h>

// Function to generate Fibonacci series using recursion


int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}

int main() {
int terms;

// Input the number of terms in the Fibonacci series


printf("Enter the number of terms in the Fibonacci series: ");
scanf("%d", &terms);

// Display the Fibonacci series using recursion


printf("Fibonacci Series:\n");
for (int i = 0; i < terms; ++i) {
printf("%d ", fibonacci(i));
}

return 0;
}

Overwriting assi3.c

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 6/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory

!gcc assi3.c
!./a.out

Enter the number of terms in the Fibonacci series: 9


Fibonacci Series:
0 1 1 2 3 5 8 13 21

9.Find product of two numbers using recursion

%%file assi3.c
#include <stdio.h>

// Function to calculate the product of two numbers using recursion


int multiply(int a, int b) {
if (b == 0) {
return 0;
} else {
return a + multiply(a, b - 1);
}
}

int main() {
int num1, num2;

// Input two numbers


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

// Calculate the product using the function and display the result
int result = multiply(num1, num2);
printf("The product of %d and %d is: %d\n", num1, num2, result);

return 0;
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

Enter the first number: 5


Enter the second number: 8
The product of 5 and 8 is: 40

10.Find the sum of digits of a number. Number must be passed to a function using pointers.

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 7/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory
%%file assi3.c

#include <stdio.h>

// Function to calculate the sum of digits using pointers


int sumOfDigits(int *num) {
int sum = 0;

// Extract digits and add them to the sum


while (*num != 0) {
sum += *num % 10; // Add the last digit
*num /= 10; // Remove the last digit
}

return sum;
}

int main() {
int number;

// Input a number
printf("Enter a number: ");
scanf("%d", &number);

// Calculate the sum of digits using the function


int result = sumOfDigits(&number);

// Display the result


printf("Sum of digits: %d\n", result);

return 0;
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

Enter a number: 34
Sum of digits: 7

11.Find GCD (HCF) of two numbers using recursion.

%%file assi3.c

#include <stdio.h>

// Function to calculate the GCD (HCF) using recursion


int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}

int main() {
int num1, num2;

// Input two numbers


printf("Enter first number: ");
scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

// Calculate and display the GCD (HCF)


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

return 0;
}

Overwriting assi3.c

!gcc assi3.c
!./a.out

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 8/9
11/18/23, 7:52 PM Assignment 3 - Colaboratory

Enter first number: 34

https://colab.research.google.com/drive/1G9h-G2cgCBlr3xhue6zyYdrOPLZExv8o#scrollTo=-pD8c-EZzGNv&printMode=true 9/9

You might also like