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

Assignment 2 module 2 - 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)
9 views

Assignment 2 module 2 - 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/ 10

11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory

Check whether the triangle is equilateral, isosceles, or scalene triangle.

%%file triangle.c
#include <stdio.h>
int main()
{
int side1, side2, side3;

printf("Enter the lengths of three sides of the triangle: ");


scanf("%d %d %d",&side1, &side2, &side3);

if (side1 == side2 && side2 == side3)


{
printf("It's an equilateral triangle. \n");
}
else if (side1 == side2 || side1 == side3 || side2 == side3)
{
printf("It's an isosceles triangle.\n");
}
else {
printf("It's a scalene triangle\n");
}
return 0;
}

Writing triangle.c

!gcc triangle.c
!./a.out

Enter the lengths of three sides of the triangle: 70


70
90
It's an isosceles triangle.

Check whether entered year is a leap year or not

%%file leap.c
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

if (year % 400 == 0) {
printf("%d is a leap year.", year);
}

else if (year % 100 == 0) {


printf("%d is not a leap year.", year);
}

else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}

else {
printf("%d is not a leap year.", year);
}

return 0;
}

Overwriting leap.c

!gcc leap.c
!./a.out

Enter a year: 2004


2004 is a leap year.

Find minimum among three numbers.

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 1/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
%%file avg.c
find minimum among 3 numbers
ask someone

Writing avg.c

!gcc avg.c
!./a.out

Enter three numbers : 6


8 10

Smallest number is: 6

Check whether a number is divisible by 5 and 11 or not.

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

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 5 == 0 && number % 11 == 0) {


printf("%d is divisible by both 5 and 11.\n", number);
} else {
printf("%d is not divisible by both 5 and 11.\n", number);
}

return 0;
}

Writing divi.c

!gcc divi.c
!./a.out

Enter a number: 11
11 is not divisible by both 5 and 11.

Check whether a number is positive, negative or zero using switch case.

%%file switch.c
#include<stdio.h>
int main()
{
int num;

printf("Enter any Number: ");


scanf("%d",&num);

switch(num > 0)
{
case 1 : printf("Number is Positive");
break;

case 0 : if(num < 0)


{
printf("Number is Negative");
}
else
{
printf("Number is Zero");
}
break;
}

return 0;
}

Writing switch.c

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 2/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
!gcc switch.c
!./a.out

Enter any Number: -90


Number is Negative

Design a calculator that performs arithmetic operations on two numbers using switch case

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

int main() {
char operator;
double num1, num2, result;

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


scanf(" %c", &operator);

printf("Enter two numbers: ");


scanf("%lf %lf", &num1, &num2);

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error! Division by zero is not allowed.\n");
return 1; // Exit with an error code
}
break;
default:
printf("Error! Invalid operator.\n");
return 1; // Exit with an error code
}

// Display the result


printf("Result: %.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);

return 0;
}

Overwriting switch.c

!gcc switch.c
!./a.out

Enter an operator (+, -, *, /): *


Enter two numbers: 6
9
Result: 6.00 * 9.00 = 54.00

Find Roots of a Quadratic Equation

%%file roots.c
ask someone

Overwriting roots.c

!gcc roots.c
!./a.out

Find factorial of a number

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

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 3/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d = %lld\n", num, factorial(num));
}

return 0;
}

Writing fact.c

!gcc fact.c
!./a.out

Enter a number: 7
Factorial of 7 = 5040

Check whether number is a palindrome or not

%%file palin.c
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}

Overwriting palin.c

!gcc palin.c
!./a.out

Enter an integer: 787


787 is a palindrome.

check whether the number is perfect sqaure or not

%%file ps.c

#include<stdio.h>
int main()
{
int i, number, flag=0;

printf("Enter a number: ");


scanf("%d", &number);

if(number == 1 || number == 0){


printf("%d is a Perfect Square.", number);

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 4/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
flag=1;
}

for(i = 2; i <= number/2; i++)


{
if(number == i*i)
{
printf("%d is a Perfect Square.", number);
flag=1;
break;
}
}
if(flag == 0)
printf("%d is not a Perfect Square\n", number);

return 0;
}

Overwriting ps.c

!gcc ps.c
!./a.out

Enter a number: 36
36 is a Perfect Square.

Double-click (or enter) to edit

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

