[go: up one dir, main page]

0% found this document useful (0 votes)
24 views27 pages

Document 6

The document contains 10 C programs that demonstrate various programming concepts like functions, recursion, loops, arrays, and more. Each program is accompanied by its source code, input, and output. The programs cover topics such as finding perfect numbers, calculating factorials, converting between number systems, and more. The document appears to be an assignment submitted by a student containing well-commented code samples.
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)
24 views27 pages

Document 6

The document contains 10 C programs that demonstrate various programming concepts like functions, recursion, loops, arrays, and more. Each program is accompanied by its source code, input, and output. The programs cover topics such as finding perfect numbers, calculating factorials, converting between number systems, and more. The document appears to be an assignment submitted by a student containing well-commented code samples.
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/ 27

Motilal Nehru National

Institute of Technology,
Allahabad

Computer Science and Engineering


Department

**Programming and Problem Solving


Lab**

Week 6 Assignment

Submitted by: Submitted to:


Shushant Sonwani Mr. Prateek pandey
Reg.no.-2023CA95 Subject code-CS31202
Group -B
Branch :MCA
Program 1:
#include <stdio.h>
// Function to find the sum of divisors of a number
int sumdiv(int num) {
int sum = 1; // Start with 1 as every number is divisible by 1
// Check for divisors up to the square root of the number
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i; // Add the divisor
// If the divisor is not equal to the other factor, add it as well
if (i != num / i) {
sum += num / i;
}
}
}
return sum;
}
// Function to print perfect numbers in a given range
void printPerfectNumbers(int start, int end) {
printf("Perfect numbers in the range %d to %d are:\n", start, end);
for (int i = start; i <= end; i++) {
// Check if the number is perfect
if (sumdiv(i) == i) {
printf("%d\n", i);
}
}
}
// Main function
int main() {
int start, end;
// Get the range from the user
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);
// Call the function to print perfect numbers in the given range
printPerfectNumbers(start, end);
return 0;
}

Output:
Program 2:
#include <stdio.h>

// Recursive function to find the sum of digits of a number


int sumOfDigits(int num) {
// Base case: if the number is 0, return 0
if (num == 0) {
return 0;
} else {
// Recursive case: add the last digit and call the function with the remaining digits
return (num % 10) + sumOfDigits(num / 10);
}
}

// Main function
int main() {
int number;

// Get the number from the user


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

// Call the function to find the sum of digits and print the result
printf("Sum of digits of %d is: %d\n", number, sumOfDigits(number));

return 0;
}
Output:
Program 3:
#include <stdio.h>

int main() {
int month;

// Get the month number from the user


printf("Enter the month number (1-12): ");
scanf("%d", &month);

// Print the corresponding month name using a switch statement


switch (month) {
case 1:
printf("The corresponding month is: January\n");
break;
case 2:
printf("The corresponding month is: February\n");
break;
case 3:
printf("The corresponding month is: March\n");
break;
case 4:
printf("The corresponding month is: April\n");
break;
case 5:
printf("The corresponding month is: May\n");
break;
case 6:
printf("The corresponding month is: June\n");
break;
case 7:
printf("The corresponding month is: July\n");
break;
case 8:
printf("The corresponding month is: August\n");
break;
case 9:
printf("The corresponding month is: September\n");
break;
case 10:
printf("The corresponding month is: October\n");
break;
case 11:
printf("The corresponding month is: November\n");
break;
case 12:
printf("The corresponding month is: December\n");
break;
default:
printf("Invalid Month\n");
}

return 0;
}
Output:
Program 4:
A)Using function:
#include <stdio.h>

// Function to calculate the factorial of a number


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

// Function to evaluate the series using a loop


double evaluateSeries(double x, int terms) {
double result = 0;

for (int i = 0; i < terms; i++) {


int sign = (i % 2 == 0) ? 1 : -1; // Alternating sign
int power = 2 * i + 1; // Exponent is 2n+1 for each term
result += sign * (pow(x, power) / factorial(power));
}

return result;
}

int main() {
double x;
int terms;

// Get input from the user


printf("Enter the value of x: ");
scanf("%lf", &x);

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


scanf("%d", &terms);

// Evaluate the series and print the result


double seriesResult = evaluateSeries(x, terms);
printf("Result of the series: %lf\n", seriesResult);

return 0;
}

Output:
B)Using recursive Function:
#include <stdio.h>

// Function to calculate the factorial of a number recursively

