(1)
Aim:-Write a C Program to Add Two
Integers.
Program:-
#include<stdio.h>
void main()
{
inta,b,c;
printf(”Enter two numbers:”);
scanf(“%d %d”,&a,&b);
c=a+b;
printf(”Sum=%d”,c);
}
B.COM II SEM C&C++ Lab Programs Page 1
(2)
Aim:-Write a C Program to Print an Integer (Entered by theUser).
Program:-
#include<stdio.h>
void main()
{
inta;
printf(”Enter any number:”);
scanf(“%d”,&a);
printf(”youentered%d”,a);
}
B.COM II SEM C&C++ Lab Programs Page 2
(3)
Aim:-WriteaCProgramtoMultiplyTwoFloating-PointNumbers.
Program:-
#include<stdio.h>
void main()
{
floata,b,c;
printf(”Enter two float numbers:”);
scanf(“%f %f”,&a,&b);
c=a*b;
printf(”product %f”, c);
}
B.COM II SEM C&C++ Lab Programs Page 3
(4)
Aim:- Write a C Program to Find ASCII Value of a Character.
Program:-
#include <stdio.h>
void main()
{
char c;
printf("Enter a character:");
scanf("%c", &c);
printf("ASCII value of %c =%d", c, c);
}
B.COM II SEM C&C++ Lab Programs Page 4
(5)
Aim:- Write a C Program to Compute Quotient and Remainder.
Program:-
#include <stdio.h>
void main()
{
int a, b, quotient = 0, remainder = 0;
printf("Enter two numbers A and B : \n");
scanf("%d %d", &a, &b);
quotient = a / b;
remainder=a%b;
printf("Quotient when A/B is: %d\n", quotient);
printf("Remainder when A/B is: %d", remainder);
}
B.COM II SEM C&C++ Lab Programs Page 5
(6)
Aim:-Write a C Program to Find the Sizeof int, float, double and char.
Program:-
#include<stdio.h>
void main()
{
int intType;
float floatType;
double doubleType;
char charType;
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));
}
B.COM II SEM C&C++ Lab Programs Page 6
(7)
Aim:- Write a C Program to Swap Two Numbers Using Temporary Variable.
Program:-
#include<stdio.h>
void main()
{
int a, b, temp; .
printf("Enter a,b values\n");
scanf("%d %d", &a, &b);
printf("Before swapping a=%d b=%d\n\n", a, b);
temp=a;
a=b;
b=temp;
printf("After swapping a=%d b=%d\n\n", a, b);
}
B.COM II SEM C&C++ Lab Programs Page 7
(8)
Aim:- Write a C Program to Check Whether a Number is Even or Odd.
Program:-
#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d",&num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
B.COM II SEM C&C++ Lab Programs Page 8
(9)
Aim:-Write a C Program to Check Odd or Even Using the Ternary Operator.
Program:-
#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");
}
B.COM II SEM C&C++ Lab Programs Page 9
(10)
Aim:- Write a C Program to Check Whether a Character is a Vowel or
Consonant.
Program:-
#include <stdio.h>
void main()
{
char c;
int lowercase, uppercase; .
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase=(c =='a'||c=='e'||c=='i'||c=='o'||c=='u');
uppercase=(c=='A'||c=='E'||c=='I'||c=='O'||c=='U');
if (lowercase || uppercase)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
}
B.COM II SEM C&C++ Lab Programs Page 10
(11)
Aim:- Write a C Program to Find the Largest Number Among Three
Numbers.
Program:-
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter a, b, c values\n");
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c)
{
printf("a is greater\n");
}
elseif(b>a && b>c)
{
printf("b is greater\n");
}
elseif(c>a && c>b)
{
printf("c is greater\n");
}
}
B.COM II SEM C&C++ Lab Programs Page 11
(12)
Aim:-Write a C Program to Check Leap Year.
Program:-
#include <stdio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
printf("%disaleapyear.",year);
else
printf("%d isnota leapyear.",year);
}
else
printf("%disaleapyear.",year);
}
else
printf("%d is not a leap year.", year);
}
B.COM II SEM C&C++ Lab Programs Page 12
(13)
Aim:-Write a C Program to Check Whether a Character is an Alphabet or not.
Program:-
#include <stdio.h>
void main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a'&& c <= 'z') ||(c >= 'A'&& c <= 'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.",c);
}
B.COM II SEM C&C++ Lab Programs Page 13
(14)
Aim:-Write a C Program to Calculate the Sum off first ‘N’ Natural Numbers.
Program:-
#include<stdio.h>
voidmain()
{
int n, i, sum=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i=1;i<=n;++i)
{
sum+=i;
}
printf("Sum = %d", sum);
}
B.COM II SEM C&C++ Lab Programs Page 14
(15)
Aim:-Write a C Program to Find Factorial of a Number.
Program:-
#include<stdio.h>
1int fact(int);
voidmain()
{
int n, f; .
printf("Enter n value\n");
scanf("%d", &n);
f=fact(n);
printf("The factorial of the given number is %d\n",f); 1
}
int fact(int n)
{
if(n>1)
{
return(n*fact(n-1));
}
else
{
return n;
}
}
B.COM II SEM C&C++ Lab Programs Page 15
(16)
Aim:-Write a C Program to Generate Multiplication Table of a given
number.
Program:-
#include<stdio.h>
void main()
{
int n,i; .
printf("Enter a number:\n");
scanf("%d",&n);
printf("\nMultiplication Table of %d is\n",n);
for(i=1;i<=10;i++)
{
printf("\n %2d * %2d = %3d", n, i, n*i);
}
}
B.COM II SEM C&C++ Lab Programs Page 16
(17)
Aim:-Write a C Program to Display Fibonacci Sequence upto ‘n’ numbers.
Program:-
#include<stdio.h>
void main()
{
int a=0,b=1,c,n,i; .
printf("Enter n value\n");
scanf("%d", &n);
printf("The Fibonacci series is\n");
printf("%d", a);
printf("%d", b);
for(i=0;i<=n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("\t %d", c);
}
}
B.COM II SEM C&C++ Lab Programs Page 17
(18)
Aim:-Write a C Program to Count Number of Digits in an Integer.
Program:-
#include <stdio.h>
void main()
{
int n,count = 0;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
n/=10;
++count;
}
printf("Number of digits: %d", count);
}
B.COM II SEM C&C++ Lab Programs Page 18
(19)
Aim:- Write a C Program to Reverse a Number.
Program:-
#include<stdio.h>
void main ()
{
int num, rev=0,r,;
printf("Enter a number\n");
scanf("%d", &num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("The reverse of the given number is %d", rev);
}
B.COM II SEM C&C++ Lab Programs Page 19
(20)
Aim:-Write a C Program to Check Whether a Number is Palindrome or Not.
Program:-
#include<stdio.h>
1void main ()
{
int num, rev=0,r,k;
printf("Enter a number\n");
scanf("%d", &num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("The reverse of the given number is %d", rev);
if(rev==k)
{
printf("The given number is palindrome\n", k);
}
else
{
printf("The given number is not palindrome\n", k);
}
}
B.COM II SEM C&C++ Lab Programs Page 20
(21)
Aim:-Write a C Program to Check Whether a Number is Prime or Not.
Program:-
#include <stdio.h>
void main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
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);
}
}
B.COM II SEM C&C++ Lab Programs Page 21
(22)
Aim:- Write a C Program to Check whether the given number is an Armstrong
Number or not.
Program:-
#include<stdio.h>
void main()
{
int n, b=0,t=n, a; .
printf("Enter the number\n");
scanf("%d", &n);
while(n>0)
{
a=n%10;
b=b+(a*a*a);
n=n/10;
}
if(b==t)
{
printf("Armstrong number\n");
}
else
{
printf("Not an Armstrong number\n");
}
}
B.COM II SEM C&C++ Lab Programs Page 22
(23)
Aim:-Write a C Program to Make a Simple Calculator using switch...case.
Program:-
#include <stdio.h>
void main()
{
char operator;
float first,second;
printf("Enter any operator (+, -, *,/,%));
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%f %f", &first, &second);
switch (operator)
{
case '+':
printf(" %f + %f = %f", first, second, first + second);
break;
case '-':
printf("%f - %f = %f", first, second, first - second);
break;
case '*':
printf("%f*%f=%f",first,second,first*second); break;
case '/':
printf("%f / %f = %f", first, second, first / second);
break;
default:
printf("Error! Operator is not correct");
}
}
B.COM II SEM C&C++ Lab Programs Page 23
(24)
Aim:-Write a C Programming Code To Create Pyramid and Pattern.
B.COM II SEM C&C++ Lab Programs Page 24
(24)
Aim:-Write a C Programming Code To Create Pyramid and Pattern.
Program:-
#include<stdio.h>
int main()
{
int i, space, rows, k=0;
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");
}
}
B.COM II SEM C&C++ Lab Programs Page 25
(25)
Aim:-Write a C program to reverse a Sentence Using Recursion.
Program:-
#include<stdio.h>
void reverse();
void main()
{
printf("Please enter a sentence: ");
reverse();
}
void reverse()
{
char c;
scanf("%c", &c);
if (c != '\n')
{
reverse();
printf("%c", c);
}
}
B.COM II SEM C&C++ Lab Programs Page 26
(26)
Aim:-Write a C Program to Display Prime Numbers Between Intervals
Using Function.
Program:-
#include<stdio.h>
void main()
{
int n1,n2,i,j,c; .
printf("Enter the starting number and ending Number:");
scanf("%d %d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
c=0;
for(j=n1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\t",i);
}
}
}
B.COM II SEM C&C++ Lab Programs Page 27
(27)
Aim:-Write a C Program to Convert Binary Number to Decimal and
Vice-versa.
Program:-
#include<stdio.h>
void main()
{
long dec, rem, i=1;
long bin=0;
printf("Enter the decimal number: \n");
scanf("%ld", &dec);
printf("Given decimal number is %ld\n", dec);
while(dec>0)
{
rem=dec%2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
printf("The Equivalent binary number is %ld",bin);
}
B.COM II SEM C&C++ Lab Programs Page 28
(28)
Aim:-Write a C Program to Check Prime or Armstrong Number Using User-
defined Function.
Program:-
#include<stdio.h>
#include<math.h>
int Prime(int n);
int Armstrong(int n);
void main()
{
int n,flag;
printf("Enter a positive integer: ");
scanf("%d", &n);
flag=Prime(n);
if(flag==1)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
flag = Armstrong(n);
if(flag==1)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number", n);
return 0;
}
int Prime(int n)
{
int i, flag = 1;
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
B.COM II SEM C&C++ Lab Programs Page 29
flag=0;
break;
}
}
return flag;
}
int Armstrong(int number)
{
int originalNumber, remainder, result = 0, n = 0, flag;
originalNumber = number;
while(originalNumber!=0)
{
originalNumber/=10;
++n;
}
originalNumber = number;
while(originalNumber!=0)
{
remainder = originalNumber%10;
result += pow(remainder, n);
originalNumber /= 10;
}
if(result==number)
flag = 1;
else
flag = 0;
return flag;
}
B.COM II SEM C&C++ Lab Programs Page 30
(29)
Aim:-Write a C program to calculate the power using
recursion.
Program:-
#include<stdio.h>
int power(int n1, int n2);
void main()
{
int base, a, result; .
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);
}
int power(int base, int a)
{
if(a!=0)
{
return(base*power(base,a-1));
}
else
{
return1;
}
}
B.COM II SEM C&C++ Lab Programs Page 31
(30)
Aim:-Write a C Program to Find G.C.D Using Recursion.
Program:-
#include <stdio.h>
int hcf(int n1, int n2);
void main()
{
int n1,n2;
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));
}
int hcf(int n1,int n2)
{
if(n2!=0)
{
return hcf(n2,n1%n2);
}
else
{
return n1;
}
}
B.COM II SEM C&C++ Lab Programs Page 32
(31)
Aim:-Write a C Program to Calculate Average Using Arrays.
Program:-
#include <stdio.h>
#include <stdlib.h>
void main()
int arr[]={56,78,94,56,73,42,31,67};
int total=0,i;
for(i=0;i<=7;i++)
total=total+arr[i];
doubleavg=total/i;
printf("The average is: %.2f ",avg);
B.COM II SEM C&C++ Lab Programs Page 33
(32)
Aim:-Write a C Program to Find Largest Element in an Array.
Program:-
#include<stdio.h>
void main()
{
int a[15], i, n, largest;
printf("\nEnter Total Number of Elements in an Array up to 15:");
scanf("%d", &n);
printf("\n Enter all the values till %d: ",n);
for(i=0; i < n; i++)
scanf("%d", &a[i]);
largest = a[0];
for(i=0;i<n;i++)
{
if ( a[i] > largest )
largest = a[i];
}
printf("\n The largest Element in array is: %d", largest);
}
B.COM II SEM C&C++ Lab Programs Page 34
33. Write a C Program to Add Two Matrices Using Multi-dimensional
Arrays.
Program:-
#include<stdio.h>
void main()
{
int i,j,r1,c1,a[10][10],b[10][10];
printf("Enter order of matrix A&B upto 10×10:\n");
scanf("%d %d",&r1,&c1);
printf("Enter elements of matrix of A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of matrix of B:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d", &b[i][j]);
}
}
printf("\n matrix addition \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%3d",a[i][j]+b[i][j]);
}
printf("\n");
B.COM II SEM C&C++ Lab Programs Page 35
}
}
(34)
Aim:- Write a C Program to Find the Length of a String.
Program:-
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30]; .
printf("Enter the string for finding length\n");
gets(str1);
printf("string length=%d\n",strlen(str1));
}
B.COM II SEM C&C++ Lab Programs Page 36
(35)
Aim:-WriteaCProgramtoConcatenateTwoStrings
Program:-
#include<stdio.h>
#include<string.h>
void main()
{
char S1[30],S2[30];
printf("Enter two Sings for concatenation\n");
gets(S1);
gets(S2);
strcat(S1,S2);
puts(S1);
B.COM II SEM C&C++ Lab Programs Page 37
(36)
Aim:-Write a C Program to Copy String without using strcpy().
Program:-
#include <stdio.h>
void main()
{
char s1[100], s2[100], i;
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);
}
B.COM II SEM C&C++ Lab Programs Page 38
(37)
Aim:-Write a C Program to Count the Number of Vowels, Consonants
and so on.
Program:-
#include <stdio.h>
void main()
{
char line[150];
int vowels, consonant, digit, space;
int vowels = consonant = digit = space = 0; .
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
for (int i = 0; line[i] != '\0'; ++i)
{
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;
}
elseif((line[i]>='a'&&line[i]<='z')||(line[i]>='A'&&line[i]<='Z'))
{
++consonant;
}
elseif(line[i]>='0'&&line[i]<='9')
{
++digit;
}
elseif(line[i]=='')
{
++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space); 1
B.COM II SEM C&C++ Lab Programs Page 39
}
(38)
Aim:-Write a C Program to Find the Frequency of Characters in a String.
Program:-
#include <stdio.h>
void main()
{
char str[20],ch;
int count = 0; .
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for(inti=0;str[i]!='\0';++i)
{
if(ch==str[i])
++count;
}
printf("Frequency of %c = %d", ch, freq); 1
}
B.COM II SEM C&C++ Lab Programs Page 40
(39)
Aim:-Write a C Program to Access Array Elements Using
Pointers.
Program:-
#include <stdio.h>
void main()
{
int data[5];
printf("Enter elements: ");
for (int i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: \n");
for (int i = 0; i < 5; ++i)
printf("%d\n",*(data+i));
B.COM II SEM C&C++ Lab Programs Page 41
(40)
Aim:-Write a C program to create, initialize, assign and access a pointer
variable.
Program:-
#include <stdio.h>
void main()
{
int num;
int *pNum;
pNum=#
num=100; .
printf("Using variable num:\n");
printf("value of num: %d\n address of num: %u\n", num, &num);
printf("Using pointer variable:\n");
printf("value of num: %d\n address of num: %u\n",*pNum, pNum);
}
B.COM II SEM C&C++ Lab Programs Page 42
(41)
Aim:-Write a C program to swap two numbers using pointers
Program:-
#include <stdio.h>
void swap(int *x, int *y)
{
int t;
t =*x;
*x =*y;
*y =t;
}
void main()
{
int n1,n2;
printf("Enter value of n1: ");
scanf("%d",&n1);
printf("Enter value of n2: ");
scanf("%d",&n2);
printf("Before Swapping: n1 is: %d, n2 is: %d\n",n1,n2);
swap(&n1,&n2);
printf("After Swapping: n1 is: %d, n2 is: %d\n",n1,n2);
}
B.COM II SEM C&C++ Lab Programs Page 43
(42)
Aim:-Write a C program to count vowels and consonants in a string using
pointers.
Program:-
#include <stdio.h>
void main()
{
char str[100];
char *p;
int vCount=0,cCount=0; .
printf("Enter any string: ");
fgets(str,100, stdin);
p=str;
while(*p!='\0')
{
if(*p=='A'||*p=='E'||*p=='I'||*p=='O'||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCount++;
else
cCount++;
p++;
}
printf("Number of Vowels in String: %d\n", vCount);
printf("Number of Consonants in String: %d", cCount);
}
B.COM II SEM C&C++ Lab Programs Page 44
(44)
Aim:- Write a C Program to Add Two Distances (in inch-feet system)using
Structures.
Program:-
#include<stdio.h>
struct Distance
{
int feet;
float inch;
} d1, d2, sumOfDistances;
void main()
{
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
sumOfDistances.feet=d1.feet+d2.feet;
sumOfDistances.inch=d1.inch+d2.inch;
if (sumOfDistances.inch>12.0)
{
sumOfDistances.inch=sumOfDistances.inch-12.0;
++sumOfDistances.feet;
}
printf("\nSum of distances = %d\'-%.1f\"", sumOfDistances.feet,
sumOfDistances.inch);
B.COM II SEM C&C++ Lab Programs Page 45
(45)
Aim:-Write a C Program to Store Information of Students Using Structure.
Program:-
#include<stdio.h>
void main ()
{
struct student
{
int sid;
char sname[20];
float fees;
}s1;
printf("Enter sid,sname,fees\n");
scanf("%d%s%f",&s1.sid,&s1.sname,&s1.fees);printf("\nsid=%d\nsname=%s\
nfees=%f\n",s1.sid,s1.sname,s1.fees);
}
B.COM II SEM C&C++ Lab Programs Page 46
(46)
Aim:-Write a C program to declare, initialize a union.
Program:-
#include<stdio.h>
void main ()
{
union emp
{
int eid;
char ename[20];
float esalary;
}e1;
printf("Enter eid, ename, esalary\n");
scanf("%d %s %f", &e1.eid, &e1.ename, &e1.esalary);
printf("\n eid=%d \n ename=%s \n esalary=%f \n",e1.eid,e1.ename,e1.esalary);
}
B.COM II SEM C&C++ Lab Programs Page 47
(47)
Aim:-Write a C++ program to implement function overloading.
Program:-
#include <iostream.h>
1int area(int);
int area(int,int);
floatarea(float);
floatarea(float,float);
void main()
{
int s,l,b;
float r,b,h; .
cout<<"Entersideofasquare:";
cin>>s;
cout<<"Enter length and breadth of rectangle: ";
cin>>l>>b;
cout<<"Enter radius of circle: ";
cin>>r;
cout<<"Enter base and height of triangle: ";
cin>>b>>h;
cout<<"Area of square is "<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(b,h);
}
intarea(ints)
{
return(s*s);
}
intarea(intl,intb)
{
return(l*b);
B.COM II SEM C&C++ Lab Programs Page 48
}
floatarea(floatr)
{
return(3.14*r*r);
}
floatarea(floatb,floath)
{
return((b*h)/2);
}
B.COM II SEM C&C++ Lab Programs Page 49
48. Write a C++ program to calculate an area of rectangle using
encapsulation.
Program:-
#include <iostream.h>
class Rectangle
{
int x,y; public:
void set_values(int,int);
int area (void)
return(x*y);
};
Void Rectangle::set_values(inta,intb)
x=a;
y=b;
void main()
{
Rectangle rect;
rect.set_values(3,4);
cout <<"Area is : "<< rect.area();
B.COM II SEM C&C++ Lab Programs Page 50
(49)
Aim:-Write a C++ program to add two numbers using data abstraction.
Program:-
#include <iostream.h>
#include <conio.h>
void main()
{
int n1, n2, Sum; .
cout <<"Enter two integers: ";
cin >> n1 >> n2;
Sum=n1+n2;
cout <<n1<<" +"<<n2<<" ="<<Sum;
}
(50)
B.COM II SEM C&C++ Lab Programs Page 51
Aim:-Write a C++ program to overload binary operators
Program:-
#include <iostream.h>
#include <conio.h>
Class Complex
{
int n1, n2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers:";
cin>>n1>>n2;
}
Complex operator+(Complex obj)
{
Complex c;
c.n1=n1+obj.n1;
c.n2=n2+obj.n2;
return(c);
}
void display()
{
cout<<n1<<"+"<<n2<<"i"<<"\n";
}
};
void main()
{
Complex c1, c2, sum;
c1.accept();
c2.accept();
sum=c1+c2;
cout<<"\n Entered Values:"<<endl;
cout<<"\t";
c1.display();
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers: "<<endl;
cout<<"\t";
sum.display();
}
B.COM II SEM C&C++ Lab Programs Page 52