int main() {
float x, sum, t, d;
int i, n;

printf("Input the Value of x :");


scanf("%f", &x);

printf("Input the number of terms : ");


scanf("%d", &n); // Read the value of 'n' from the user.

sum = 1;
t = 1;
// Loop to calculate the sum of the series.
for (i = 1; i < n; i++) {
d = (2 * i) * (2 * i - 1);
t = -t * x * x / d;
sum = sum + t;
}

printf("\nThe sum = %f\nNumber of terms = %d\nValue of x = %f\n", sum, n, x);

return 0;

Writing sum.c

!gcc sum.c
!./a.out

Input the Value of x :7


Input the number of terms : 9

The sum = 0.978644


Number of terms = 9
Value of x = 7.000000

printing right half pyramid

%%file py.c
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 5/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

Writing py.c

!gcc py.c
!./a.out

Enter the number of rows: 4


*
* *
* * *
* * * *

printing pyramid of alphabets

%%file py.c
#include <stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';
printf("Enter an uppercase character you want to print in the last row: ");
scanf("%c", &input);
for (i = 1; i <= (input - 'A' + 1); ++i) {
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}

Overwriting py.c

!gcc py.c
!./a.out

output Enter an uppercase character you want to print in the last row: E
A
B B
C C C
D D D D
E E E E E

Floyd's triangle

%%file tri.c
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}

Writing tri.c

!gcc tri.c
!./a.out

Enter the number of rows: 4


1

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 6/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
2 3
4 5 6
7 8 9 10

Calculate the greatest common divisor of two numbers

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

// Function to calculate the GCD using the Euclidean algorithm


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

int main() {
int num1, num2;

// Taking input from the user


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

printf("Enter second number: ");


scanf("%d", &num2);

// Calculating and printing the GCD


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

return 0;
}

Writing hi.c

!gcc hi.c
!./a.out

Enter first number: 6


Enter second number: 9
GCD of 6 and 9 is: 3

Generate first n numbers in the Fibonacci series

%%file fib.c
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

Overwriting fib.c

!gcc fib.c
!./a.out

Enter the number of elements:9

0 1 1 2 3 5 8 13 21

Generate n prime numbers

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 7/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
%%file prime.c

#include<stdio.h>
void main(){
int i, num, n, count;
printf("Enter the range: ");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
}

Overwriting prime.c

!gcc prime.c
!./a.out

Enter the range: 20


The prime numbers in between the range 1 to 20:2 3 5 7 11 13 17 19

Swap two numbers using pointers.

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

// Function to swap two numbers using pointers


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1, num2;

printf("Enter the first number: ");


scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Call the swap function with the addresses of num1 and num2
swap(&num1, &num2);

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

Writing swap.c

!gcc swap.c
!./a.out

Enter the first number: 89


Enter the second number: 98
Before swapping: num1 = 89, num2 = 98
After swapping: num1 = 98, num2 = 89

Performs all the five arithmetic operations using Pointers.

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

// Function to perform addition using pointers


int add(int *a, int *b) {

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 8/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory
return (*a + *b);
}

// Function to perform subtraction using pointers


int subtract(int *a, int *b) {
return (*a - *b);
}

// Function to perform multiplication using pointers


int multiply(int *a, int *b) {
return (*a * *b);
}

// Function to perform division using pointers


float divide(int *a, int *b) {
if (*b != 0) {
return ((float)*a / *b);
} else {
printf("Error: Division by zero!\n");
return 0;
}
}

// Function to perform modulus using pointers


int modulus(int *a, int *b) {
if (*b != 0) {
return (*a % *b);
} else {
printf("Error: Modulus by zero!\n");
return 0;
}
}

int main() {
int num1, num2;

printf("Enter the first number: ");


scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

printf("Addition: %d + %d = %d\n", num1, num2, add(&num1, &num2));


printf("Subtraction: %d - %d = %d\n", num1, num2, subtract(&num1, &num2));
printf("Multiplication: %d * %d = %d\n", num1, num2, multiply(&num1, &num2));

// Check for division by zero before performing division and modulus


if (num2 != 0) {
printf("Division: %d / %d = %.2f\n", num1, num2, divide(&num1, &num2));
printf("Modulus: %d %% %d = %d\n", num1, num2, modulus(&num1, &num2));
} else {
printf("Cannot perform division and modulus. The second number is zero.\n");
}

return 0;
}

Writing poin.c

!gcc poin.c
!./a.out

Enter the first number: 6


Enter the second number: 9
Addition: 6 + 9 = 15
Subtraction: 6 - 9 = -3
Multiplication: 6 * 9 = 54
Division: 6 / 9 = 0.67
Modulus: 6 % 9 = 6

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 9/10
11/14/23, 10:05 PM Assignment 2 module 2 - Colaboratory

https://colab.research.google.com/drive/19YcRp-2xc9NIRVUhrER84m4pJj7QmhSg#scrollTo=5oCkgVKurm3c&printMode=true 10/10

You might also like