Tuesday, January 31, 2023

Calculate the volume of a sphere

Write a program in C++ to calculate the volume of a sphere.

The Volume of a Sphere may be calculated in following terms

in terms of radius

V = 4/3πr3

V ≈ 4.1888r3

in terms of surface area

V = A3/2 6π

V ≈ 0.09403A3/2

in terms of circumference

V = C32

V ≈ 0.01689C3

Where

r = radius
V = volume
A = surface area
C = circumference
π = pi = 3.1415926535898
√ = square root



Sample Solution:

C++ Code :

#include <iostream>

using namespace std;

    int main()

    {

            int rad1;

            float volsp;

            cout << "\n\n Calculate the volume of a sphere :\n";

            cout << "---------------------------------------\n";                       

            cout<<" Input the radius of a sphere : ";

            cin>>rad1;

            volsp=(4*3.14*rad1*rad1*rad1)/3;

            cout<<" The volume of a sphere is : "<< volsp << endl;

            cout << endl;

            return 0;

    }

Sample Output:

Calculate the volume of a sphere:

----------------------------------------------------

Input the radius of a sphere:    6

The volume of a sphere is :     904.32

 

Wednesday, January 25, 2023

Swap two numbers

 Write a program in C++ to swap two numbers.

Swapping:

Swapping in computer science means shuffling or interchanging. Mutually exchanging two values is called swapping.

Using a temporary variable :

The simplest method to swap two variables is to use a third temporary variable. The temporary variable is used to keep the value of one of the variables temporarily. The values in variables may be a numeric or alphabetic or any other characters I.e. alphanumeric, symbols etc. Temporary variable is a third variable along with two other variables carrying values. Temporary variable is empty but initialized at declaration and no value is assigned. The value is assigned at the time of shuffling of variables. For this , the value of one variable carrying values is assigned to the temporary variable thus that variable becomes empty. Now the value of the other value variable is assigned to the first recently emptied variable. At the end the value of the temporary variable is assigned to the second variable. Now the temporary variable is again empty and both the numbers are swapped. The same is shown in figure.

Pseudo code:

Here is the pseudo code for swapping two numbers.

define swap(x, y)

    temp := x

    x := y

y := temp

Sample Solution :-

C++ Code :

#include <iostream> 

using namespace std; 

int main() 

{ 

    cout << "\n\n Swap two numbers :\n"; 

    cout << "-----------------------\n"; 

    int num1, num2, temp; 

    cout << " Input 1st number : "; 

    cin >> num1 ; 

    cout << " Input 2nd number : "; 

    cin >> num2; 

    temp=num2; 

    num2=num1; 

    num1=temp; 

    cout << " After swapping the 1st number is : "<< num1 <<"\n" ; 

    cout << " After swapping the 2nd number is : "<< num2 <<"\n\n" ; 

}

Sample Output:

Swap two numbers:

Input 1st number : 35

Input 2nd number : 29

After swapping, the 1st number is : 29

After swapping, the 2nd number is : 35

Flowchart:

Here is the flowchart for swapping numbers:



Tuesday, January 24, 2023

Display the operation of pre and post increment and decrement.

 Write a program in C++ to display the operation of pre and post increment and decrement.

Increment:

It is an increase in number and may be of any amount like 1,2 etc. It is always done with the integer or numeric data type. The number is raised to the upper level, for example, 4 is raised to 5.

Decrement:

It is a decrease in number and may be of any amount like 1,2 etc. It is always done with the integer or numeric data type. The number is downed to the lower level, for example, 4 goes down to 3.

Pre:

This keyword is used to increase or decrease the number before any other operation is performed.

Post:

This keyword is used to increase or decrease the number after any other operation is performed.

Sample Solution:

C++ Code :

#include <iostream> 

using namespace std; 

int main() 

{ 

int num = 57; 

cout << "\n\n Display the operation of pre and post increment and                    decrement :\n"; 

cout << "------------------------------------------------ --------------------\n"; 

cout <<" The number is : " << num << endl; 

num++; 

// increase by 1 (post-increment) 

cout <<" After post increment by 1 the number is : " <<  num << endl; 

++num; 

// increase by 1 (pre-increment) 

cout <<" After pre increment by 1 the number is : " <<  num << endl; 

num = num + 1; 

// num is now increased by 1. 

cout <<" After increasing by 1 the number is : " << num << endl; 

// 79 num--; 

// decrease by 1 (post-decrement) 

cout <<" After post decrement by 1 the number is : " <<  num << endl; 

--num; // decrease by 1 (pre-decrement) 

cout <<" After pre decrement by 1 the number is : " <<  num << endl; 

num = num - 1; 

// num is now decreased by 1. 

cout <<" After decreasing by 1 the number is : " << num << endl; 

cout << endl; 

return 0; 

}

Sample Output:

Display the operation of pre and post increment and decrement:

--------------------------------------------------------------------------------------

The number is : 57

After post increment by 1 the number is : 58

After pre increment by 1 the number is : 59

After increasing by 1 the number is : 60

After post decrement by 1 the number is : 59

After pre decrement by 1 the number is : 58

After decreasing by 1 the number is : .57

Saturday, January 21, 2023

Convert from celcius degrees to Kelvin and Fahrenheit.

 Write a C# Sharp program to convert from Celsius degrees to Kelvin and Fahrenheit.

kelvin = Celsius + 273
Fahrenheit = Celsius x 18 / 10 + 32

The units used to measure temperature are Celsius, kelvin and Fahrenheit. In routine life room temperature is measured in degree Celsius and human body temperature is measured in degree Fahrenheit. Thus it is usually needed to convert from one degree to another. For such conversion, there are some standard rules. Here are the formulas to convert temperature.

Celsius = 0.56 × (°Fahrenheit - 32)°

Fahrenheit = (1.8 × °Celsius) + 32

Kelvin = (273 + °Celsius)

Method: 

To convert temperature from one degree to another, the specified formula is used. First of all, input in Celsius degree is taken using ReadLine method of C# language. Then by using the standard formula, given value is converted into kelvin and Fahrenheit and displayed using WriteLine method of C# language.

Pseudo code: 

define convert(x)

F = (x * 1.8) + 32

K = x + 273

Print F

Print K

Sample Solution:

C# Sharp Code:

using System; 

public class Exercise1

{ 

public static void Main( ) 

{

Console.Write("Enter the amount of Celsius: "); 

int celsius = Convert.ToInt32(Console.ReadLine());                  

Console.WriteLine("Kelvin = {0}", celsius + 273); 

Console.WriteLine("Fahrenheit = {0}", (celsius*1.8)+ 32);

} 

}

Sample Output:

Enter the amount in Celcius: 40

Kelvin: 313

Fahrenheit: 104

Flowchart:

Here is the flowchart to convert temperature from one degree to the other.



Popular Posts