29/01/2023

Swapping of Two Numbers in C++ Using Functions | Call by Value
Swapping of Two Numbers in C++ Using Functions Call by Reference and Call by Value. There are two methods to solve this problem with the help of functions. The first one is Call By Value and the second one is Call by Reference. In the Call by Value Function, we can pass the value and in the Call by Reference Function we pass the Address of the variable. Before going diving into the Call by Reference and Call by Value.

Swapping of Two Numbers in C++ Using Functions

There are 2 things we have to discuss Actual Parameter and the Formal Parameter to fully understand the Swapping of Two Numbers in C++ Using Functions. Swapping of two numbers in this article I am going to explain Call By Value, Call by Reference, Actual Parameter and Formal Parameter. So all the doubts will be clear to understand this problem.

Check This: C++ Program to Calculate Standard Deviation Using Function

Swapping of Two Numbers in C++ Using Functions | Call by Reference


#include<iostream>
using namespace std;

void swap(int *x ,int *y );
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/
int main()
{
   //Program by- Ghanendra Yadav
    int a,b;
    cout<<"Enter Two Numbers To Swap: ";
    cin>>a>>b;
    
    swap(&a,&b);
    
    cout<<"\nAfter Swapping Two Numbers: ";
    cout<<a<<" "<<b<<" \n";
    
    return 0;
}
void swap(int *x,int *y)
{
 int z;
 z=*x;
/*Copying the first variable Address to the tempriory variable*/
 *x=*y;
/*Copying the second variable Address to the first variable*/
 *y=z;
/*Copying the tempriory variable Address to the second variable*/ 
}

Swapping of Two Numbers in C++ Using Call by Value Functions


#include<iostream>
using namespace std;

void swap(int ,int );
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/
int main()
{
   //Program by- Ghanendra Yadav
    int a,b;
    cout<<"Enter the Two Numbers to Swap in C++: ";
    cin>>a>>b;
    cout<<"\nAfter Swapping of Two Numbers:";
    swap(a,b);
    
    return 0;
}
void swap(int x,int y)
{
 int z;
/*Extra veriable for storing the value of first or second variable*/ 
 
 z=x;
/*Copying the first variable value to the tempriory variable*/
 x=y;
/*Copying the second variable value to the first variable*/
 y=z;
/*Copying the tempriory variable value to the second variable*/ 
 cout<<" "<<x<<"   "<<y;
 
}

Swapping of Two Numbers Using Function Call by Reference & Call by Value


1. Call by Value

In Call by Value Actual parameters are passed while calling the function, The operation's effect on the formal parameters doesn't reflect on the Actual Parameters.

Example: Int A = 5 is an actual parameter and Int X = 5(Here we have Copied the Int A = 5 value to the X = 5), so whatever we do with the X, it does not reflect the Actual Value Int A = 5. It will always remain the same. If we increase the value of the X by 1 then the value of the X will be 6 and the value of the A remains the same as 5 as previously.

2. Call by Reference


In Call by Reference, we passed the address of the actual parameter in a formal parameter, So the changes on the Formal Parameters reflect on the Actual parameters. If we take the above example for this then if we increase the value of the X will reflect on the A thus the value of the X and A will be the same(X = A = 6)

Before going to understand the Call by value and Call by Reference let's first understand the Actual and Formal Parameters terminology to fully understand the code.

Actual Parameters: The Actual parameters that appear in function calls.
Formal Parameters: The Formal parameters that appear in function declarations.

The Output of Swapping of Two Numbers in C++ Using Call by Value


The Output of Swapping of Two Numbers in C++ Using Call by Value

Program Output of  Swapping of Two Numbers Using Call by Value


Swapping of Two Numbers in C++ Using Functions

Write a C Program to Display The Size of Different Data Types
Write a c program to display the size of different data types. Here is one thing noted down the size of Datatype maybe depends upon your Operating System. Operating System You Are using maybe 32 Bit or 64 Bit. The data type of problems can handle easily in C++. C++ have a "sizeof" Operator to find the size of any datatype following the given syntax. The size of data types in c can be solved by using the following syntax.

