OU Site

Saturday, 9 March 2019

C & C++ LAB PROGRAMS

C-LAB PROGRAMS



1. Program for Arithmetic operator
 #include<stdio.h>
  int main(  ) 
{
 int a = 9,b = 4, c;
 c = a+b;
 printf("a+b = %d \n",c); 
 c = a-b; printf("a-b = %d \n",c); 
 c = a*b; printf("a*b = %d \n",c); 
 c=a/b; printf("a/b = %d \n",c); 
 c=a%b; printf("Remainder when a divided by b = %d \n",c); return 0; 

2. Program for Armstrong Number 
 
#include<stdio.h>
int main(  )
{
 int num,r,sum=0,
temp; printf("Enter a number: ");
 scanf("%d",&num); 
 temp=num; 
 while(num!=0)
{
 r=num%10; 
 num=num/10; 
 sum=sum+(r*r*r);
 } if(sum==temp)
 printf("%d is an Armstrong number",temp);
 else printf("%d is not an Armstrong number",temp); 
 return 0;
 } 

3. Program For Largest and smallest element in an array 
 #include<stdio.h>
int main(  )
{ int a[50],size,i,big,small; 
 printf("\nEnter the size of the array: ");
 scanf("%d",&size); 
 printf("\nEnter %d elements in to the array: ", size); 
 for(i=0;ia[i]) small=a[i]; 
 }
 printf("Smallest element: %d",small); 
 return 0; 

4. Write a C program to find the Square root of a given number 

 #include<stdio.h>
  #include<math.h>
int main(  ) 
 double num, root;
 /* Input a number from user */
 printf("Enter any number to find square root: ");
 scanf("%lf", &num);
 /* Calculate square root of num */
 root = sqrt(num); 
 /* Print the resultant value */
 printf("Square root of %.2lf = %.2lf", num, root);
 return 0; 
}

5. Program for call by value 

#include<stdio.h>
void swapnum( int var1, int var2 ) 
{
 int tempnum ;
 /*Copying var1 value into temporary variable */ 
 tempnum = var1 ; 
 /* Copying var2 value into var1*/
 var1 = var2 ;
 /*Copying temporary variable value into var2 */ 
 var2 = tempnum ; 
}
 int main( ) 
 int num1 = 35, num2 = 45 ; 
 printf("Before swapping: %d, %d", num1, num2); 
 /*calling swap function*/ 
 swapnum(num1, num2); 
 printf("\nAfter swapping: %d, %d", num1, num2);
 } 

6. Write a C program to find the area and circumference of a circle 

#include<stdio.h>
int main(  ) 
{
 int rad; 
 float PI = 3.14, area, ci; 
 printf("\nEnter radius of circle: ");
 scanf("%d", &rad);
 area = PI * rad * rad;
 printf("\nArea of circle : %f ", area);
 ci = 2 * PI * rad; 
 printf("\nCircumference : %f ", ci); 
 return (0); 

7.. Write a C program to demonstrate swapping of two numbers
 
#include<stdio.h>
  int main( ) 
 int x, y, t;
 printf("Enter two integers\n");
 scanf("%d%d", &x, &y); 
 printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
 t = x; x = y; y = t; 
 printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y); 
 return 0; 
}

8. Write Program to demonstrate switch.

 #include<stdio.h> 
#include<conio.h> 

int main (  ) 
 char grade = 'B';
 switch(grade) 
 { 
 case 'A' : printf("Excellent!\n" ); 
 break; 
 case 'B' :
 printf("Well done\n" );
 break; 
 case 'C' :
 printf("Good\n" ); 
 break; 
 case 'D' :
 printf("You passed\n" ); 
 break; 
 case 'F' :
 printf("Better try again\n" );
 break;
 default : 
printf("Invalid grade\n" ); 
 }
 printf("Your grade is %c\n", grade ); 
 return 0; 
}

9.Write a C program to determine given number is Armstrong number or not. (Sum of cubes = that number) 

 #include<stdio.h> 
 int main(  )
{
 int num,r,sum=0,
temp; 
printf("Enter a number: ");
 scanf("%d",&num); 
temp=num; 
 while(num!=0)
{
 r=num%10; 
 num=num/10; 
 sum=sum+(r*r*r); 
 } 
 if(sum==temp) 
 printf("%d is an Armstrong number",temp); 
 else 
 printf("%d is not an Armstrong number",temp);
 return 0;
 }

