C and CPP Record II Sem CA
C and CPP Record II Sem CA
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int number1,number2,sum;
clrscr();
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2; // calculating sum
printf("Addition of two integers = %d",sum);
getch();
}
Output :-
Enter two integers: 12
11
Addition of two integers =
23
2. Write a C Program to Print an Integer (Entered by the User).
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf("Enter an integer: ");
scanf("%d",&number); // reads and stores input
printf("You entered: %d", number); // displays output
getch();
}
1
Output:-
Enter an integer: 25
You entered: 25
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
double a, b, product;
clrscr();
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
product = a * b;// Calculating product
// Result up to 2 decimal point is displayed using %.2lf
printf("Product = %.2lf", product);
getch();
}
Output:-
Enter two numbers: 2.4
1.12
Product=2.69
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
2
clrscr();
printf("Enter a character: ");
scanf("%c",&c);
Output:-
Enter a character: G
ASCII value of G = 71
#include<stdio.h>
#include<conio.h>
void main()
{
int dividend, divisor, quotient, remainder;
clrscr();
printf("Enter dividend: ");
scanf("%d",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
Output:-
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
3
6. Write a C Program to Find the Size of int, float, double and char.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int intType;
float floatType;
double doubleType;
char charType;
clrscr();
// sizeof() evaluates the size of a variable
printf("Size of int: %ld bytes\n",sizeof(intType));
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));
getch();
}
Output:-
Size of int: 2 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
#include<stdio.h>
#include<conio.h>
void main()
{
int first, second, temp;
clrscr();
printf("Enter first number: ");
scanf("%d",&first);
printf("Enter second number: ");
scanf("%d",&second);
printf("\nBefore swapping, firstNumber = %d secondNumber = %d
\n",first,second);
4
// Value of first is assigned to temp
temp = first;
Output:-
Enter first number: 20
Enter second number: 45
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter an integer: ");
scanf("%d",&num);
getch();
}
Output:-
Enter an integer: 7
7 is odd.
5
9. Write a C Program to Check Odd or Even Using the Ternary Operator.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter an integer: ");
scanf("%d",&num);
getch();
}
OUTPUT:-
Enter an integer: 33
33 is odd.
10. Write a C Program to Check Whether a Character is a Vowel or Consonant.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf ("Please Enter an alphabet: \n");
scanf(" %c", &ch);
6
getch();
}
OUTPUT:-
Enter an alphabet: G
G is a consonant.
11. Write a C Program to Find the Largest Number Among Three Numbers.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, n3;
clrscr();
printf("Enter three numbers: ");
scanf("%d %d %d ",&n1,&n2,&n3);
if(n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
else if(n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
getch();
}
OUTPUT:-
There are two conditions for leap year: 1- If year is divisible by 400 (for Century years), 2- If year is
divisible by 4 and must not be divisible by 100 (for Non Century years).
For example,
7
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year: ");
scanf("%d", &year);
if((year % 4 == 0 && year % 100!= 0 ) || (year % 400 == 0) )
{
printf("%d is a leap year", year);
}
else
{
printf("%d is not a leap year", year);
}
getch();
}
OUTPUT:-
OUTPUT:-
Within this C Program to check the Character is Alphabet or Not example, the first printf
statement will ask the user to enter any character. Next, the character will assign to variable ch.
if( (ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’))
Within the If statement, the First condition (ch >= ‘a’ && ch <= ‘z’) will check whether the
character entered by the user is between a and z. The second condition (ch >= ‘A’ && ch <= ‘Z’)
will check whether the character entered by the user is between A and B.
If one of those conditions is TRUE, the character entered by the user is an Alphabet.
If both of the above conditions is FALSE, the character entered by the user is Not an
Alphabet.
8
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("Enter a character: ");
scanf("%c",&c);
getch();
}
OUTPUT:-
Enter a character: *
* is not an alphabet
This C program check whether the input character is Alphabet or not using one of the built-in
function isapha()
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\n Please Enter any character \n");
scanf("%c", &ch);
if(isalpha(ch) )
printf("\n%c is an Alphabet", ch);
else
printf("\n %c is not an Alphabet", ch);
getch();
}
9
OUTPUT:-
ANALYSIS
isalpha(ch) function check whether the given character (ch) is Alphabet or not.
If the condition isalpha(ch) is TRUE, the character is an Alphabet.
If the condition isalpha(ch) is FALSE, the character is Not an Alphabet.
14. Write a C Program to Calculate the Sum of first ‘N’ Natural Numbers.
The positive numbers 1, 2, 3...are known as natural numbers. The sum of natural numbers up to 5
is:
Sum = 1 + 2 + 3 + 4 + 5
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
10
OUTPUT:-
Enter a positive integer:5
Sum=15
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum =0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
i =1;
while(i <= n)
{
sum+= i;
++i;
}
OUTPUT:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum =0;
clrscr();
11
do{
printf("Enter a positive integer: ");
scanf("%d",&n);
}while(n <=0);
OUTPUT:-
Sum=15
1. Factorial of n: (n!)=1*2*3*4....n
This program takes a positive integer from the user and computes the factorial using for loop.
Since the factorial of a number may be very large, the type of factorial variable is declared as
unsigned long long.
If the user enters a negative number, the program displays a custom error message.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
unsigned long long fact =1;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);
12
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i =1; i <= n;++i)
{
fact*= i;
}
printf("Factorial of %d = %llu", n, fact);
}
getch();
}
OUTPUT:-
Enter an integer: 10
Factorial of 10 = 3628800
The program below takes an integer input from the user and generates the multiplication tables
up to 10.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
getch();
}
13
OUTPUT:-
Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms.
The first two terms of the Fibonacci sequence are 0 followed by 1.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, t1 =0, t2 =1,nextTerm;
clrscr();
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
14
OUTPUT:-
This program takes an integer from the user and calculates the number of digits. For example: If
the user enters 2319, the output of the program will be 4.
The integer entered by the user is stored in variable n. Then the while loop is iterated until the
test expression n! = 0 is evaluated to 0 (false).
After the first iteration, the value of n will be 345 and the count is incremented to 1.
After the second iteration, the value of n will be 34 and the count is incremented to 2.
After the third iteration, the value of n will be 3 and the count is incremented to 3.
After the fourth iteration, the value of n will be 0 and the count is incremented to 4.
Then the test expression of the loop is evaluated to false and the loop terminates.
15
OUTPUT:-
This program takes an integer input from the user. Then the while loop is used until n!= 0 is
false (0).
In each iteration of the loop, the remainder when n is divided by 10 is calculated and the value of
n is reduced by 10 times.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev =0, remainder;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);
while(n !=0)
{
remainder= n %10;
rev= rev *10+ remainder;
n/=10;
}
printf("Reversed number = %d", rev);
getch();
}
OUTPUT:-
16
20. Write a C Program to Check Whether a Number is Palindrome or Not.
An integer is a palindrome if the reverse of that number is equal to the original number.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, reversedN = 0, remainder, originalN;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
getch();
}
OUTPUT:-
17
21. Write a C Program to Check Whether a Number is Prime or Not.
A prime number is a positive integer that is divisible only by 1 and itself. For example: 2, 3, 5, 7,
11, 13, 17
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, flag = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
getch();
}
OUTPUT:-
18
In the program, a for loop is iterated from i = 2 to i < n/2.
1. if(n % i ==0){
2.
3. }
If n is perfectly divisible by i, n is not a prime number. In this case, flag is set to 1, and the loop
is terminated using the break statement.
After the loop, if n is a prime number, flag will still be 0. However, if n is a non-prime number,
flag will be 1.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num,originalNum, remainder, result =0;
clrscr();
printf("Enter a three-digit integer: ");
scanf("%d",&num);
originalNum=num;
while(originalNum!=0)
{
remainder=originalNum%10;
result+= remainder * remainder * remainder;
originalNum/=10;
}
if(result ==num)
printf("%d is an Armstrong number.",num);
else
printf("%d is not an Armstrong number.",num);
getch();
}
19
OUTPUT:-
This program takes an arithmetic operator +, -, *, / and two operands from the user. Then, it
performs the calculation on the two operands depending upon the operator entered by the user.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char operator;
clrscr();
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
getch();
}
20
OUTPUT:-
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, space, rows, k=0;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows;++i,k=0)
{
for(space=1; space<=rows-i;++space)
{
printf(" ");
}
while(k!=2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
getch();
}
OUTPUT:-
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
21
27. Write a C Program to Convert Binary Number to Decimal and vice-versa.
We can obtain a decimal number by multiplying each digit of binary number with power of 2
and adding each multiplication result. The power starts from 0 and goes to n-1 where n is
the total number of digits in binary number.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
long int i,n,x=0,a;
clrscr();
printf("Enter any binary number: ");
scanf("%ld",&n);
printf("\nThe decimal conversion of %ld is ",n);
for(i=0;n!=0;++i)
{
a=n%10;
x=(a)*(pow(2,i))+x;
n=n/10;
}
printf("%ld",x);
getch();
}
OUTPUT:-
22
28. Write a C Program to Check Prime or Armstrong Number Using
User-defined Function.
The checkPrimeNumber() function returns 1 if the number entered by the user is a prime number.
Similarly, checkArmstrongNumber() function also returns 1 if the number entered by the user is an
Armstrong number.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<math.h>
#include<stdio.h>
#include<conio.h>
int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);
void main()
{
int n, flag;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
int checkPrimeNumber(int n)
{
int i, flag =1;
for(i =2; i <= n /2;++i){
// condition for non-prime number
if(n % i ==0){
flag=0;
break;
}
}
return flag;
}
23
int checkArmstrongNumber(intnum)
{
int original, rem, result =0, n =0, flag;
original=num;
while(original !=0){
original/=10;
++n;
}
original=num;
while(original !=0)
{
rem= original %10;
result+=pow(rem, n);
original/=10;
}
// condition for Armstrong number
if(result ==num)
flag=1;
else
flag=0;
return flag;
}
OUTPUT:-
Enter a positive integer:
407
407 is not a prime number.
407 is an Armstrong number.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
int power(int n1,int n2);
void main()
{
int base, a, result;
clrscr();
printf("Enter base number: ");
scanf("%d",&base);
printf("Enter power number(positive integer): ");
scanf("%d",&a);
result= power(base, a);
printf("%d^%d = %d",base, a, result);
getch();
}
24
int power(intbase,int a)
{
if(a !=0)
return(base* power(base, a -1));
else
return1;
}
OUTPUT:-
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
int hcf(int n1,int n2);
void main()
{
int n1, n2;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
printf("G.C.D of %d and %d is %d.", n1, n2,hcf(n1, n2));
getch();
}
inthcf(int n1,int n2)
{
if(n2 !=0)
returnhcf(n2, n1 % n2);
else
return n1;
}
OUTPUT:-
25
31. Write a C Program to Calculate Average Using Arrays.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
float num[100], sum =0.0,avg;
clrscr();
printf("Enter the numbers of elements: ");
scanf("%d",&n);
avg= sum / n;
printf("Average = %.2f",avg);
getch();
}
OUTPUT:-
26
32. Write a C Program to Find Largest Element in an Array.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float arr[100];
clrscr();
printf("Enter the number of elements (1 to 100): ");
scanf("%d",&n);
for(i =0; i < n;++i)
{
printf("Enter number%d: ", i +1);
scanf("%f",&arr[i]);
}
// storing the largest number to arr[0]
for(i =1; i < n;++i)
{
if(arr[0]<arr[i])
arr[0]=arr[i];
}
OUTPUT:-
27
33. Write a C Program to Add Two Matrices Using Multi-dimensional Arrays.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
clrscr();
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d",&r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d",&c);
28
OUTPUT:-
10 8 6
34. Write a C Program to Find the Length of a String.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char s[]="Programming is fun";
int i;
crlscr();
for(i =0; s[i]!='\0';++i);
OUTPUT:-
29
// C program to find the length of
// string using strlen function
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
Char Str[1000];
int i;
clrscr();
printf("Enter the String: ");
scanf("%s", Str);
printf("Length of Str is %ld", strlen(Str));
getch();
}
OUTPUT:-
30
OUTPUT:-
// terminating s1 string
s1[i]='\0';
getch();
}
OUTPUT:-
31
36. Write a C Program to Copy String without usingstrcpy ().
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100],s2[100],i;
clrscr();
printf("Enter string s1: ");
fgets(s1,sizeof(s1),stdin);
for(i =0; s1[i]!='\0';++i)
{
s2[i]= s1[i];
}
s2[i]='\0';
printf("String s2: %s", s2);
getch();
}
OUTPUT:-
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char line[150];
clrscr();
int vowels, consonant, digit, space;
32
if(line[i]=='a'|| line[i]=='e'|| line[i]=='i'||line[i]=='o'||
line[i]=='u'|| line[i]=='A'||line[i]=='E'|| line[i]=='I'||
line[i]=='O'||line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z')||(line[i]>='A'&& line[i]<='Z'))
{
++consonant;
}
else if(line[i]>='0'&& line[i]<='9')
{
++digit;
}else if(line[i]==' ')
{
++space;
}
}
OUTPUT:-
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str[1000], ch;
int count =0;
clrscr();
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);
33
for(int i =0; str[i]!='\0';++i)
{
if(ch == str[i])
++count;
}
OUTPUT:-
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int data[5];
clrscr();
printf("Enter elements: ");
for(int i =0; i <5;++i)
scanf("%d", data + i);
OUTPUT:-
Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4
34
40. Write a C program to create, initialize, assign and access a pointer variable.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num;/*declaration of integer variable*/
int *pNum;/*declaration of integer pointer*/
clrscr();
pNum=#/*assigning address of num*/
num=100;/*assigning 100 to variable num*/
getch();
}
OUTPUT:-
35
41. Write a C program to swap two numbers using pointers
/*
Name:
H.T No:
Section:
Aim:
*/
#include <stdio.h>
void main()
{
int num1,num2;
return 0;
}
OUTPUT:
36
42.Write a C program to count vowels and consonants in a string using pointer?
#include <stdio.h>
void main()
{ char str[100];
char *p;
int vCount=0,cCount=0;
Output:
37
43. Write a C Program to Store Information of a Student Using Structure?
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
void main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output
Enter information of students:
Roll number: 1
Name: Tom
Marks: 98
.
.
38
44. Write a C Program to Add Two Distances (in inch-feet system) using Structures.?
#include <stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, result;
void main() {
// take first distance input
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
// adding distances
result.feet = d1.feet + d2.feet;
result.inch = d1.inch + d2.inch;
39
45. Write a C Program to Store Information of Students Using Structure?
46. Write a C program to declare, initialize an union?
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
void main() {
j.salary = 12.3;
40
47. Write a C++ program to implement function overloading?
#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);
Output:
r1 is : 42
r2 is : 0.6
41
48. Write a C++ program to calculate an area of rectangle using encapsulation?
#include <iostream>
using namespace std;
class Rectangle {
public:
// Variables required for area calculation
int length;
int breadth;
void main() {
// Create object of Rectangle class
Rectangle rect(8, 6);
return 0;
}
Output
Area = 48
42
49. Write a C++ program to add two numbers using data abstraction?
#include <iostream>
using namespace std;
void main() {
// prints sum
cout << first_number << " + " << second_number << " = " <<
sum;
return 0;
}
Run Code
Output
Enter two integers: 4
5
4 + 5 = 9
43
50.Write a C++ program to overload binary operators?
// C++ program to overload the binary operator +
// This program adds two complex numbers
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag <<
"i";
else
cout << "Output Complex number: " << real << "+" <<
imag << "i";
}
};
int main() {
Complex complex1, complex2, result;
44
// complex1 calls the operator function
// complex2 is passed as an argument to the function
result = complex1 + complex2;
result.output();
return 0;
}
Run Code
Output
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i
45