Day8 Assignment
Day8 Assignment
#include <stdio.h>
int increment(int );
int main() {
int num = 5;
printf("Original value of num: %d\n", num);
int result = increment(num);
printf("Result of function increment: %d\n", result);
printf("Value of num after function call: %d\n", num);
return 0;
}
2.
//2. Write a C program that attempts to swap two integers using a function that
employs call by value. Show that the original values remain unchanged after the
function call.
#include <stdio.h>
void swap(int, int);
int main() {
int a = 10, b= 20;
printf("Before swap inside main function: a = %d, b = %d\n", a, b);
swap(a, b);
printf("After swap inside main function: a = %d, b = %d\n", a, b);
}
3.
//3. Develop a C program that calculates the factorial of a number using call by
value.
#include <stdio.h>
int fact(int);
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int res = fact(num);
printf("Factorial of %d is %d", num, res);
return 0;
}
4.
//4. Create a C program that defines a function to find the maximum of two
numbers using call by value.
#include<stdio.h>
void max(int, int);
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
max(num1, num2);
return 0;
}
5.
/*Problem Statement: Arithmetic Operations Calculator
Requirements:
Create separate functions for addition, subtraction, multiplication, and
division.
Each function should take two parameters (the numbers) and return the result.
Use appropriate data types for the variables.
Use operators for arithmetic calculations.
Example Input/Output:
Enter first number: 10
Enter second number: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0*/
#include <stdio.h>
int add(int, int);
int subtract(int, int);
int multiply(int, int);
float divide(int, int);
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
6.
/*Problem Statement: Temperature Conversion
Requirements:
Create two functions: one for converting Celsius to Fahrenheit and another for
converting Fahrenheit to Celsius.
Each function should accept a temperature value as an argument and return the
converted temperature.
Use appropriate data types for temperature values.
Use arithmetic operators to perform the conversion calculations.
Example Input/Output:
#include <stdio.h>
float c_to_f(float);
float f_to_c(float);
int main() {
float c, f;
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
float res1 = c_to_f(c);
printf("Temperature in Fahrenheit: %.1f\n", res1);
printf("\n");
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
float res2 = f_to_c(f);
printf("Temperature in Celsius: %.1f\n", res2);
return 0;
}
float c_to_f(float c) {
return (c * 9/5) + 32;
}
float f_to_c(float f) {
return (f - 32) * 5/9;
}
7.
/*Problem Statement: Simple Interest Calculator
Requirements:
Implement a function that takes three parameters (principal, rate, time) and
returns the calculated simple interest.
Use appropriate data types for financial calculations (e.g., float or double).
Utilize arithmetic operators to compute simple interest using the formula
SI = P×R×T/100
Example Input/Output:
#include <stdio.h>
float simple_interest(float, float, int);
int main() {
float P, R;
int T;
printf("Enter principal amount: ");
scanf("%f", &P);
printf("Enter rate of interest: ");
scanf("%f", &R);
printf("Enter time period (in years): ");
scanf("%d", &T);
float SI = simple_interest(P, R, T);
printf("Simple Interest is: %.1f\n", SI);
return 0;
}
8.
#include <stdio.h>
int main() {
char A = 100;
printf("Address of A is %p\n", &A);
char *pA = &A;
printf("Value of A is %c\n", A);
char B = *pA;
printf("Value of B is %c\n", B);
*pA = 65;
printf("New value of A is %c\n", A);
}
9.
//Write a C program that swaps the values of two integers using pointers.
#include <stdio.h>
int main()
{
int A = 10, B = 11;
int *ptrA = &A, *ptrB = &B;
printf("A = %d, B = %d\n", A, B);
10.
//Write a C program that swaps the values of two integers using swap function and
follow pass by reference method.
#include <stdio.h>
void swap(int *, int *);
int main() {
int a = 10, b= 20;
printf("Before swap inside main function: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap inside main function: a = %d, b = %d\n", a, b);
}
11.
//WAP for Finding the Cube of a Number Using Pass by Reference
#include <stdio.h>
#include <stdio.h>
float simple_interest(float *, float *, int *);
int main() {
float P, R;
int T;
printf("Enter principal amount: ");
scanf("%f", &P);
printf("Enter rate of interest: ");
scanf("%f", &R);
printf("Enter time period (in years): ");
scanf("%d", &T);
float SI = simple_interest(&P, &R, &T);
printf("Simple Interest is: %.1f\n", SI);
return 0;
}