10. Program for call by reference

 #include<stdio.h>
 void swapnum ( int *var1, int *var2 )
 { 
 int tempnum ;
 tempnum = *var1 ;
 *var1 = *var2 ; 
 *var2 = tempnum ; 
}
int main(  ) 
{
 int num1 = 35, num2 = 45 ;
 printf("Before swapping:"); 
 printf("\nnum1 value is %d", num1);
 printf("\nnum2 value is %d", num2);
 /*calling swap function*/ 
 swapnum( &num1, &num2 ); 
 printf("\nAfter swapping:"); 
 printf("\nnum1 value is %d", num1);
 printf("\nnum2 value is %d", num2);
 return 0;
 } 


11. Matrix Addition

 #include<stdio.h>
void main(  )
 int m, n, c, d, first[10][10], second[10][10], sum[10][10];
 printf("Enter the number of rows and columns of matrix\n");
 scanf("%d%d", &m, &n); 
 printf("Enter the elements of first matrix\n");
 for (c = 0; c < m; c++) for (d = 0; d < n; d++) 
 scanf("%d", &first[c][d]); 
 printf("Enter the elements of second matrix\n")
 for (c = 0; c < m; c++)
 for (d = 0 ; d < n; d++) 
 scanf("%d", &second[c][d]);
 printf("Sum of entered matrices:-\n"); 
 for (c = 0; c < m; c++)
 { 
 for (d = 0 ; d < n; d++)
 {
 sum[c][d] = first[c][d] + second[c][d];
 printf("%d\t", sum[c][d]); 
 } 
 printf("\n");
 } 

12. Call by reference #include

#include
void swap(int * , int * );
 // function prototype or function declaration
 int main(  ) 
 int a = 10, b = 20; 
 clrscr( ); 
 printf(" values before swap a = %d \n and b = %d", a, b);
 swap(&a, &b); getch( ); 
 }
 void swap(int * x, int * y) 
 int temp = *x; * x = *y; *y= temp; 
 printf(" \nvalues after swap x= %d\n and y = %d", *x, *y);
}

_______________________________________________________________________

C++ LAB PROGRAM


1.       Print the sum of digits of a given number
     #include<iostream.h>
void main()
{
    int val, num, sum = 0;
     cout << "Enter the number : ";
    cin >> val;
    num = val;
    while (num != 0)
    {
        sum = sum + num % 10;
       num = num / 10;
    }

    cout << "The sum of the digits of " << val << " is " << sum;
}

2.       Check whether the given number is Armstrong or not
#include <iostream>
void  main() {
   int num, sum = 0, digit;
   cout<<"Enter a positive integer: ";
   cin>>num;

   for(int temp=num; temp!=0;){
      digit = temp % 10;
      sum = sum +(digit * digit * digit);
      temp = temp/10;
   }

   if(sum == num)
      cout<<num<<" is an Armstrong number.";
   else
      cout<<num<<" is not an Armstrong number.";

   }

3.       Write a program to find largest and smallest elements in a given list of numbers and sort the
given list.



  #include<iostream.h>
       int main ()
    {
        int arr[10], n, i, max, min;
        cout << "Enter the size of the array : ";
        cin >> n;
        cout << "Enter the elements of the array : ";
        for (i = 0; i < n; i++)
            cin >> arr[i];
        max = arr[0];
        for (i = 0; i < n; i++)
        {
            if (max < arr[i])
                max = arr[i];
        }
        min = arr[0];
        for (i = 0; i < n; i++)
        {
            if (min > arr[i])
                min = arr[i];
        }
        cout << "Largest element : " << max;
        cout << "Smallest element : " << min;
        return 0;
    }


4..Write a program to read the student name, roll no, marks and display the same using class
and object.


#include<iostream.h>
class student
{
                private:
                               
                                                char name[20],regd[10],branch[10];
                                                int sem;
                public:
                                void input();
                                void display();
                                               
                               
};
void student::input()
{
                cout<<"Enter Name:";
                cin>>name;
                cout<<"Enter Regdno.:";
                cin>>regd;
                cout<<"Enter Branch:";
                cin>>branch;
                cout<<"Enter Sem:";
                cin>>sem;
}
void student::display()
{
                cout<<"\nName:"<<name;
                cout<<"\nRegdno.:"<<regd;
                cout<<"\nBranch:"<<branch;
                cout<<"\nSem:"<<sem;
}
int main()
{
                student s;
                s.input();
                s.display();
}


