0% found this document useful (0 votes)
11 views10 pages

Day8 Assignment

The document contains multiple C programs demonstrating various programming concepts, including functions for incrementing integers, swapping values, calculating factorials, performing arithmetic operations, converting temperatures, and calculating simple interest. Each program illustrates the use of call by value or call by reference, showcasing how original values remain unchanged or are modified. Additionally, it includes examples of pointer usage and memory address manipulation.

Uploaded by

bettinakpeter
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)
11 views10 pages

Day8 Assignment

The document contains multiple C programs demonstrating various programming concepts, including functions for incrementing integers, swapping values, calculating factorials, performing arithmetic operations, converting temperatures, and calculating simple interest. Each program illustrates the use of call by value or call by reference, showcasing how original values remain unchanged or are modified. Additionally, it includes examples of pointer usage and memory address manipulation.

Uploaded by

bettinakpeter
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

1.

/*1. Create a C program that defines a function to increment an integer by 1.


The function should demonstrate call by value, showing that the original value
remains unchanged. */

#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;
}

int increment(int num) {


num++;
return num;
}

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);
}

void swap(int a, int b) {


printf("Before swap inside swap function: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swap inside swap 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;
}

int fact(int num) {


if (num == 0 || num == 1)
return 1;
else
return num * fact(num-1);
}

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;
}

void max(int num1, int num2) {


int result;
if (num1 > num2) {
result = num1;
} else {
result = num2;
}
printf("The maximum number is: %d\n", result);
}

5.
/*Problem Statement: Arithmetic Operations Calculator

Description: Write a C program that performs basic arithmetic operations


(addition, subtraction, multiplication, and division) on two numbers provided by
the user.
The program should use functions to perform each operation and demonstrate call
by value.

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);

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


printf("Subtraction: %d\n", subtract(num1, num2));
printf("Multiplication: %d\n", multiply(num1, num2));
printf("Division: %.1f\n", divide(num1, num2));
}

int add(int num1, int num2) {


return num1 + num2;
}

int subtract(int num1, int num2) {


return num1 - num2;
}

int multiply(int num1, int num2) {


return num1 * num2;
}

float divide(int num1, int num2) {


if (0 == num2)
{
printf("Error: Division by zero!\n");
return 0.0;
}
else
{
return (float)num1 / num2;
}
}

6.
/*Problem Statement: Temperature Conversion

Description: Develop a C program that converts temperatures between Celsius and


Fahrenheit.
The program should use functions to handle the conversions and demonstrate call
by value.

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:

Enter temperature in Celsius: 25


Temperature in Fahrenheit: 77.0

Enter temperature in Fahrenheit: 77


Temperature in Celsius: 25.0*/

#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

Description: Develop a C program that calculates simple interest based on user


input for principal amount,
rate of interest, and time period. The program should use a function to compute
interest and demonstrate call by value.

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:

Enter principal amount: 1000


Enter rate of interest: 5
Enter time period (in years): 3
Simple Interest is: 150.0*/

#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;
}

float simple_interest(float P, float R, int T) {


float SI = (P * R * T) / 100;
return SI;
}

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);

int temp = *ptrA;


*ptrA = *ptrB;
*ptrB = temp;

printf("After swapping, A = %d, B = %d\n", A, B);


return 0;
}

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);
}

void swap(int *p, int *q) {


printf("Before swap inside swap function: a = %d, b = %d\n", *p, *q);
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
printf("After swap inside swap function: a = %d, b = %d\n", *p, *q);
}

11.
//WAP for Finding the Cube of a Number Using Pass by Reference

#include <stdio.h>

int cube(int *);


int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int res = cube(&num);
printf("The cube of %d is: %d\n", num, res);
return 0;
}

int cube(int *i) {


return (*i) * (*i) * (*i);
}
12.
/*WAP to calculate the simple interest with
the help of a function and call by reference method.*/

#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;
}

float simple_interest(float *a, float *b, int *c) {


float SI = (*a * *b * *c) / 100;
return SI;
}

You might also like