Syntax: sizeof( datatype )

Write a C Program to Display The Size of Different Data Types

See Also: C++ Program to Display The Size of Different Data Types

C Program to Display The Size of Different Data Types


#include <stdio.h>

int main() {
  printf("Size of Int Data Types in C = %2d bytes \n", sizeof(short int));
  printf("Size of Long Int Data Types in C = %2d bytes \n", sizeof(long int));
  printf("Size of Float Data Types in C = %2d bytes \n", sizeof(float));
  printf("Size of Double Data Types in C = %2d bytes \n", sizeof(double));
  printf("Size of Long Double Data Types in C = %2d bytes \n", sizeof(long double));
  printf("Size of Char Data Types in C = %2d bytes \n", sizeof(char));
  return 0;
}

See Also: Java Program to Display The Size of Different Data Types

The Output of Print Size of Data Types in C


The Output of Print Size of Data Types in C

List of Size of Data Types in C


Below is the list of the Data Types in C or The size of data types in c. C Programming Data Type, Range and Size in Bytes.

  1. Int
  2. Long Int
  3. Float
  4. Char
  5. Double
  6. Long Double

TypeRangeSize (in bytes)
Unsigned Char0 to 2551
Signed Char or Char-128 to +1271
Unsigned Int0 to 655352
Signed Int or Int-32,768 to +327672
Unsigned Short Int0 to 655352
Signed Short Int or Short Int-32,768 to +327672
Unsigned Long Int0 to +4,294,967,2954
Signed Long Int or Long Int-2,147,483,648 to +2,147,483,6474
Long Double3.4E-4932 to 1.1E+493210
Double1.7E-308 to 1.7E+3088
Float3.4E-38 to 3.4E+384

28/01/2023

C++ Program to Find Area of a Triangle/Square/Circle/ Rectangle Using Switch Statement.
Write a C++ Program to Find Area of a Triangle Square Circle Rectangle Using Switch Statement. In this one problem, we have four problems, Area of Triangle, Area of Square, Area of Circle and Area of Rectangle. We are going to solve this problem using a Switch Case Statement. So according to the user input, our problem will display an answer. 

There will be a total of 4 Choices for the user, according to the input of the user particular case will be performed. Here the first user has to make a choice to find the Area of a Triangle, the Area of a Square, the Area of a Circle and the Area of a Rectangle.

C++ Program to Find Area of a Triangle Square Circle Rectangle Using Switch Statement

After that according to the choice again user has to provide the necessary extra details to perform the operation. So first we must know the formulas of the given problem, and then we will implement them in our program using a switch case. So first we need to find the value of PI for the area of the radius,

Formulas for the Area of a Triangle, Square, Circle and Rectangle

Area of Triangle = (Base * Height) / 2.

Area of Square = Side * Side or Side2

Area of Circle = 3.14159 * radius * radius; or πR2
 
Area of Rectangle = Length * Width


C++ Program to Find Area of a Triangle Square Circle Rectangle


#include<iostream>

#include<math.h>