int factorial(int n) {

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

return 1;

} else {

return n * factorial(n - 1);

// Recursive function to evaluate the series

double evaluateSeriesRecursive(double x, int terms, int currentTerm, int sign) {

if (currentTerm == terms) {

return 0;

} else {

int denominator = 2 * currentTerm + 1;

return sign * (pow(x, denominator) / factorial(denominator))

+ evaluateSeriesRecursive(x, terms, currentTerm + 1, -sign);

// Main function

int main() {

double x;

int terms;

// Get user input

printf("Enter the value of x: ");

scanf("%lf", &x);
printf("Enter the number of terms: ");

scanf("%d", &terms);

// Evaluate and print the result using a recursive function

double resultRecursive = evaluateSeriesRecursive(x, terms, 0, 1);

printf("Result using recursive function: %lf\n", resultRecursive);

return 0;

Output:
Program 5:
#include <stdio.h>

// Function to circularly shift three variables to the right


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

int main() {
int x, y, z;

// Get user input for the variables


printf("Enter the value of x: ");
scanf("%d", &x);

printf("Enter the value of y: ");


scanf("%d", &y);

printf("Enter the value of z: ");


scanf("%d", &z);

// Print the initial values


printf("Before circular shift: x = %d, y = %d, z = %d\n", x, y, z);

// Call the function to perform circular shift


circularShiftRight(&x, &y, &z);
// Print the values after circular shift
printf("After circular shift: x = %d, y = %d, z = %d\n", x, y, z);

return 0;
}

Output:
Program 6:
#include <stdio.h>

// Function to calculate the factorial of a number


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

int main() {
int num;

// Get user input for the integer


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

// Check if the input is non-negative


if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Call the function to calculate factorial and print the result
long long result = factorial(num);
printf("The factorial of %d is: %lld\n", num, result);
}

return 0;
}

Output:
Program 7:
#include <stdio.h>
#include <math.h>

int main() {
double x, y;
int n;

// Get user input for X and n


printf("Enter the value of X: ");
scanf("%lf", &x);

printf("Enter the non-negative integer value of n: ");


scanf("%d", &n);

// Check if n is non-negative
if (n < 0) {
printf("Invalid input. n should be a non-negative integer.\n");
} else {
// Calculate X^n using pow function
y = pow(x, n);

// Print the result


printf("Y = %.2lf\n", y);
}

return 0;
}
Output:
Program 8:
#include <stdio.h>

// Function to obtain prime factors of a positive integer


void primeFactors(int num) {
printf("Prime factors of %d are: ", num);

// Print the number of 2s that divide num


while (num % 2 == 0) {
printf("2 ");
num /= 2;
}

// num must be odd at this point, so a skip of 2 ( i = i + 2) can be used


for (int i = 3; i <= num; i += 2) {
// While i divides num, print i and divide num
while (num % i == 0) {
printf("%d ", i);
num /= i;
}
}

printf("\n");
}

int main() {
int num;

// Get user input for a positive integer


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

// Check if the entered number is positive


if (num <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
} else {
// Call the function to obtain prime factors
primeFactors(num);
}

return 0;
}

Output:
Program 9:
#include <stdio.h>

// Recursive function to generate the nth Fibonacci number


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

// Function to print the first 25 numbers of the Fibonacci sequence


void printFibonacciSequence(int count) {
printf("Fibonacci Sequence (first %d numbers):\n", count);

for (int i = 0; i < count; i++) {


printf("%d ", fibonacci(i));
}

printf("\n");
}

int main() {
// Print the first 25 numbers of the Fibonacci sequence
printFibonacciSequence(25);

return 0;
}
Output:
Program 10:
#include <stdio.h>

// Recursive function to convert a decimal number to binary


void decimalToBinary(int n) {
if (n > 0) {
decimalToBinary(n / 2);
printf("%d", n % 2);
}
}

int main() {
int decimalNumber;

// Get user input for a positive integer


printf("Enter a positive integer: ");
scanf("%d", &decimalNumber);

// Check if the entered number is positive


if (decimalNumber <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
} else {
// Print the binary equivalent using the recursive function
printf("Binary equivalent: ");
decimalToBinary(decimalNumber);
printf("\n");
}

return 0;
}

Output:
Program 11:
#include <stdio.h>
#include <math.h>

// Function to compute the distance between two points


double distance(double x1, double y1, double x2, double y2) {
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

// Function to compute the area of a triangle given its vertices


double calculateTriangleArea(double x1, double y1, double x2, double y2, double x3, double
y3) {
return fabs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
}

// Function to check if a point (x, y) lies inside the triangle ABC


int pointInsideTriangle(double x, double y, double x1, double y1, double x2, double y2,
double x3, double y3) {
double areaABC, areaPAB, areaPBC, areaPAC, distanceAB, distanceBC, distanceCA;

// Calculate distances between points A, B, and C


distanceAB = distance(x1, y1, x2, y2);
distanceBC = distance(x2, y2, x3, y3);
distanceCA = distance(x3, y3, x1, y1);

// Calculate the area of the triangle ABC


areaABC = calculateTriangleArea(x1, y1, x2, y2, x3, y3);

// Calculate the areas of the triangles PAB, PBC, and PAC


areaPAB = calculateTriangleArea(x, y, x2, y2, x3, y3);
areaPBC = calculateTriangleArea(x1, y1, x, y, x3, y3);
areaPAC = calculateTriangleArea(x1, y1, x2, y2, x, y);

// Check if the sum of areas of PAB, PBC, and PAC is approximately equal to ABC
if (fabs(areaABC - (areaPAB + areaPBC + areaPAC)) < 1e-6) {
printf("The point P(%lf, %lf) lies inside the triangle ABC.\n", x, y);
printf("Distance between A and B: %lf\n", distanceAB);
printf("Distance between B and C: %lf\n", distanceBC);
printf("Distance between C and A: %lf\n", distanceCA);
printf("Area of triangle ABC: %lf\n", areaABC);
return 1;
} else {
printf("The point P(%lf, %lf) does not lie inside the triangle ABC.\n", x, y);
return 0;
}
}

int main() {
double x1, y1, x2, y2, x3, y3, x, y;

// Get user input for the vertices of the triangle (x1, y1), (x2, y2), (x3, y3)
printf("Enter the coordinates of vertex A (x1 y1): ");
scanf("%lf %lf", &x1, &y1);

printf("Enter the coordinates of vertex B (x2 y2): ");


scanf("%lf %lf", &x2, &y2);

printf("Enter the coordinates of vertex C (x3 y3): ");


scanf("%lf %lf", &x3, &y3);

// Get user input for the point (x, y)


printf("Enter the coordinates of the point P (x y): ");
scanf("%lf %lf", &x, &y);

// Check if the point lies inside the triangle and print the result
pointInsideTriangle(x, y, x1, y1, x2, y2, x3, y3);

return 0;

Output:

You might also like