C++ 3
C++ 3
Algorithm…..
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(a,b)
Step 3a: Start function.
Step 3b: Assign t ← x
Step 3c: Assign x ← y
Step 3d: Assign y ← t
Step 3e: Print x and y.
Step 3f: End function.
Step 4: Stop the program.
Source code…
#include<stdio.h>
#include<conio.h>
void swap(int , int); // Declaration of function
void main( )
{
int a = 10, b = 20 ;
clrscr();
printf("n Before swapping");
printf ( "n a = %d b = %d", a, b ) ;
swap(a,b);// call by value : a and b are actual parameters
getch();
}
void swap( int x, int y ) // x and y are formal parameters
{
int t ;
t=x;
x=y;
y=t;
printf("n After swapping");
printf ( "n a = %d b = %d", x, y ) ;
}
output
NAME – SACHIN BISHT
Roll no. : 48
2.Write a c program to swap a number using call by refrence
function ?
Algorithm
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(&a, &b)
Step 3a: Start function
Step 3b: Assign t ← *x
Step 3c: Assign *x ← *y
Step 3d: Assign *y ← t
Step 3e: End function
Step 4: Print x and y.
Step 5: Stop the program.
Source code.
#include<stdio.h>
#include<conio.h>
void swap(int *,int *); // Declaration of function
void main( )
{
int a = 10, b = 20 ;
clrscr();
printf("n Before swapping");
printf( "n a = %d b = %d", a, b );
swap(&a,&b); // call by reference
printf("n After swapping");
printf( "n a = %d b = %d", a, b );
getch();
}
void swap( int *x, int *y )
{
int t;
t = *x;
*x = *y;
*y = t;
}
OUTPUT
Algorithm
1. Declare 4 variables - num1, num2, result, opt. The variables
num1 and num2 are for storing the values or operands, for
storing the calculation result, and opt to take the operator as
inputs.
2. Take inputs of the operands, operator.
3. Use switch case or conditional statements to check the
operator.
4. Display the result according to the operator.
5. Exit the program
Source code.
#include <stdio.h>
int main()
{
int num1,num2;
float result;
char ch; //to store operator choice
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/,%): ");
scanf(" %c",&ch);
result=0;
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}
Roll no : 48
OUTPUT
4.Develop a program to find the sum of diagonal elements, upper triangular elements and
lower triangular elements using function for square matrix. Note: Make three functions for
sum of diagonal elements, sum of upper triangular elements and sum of lower triangular
elements.
ALGORITHM.
1. Start
2. Input the size of the square matrix.
3. Input the elements of the matrix.
4. Call the sumDiagonal function to calculate and print the sum of diagonal
elements.
5. Call the sumUpperTriangular function to calculate and print the sum of
upper triangular elements.
6. Call the sumGlobalTriangular function to calculate and print the sum of
global triangular elements.
7. End.
Source code
#include <stdio.h>
// Function to calculate the sum of diagonal elements
int sumDiagonal(int matrix[100][100], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += matrix[i][i];
}
return sum;
}
// Function to calculate the sum of upper triangular elements
int sumUpperTriangular(int matrix[100][100], int size) {
int sum = 0;
For (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
sum += matrix[i][j];
}
}
return sum;
}
// Function to calculate the sum of global triangular elements
int sumGlobalTriangular(int matrix[100][100], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) {
sum += matrix[i][j];
}
}
return sum;
}
int main() {
int size;
// Input matrix size
printf("Enter the size of the square matrix: ");
scanf("%d", &size);
// Input matrix elements
int matrix[100][100];
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Calculate and print the results
printf("Sum of diagonal elements: %d\n", sumDiagonal(matrix, size));
printf("Sum of upper triangular elements: %d\n", sumUpperTriangular(matrix, size));
printf("Sum of global triangular elements: %d\n", sumGlobalTriangular(matrix, size));
return 0;
}
Output
Let's assume a 3x3 matrix with the following elements:
Input:
123
456
789
Enter the size of the square matrix: 3
Enter the elements of the matrix:
123
456
789
Sum of diagonal elements: 15
Sum of upper triangular elements: 11
Sum of global triangular elements: 28