using namespace std;
int main() {
  /*
  write a c++ program to find area of a triangle / square / circle / rectangle using switch statement.

  Visit: https://www.programmingwithbasics.com/2015/11/write-c-to-calculate-area-of-circle.html

  */

  float a, b, c, s, radius, area;

  int choice;

  cout << "Press 1 for Area of a Triangle";
  cout << "\nPress 2 for Area Of Square";
  cout << "\nPress 3 for Area Of Circle ";
  cout << "\nPress 4 for Area Of Rectangle\n";
  cout << "\nEnter Your Choice :";

  cin >> choice;

  switch (choice) {
  case 1: {
    cout << "\nEnter Base and Height of Triangle: ";
    cin >> a >> b;
    area = (a*b) / 2 ; 
    cout << "\nArea of Triangle = " << area << endl;
    break;
  }
  case 2: {
    cout << "\nEnter the Side Of Square: ";
    cin >> a;
    area = a * a;
    cout << "\nArea of Square = " << area << endl;
    break;
  }
  case 3: {
    cout << "\nEnter the Radius of Circle: ";
    cin >> radius;
    area = 3.14159 * radius * radius;
    cout << "\nArea of Circle = " << area << endl;
    break;
  }
  case 4: {
    cout << "\nEnter the Length and Width of Rectangle: ";
    cin >> a >> b;
    area = a * b;
    cout << "\nArea Of Rectangle = " << area << endl;
    break;
  }
  default:
    cout << "\n Invalid Choice!!! Choose Between 1 to 4";
    break;
  }
  return 0;
}

The Output of Find Area of a Triangle Square Circle Rectangle in C++


The Output of Find Area of a Triangle Square Circle Rectangle in C++

Similar to the Find Area of a Triangle Square Circle Rectangle

Structure Program for Student Details in C | Code with Output

Write a Structure Program for Student Details in C or Write a Program to Store and Print the Roll No., Name, Age and Marks of a Student Using Structures. We have to create a Structure with Student Name, Roll No and Marks. Now the User has to Enter the Student details manually Ie. Student Name, Student Roll Number & Student Marks: At the end, we have to print the Student details using the same respectful manner as Input using Structure (Name, Roll & Marks).

Before coding, we must know What is Structure, the Definition of Structure and the Syntax of Structure. We have to write a program to store and print the roll no., name, age and marks of a student using structures. 

Structure Program for Student Details in C

Below is the C Program to Display Student Details Using Structure. In this solution, we are going to Store and Print the Name, Roll No, and Marks of a Student Using Structures in C Programming. I will give you an assignment to all of you, try to solve this problem by slightly modifying the below code. The program statement: write c program to store information of students (Roll no, Name, Branch and Year) using structure.

Structure Program for Student Details in C


#include <stdio.h>
struct student
{
    char name[50];
    int roll;
    float marks;
};
int main()
{
    struct student s;
    
 printf("Enter The Information of Students :\n\n");
    
 printf("Enter Name : ");
    scanf("%s",s.name);
    
 printf("Enter Roll No. : ");
    scanf("%d",&s.roll);
    
    printf("Enter marks : ");
    scanf("%f",&s.marks);
    
    printf("\nDisplaying Information\n");
    
 printf("Name: %s\n",s.name);
    printf("Roll: %d\n",s.roll);
    printf("Marks: %.2f\n",s.marks);
    return 0;
}

C Program to Display Student Details Using Structure Output


C Program to Display Student Details Using Structure Output

What Is Structure


The structure is a user-defined data type in C. Structure represents a record. Suppose you want to store a student record of the student's name, address, roll number and age. You can define a structure to hold this information. C Program for Structure. Write a C program to display student details using structure.

Defining a Structure


struct keyword is used to define a structure. struct defines a new data type, a collection of different data types. Below is the Syntax of the Structure for the C Program to Display Student Details Using Structure.

Syntax of Structure


struct structure_name
{
//Statements
};

Below is the C Program to display the student's name, roll number and marks.

The Logic for C Program to Display Student Details Using Structure


Here we are Defining the Structure and the Data Types with Structure. structure program for student details in c to store the Student Name, Roll no, and Student Marks.

struct student
{
    char name[50];
    int roll;
    float marks;
};

Here we are storing single student information ie, Student Name, Student Roll Number and Student Marks without using the FOR LOOP.


printf("Enter The Information of Students :\n\n");

scanf("%s",s.name);
scanf("%d",&s.roll);
scanf("%f",&s.marks); 

Here we are Printing the Student Details, Student Name, Student Roll Number and Student Marks without using any LOOPS.


printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);

Try This C Program To Store Multiple Student Information Using Structure

Related to Structure Program in C