5. Write a program to find area of a rectangle, circle, and square using constructors.


    #include<iostream.h>
    class Area
    {
private:
    int a,b;
    float c;
    public:
    void triangle()
    {
cout <<"area of triangle is" ;
    c=0.5*a*b;
    cout<<c <<endl;
    }
    void rectangle()
    {
cout<<"area of rectangle is  "<<a*b<<endl;
    }
    void circle()
    {cout<<"area of circle is  ";
    c=3.14*a*a;
    cout<<c <<endl;
    }
    Area(int  x,int y)
    {cout<<"constructor called "<<endl;
    a=x;
    b=y;
    }
    Area(int x)
    {
cout<<"constructor called "<<endl;
    a=x;
    }
    };
    int main()
    {Area obj(4,6);//1st call implicit call
    Area obj1=Area(2,3);//2nd call Explicit
    Area obj2=8;//if one argument can pass  argument directly
    obj.triangle();
    obj1.rectangle();
    obj2.circle();
    return 0;
    }



6. Construtor overloading
#include <iostream>

class Area
{
    private:
       int length;
       int breadth;

    public:
       // Constructor with no arguments
       Area(): length(5), breadth(2) { }

       // Constructor with two arguments
       Area(int l, int b): length(l), breadth(b){ }

       void GetLength()
       {
           cout << "Enter length and breadth respectively: ";
           cin >> length >> breadth;
       }

       int AreaCalculation() {  return length * breadth;  }

       void DisplayArea(int temp)
       {
           cout << "Area: " << temp << endl;
       }
};

int main()
{
    Area A1, A2(2, 1);
    int temp;

    cout << "Default Area when no argument is passed." << endl;
    temp = A1.AreaCalculation();
    A1.DisplayArea(temp);

    cout << "Area when (2,1) is passed as argument." << endl;
    temp = A2.AreaCalculation();
    A2.DisplayArea(temp);

    return 0;
}

7. Operator overloading
#include<iostream.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-();
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout<<"="<<x<<"";
cout<<"y="<<y<<"";
cout<<"z="<<z<<"\n";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
space s;
s.getdata(10,-20,30);
cout<<"s";
s.display();
-s;
cout<<"-s";
s.display();
return 0;
}

8. Overloading  binary operator

#include<iostream.h>
class complex

{
float x;
float y;
public:
complex()
{
}
complex(float real,float imag)
{
x=real;

y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.y;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<y"+j"<<y<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1"=";
cout<<"c2=";
cout<<"c3=";
return 0;
}

 Type converison
#include<iostream.h>
class time
{
private:
int h,m;
public:
time()
{
h=m=0;
}
time(int t)
{
h=t/60;
m=t%60;
}
void get_data()
{
cin>>h>>m;
}
void show_data()
{
cout<<h<<"hrs"<<m<<"mins";
}
operator int()
{
int t=h*60+m;
return t;
}
};
void main()
{
int min;
cout<<"enter the minutes:";
cin>>min;
time t1;
t1.show_data();
cout<<"\n enter the number of hours and minutes:";
t1.get_data();
min=t1;
cout<<"\n total minutes="<<min;
}


class to class conversion:
#include<iostream.h>
class rectangle;
class square
private:
int side;
public:
square(int s)
{
side=s;
int get_side()
{
return side;
}
};
class rectangle
{
private:
int length, breadth;
public:
rectangle(int l,int b)
{
length=l;
breadth=b;
}
rectangle(square s)
{
int sqr_side=s.get_side();
length=breadth=sqr_side;
}
void show_data()
{
cout<<"\n length="<<length<<"and breadth="<<breadth;
}
};
void main()
{
rectanlge r(10,20);
r.show_data();
square s(50);
r=s;
r.show_data();
}


Single Inheritance:

#include <iostream.h>
Class B
{
 int a;
 public:
 int b;
void get_ab();
int get_a();
void show_a();
};
Class D: public B
{
int c;
public:
void mul();
void display();
};
Void B :: get_ab()
{ a=5;b=10; }
Int B :: get_a()
{ return a;}
Void B :: show_a()
{ count<< “a=”<<a<< “\n" ;
}
Void D :: mul()
{ c=b*get_a();}
Void D :: display()
{
Count<< “a=”<<get_a();
Count<< “b=”<<b;
Count<< “c=”<<c;
}
int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();

return 0;
}


































































































No comments:

Post a Comment