BASIC C AND C++ PROGRAMS

C PROGRAMMING:
-         Basics knowledge of programming.
-         Programming languages.
-         Basic concepts of C language.

Programms in C:

1.) Program to describe the structure of C
#include<stdio.h>
#include<conio.h>
Void main( )
{
Int a,b,c;
Clrscr( );
Printf(“enter the values of a and b”);
Scanf(“%d”,&a,&b);
C=a+b;
Printf(“the result is %d”,c);
Getch();
}

Output:
Enter the value of a and b 4 5
The result is 9

2.) Program to find the greatest of two numbers using if statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Enter the number");
scanf("%d",&i);
if(i<10)
printf("\n The number is less than 10");
else
printf("The number is greater than 10");
getch();
}

3.) Program for swapping of two numbers using third variable
#inlcude<stdio.h>
#include<conio.h>
Void main()
{
Int a,b,c;
Clrscr();
Printf(“enter the values for a and b”);
Scanf(“%d%d”,&a,&b);
c=a;
a=b;
b=c;
printf(“after swapping the numbers are”, a,b);
getch();
}

4.)  Without using third variable
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a,b;
Clrscr();
Printf(“enter the values of a and b”);
Scanf(“%d%d”,&a,&b);
a=(b-a)+(b=a);
printf(“the swapped values are”,a,b);
getch();
}
5.) To differentiate the usage of increment and decrement operator.
#inlcude<stdio.h>
#include<conio.h>
{
Int a=1;
Printf(“the incremented value of a”,++a,a++,a);
Getch();
}
 Output:
The incremented value of a 3 3 1.

6.) Program to find the given year is leap or not
#include<stdio.h>
#inlcude<conio.h>
Void main()
{
Int a;
Printf(“enter the year of a”);
Scanf(“%d”,a);
If(a%4==0)
{
Printf(“the year is leap”);
}
Else
{
Printf(“non leap year”);
}
Getch();
}
7.) Printing pattern using for loop
#inlcude<stdio.h>
#include<conio.h>
Void main()
{
Int i,j,n;
Clrscr();
Printf(“enter tha value of n”);
Scanf(“%d”,n);
For(i=1;i<=n;i++)
{
For(j=1;j<=i;j++)
{
Printf(“%d”,j);
}
Printf(“\n”);
}
Getch();
}

Output:
Enter the value of n 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

8.) Printing star pattern

#inlcude<stdio.h>
#include<conio.h>
Void main()
{
Int i,j,n;
Clrscr();
Printf(“enter tha value of n”);
Scanf(“%d”,n);
For(i=1;i<=n;i++)
{
For(j=1;j<=i;j++)
{
Printf(“*”);
}
Printf(“\n”);
}
Getch();
}
Output:
Enter the value of n 4
*
* *
* * *
* * * *

9.) Printing the variables.
#include<conio.h>
Void main()
{
Int i,j;
Char a[ ]={‘a’,’b’,’c’,’d’,’e’,’f’};
Clrscr();
Printf(“enter tha value of n”);
Scanf(“%d”,n);
For(i=0;i<=5;i++)
{
For(j=0;j<=i;j++)
{
Printf(“%d”,a[j]);
}
Printf(“\n”);
}
Getch();
}

Output:
a
a b
a b c d
a b c d e

10.)                   Program to find the factorial of given number
#inlcude<stdio.h>
#include<conio.h>
Void main()
{
Int i, fact=1,n;
Clrscr();
Printf(“enter the number”);
Scanf(“%d”,i);
For(i=1;i<=n;i++)
{
Fact=fact*i;
}
Printf(“factorial of given numb is: %d”,n,fact);
Getch();
}
Output:
          Enter the number: 5
          The factorial of given number is:120
11.)                   Program for AND operator :
                                #include<stdio.h>
                                #include<conio.h>
                                main()
                                {
                                 int a=1,b=0;
                                 clrscr();
                                 if(a&&b)
                                 {
                                  printf("condition is true");
                                 }
                                    else
                                  {
                                   printf("condition is false");
                                   }
           :                    getch();
                                  }
Output”
If a=0, b=0 the condition is false
If a=1, b=1 the condition is true
If a=0, b=1 the condition is false
If a=1, b=0 the condition is false
12.)                   Call by value
                                 #include<stdio.h>
                                #include<conio.h>
                                void add(int x)
                                {
                                   x+=10;
                             printf("the value of x”,x);
                              }
                            int main()
                           {
                            int y=10;
                              clrscr();
                               add(y);
                                printf("the value of y",y);
                                  getch();
                           return 0;
                         }
Output:
The value of y 10
The value of y 10
13.)                   Printing details using structure and union.
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[10];
}s;
union student
{
int rollno;
char name[10];
}u;
void main()
{
clrscr();
printf("size of structure is %d",sizeof(s));
printf("size of union is %d",sizeof(u));

}


PROGRAMMS ON C++

1.)  Program to find the divisibility.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=0;i<=100;i++)
{
if((i%3==0||(i%5==0))
{
cout<<"\t"<<i;
}
}
getch();
}



2.)  Programs on constructor and destructor
 #include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
int marks;
public:
student()
{
rollno=0;
marks=0;
}
student(int a,int b)
{
rollno=a;
marks=b;
cout<<endl<<"Parameterized constructor"<<endl<<"rollno"<<rollno<<endl<<"marks"<<marks;
}
student(student &t)
{
rollno=t.rollno;
marks=t.marks;
cout<<"Copy constructor"<<endl<<"rollno"<<rollno<<endl<<"marks"<<marks<<endl;
}
~student()
{
cout<<"the constructor destroys";
}
};
void main()
{
student s1();
student s2(4,5);
student s3(s2);
getch();
}
Output:
Parameterized constructor
Rollno 29
Marks 90
Copy constructor
Rollno 29
Marks 90
The constructor destroyed
3.)  Single inheritance

#include<iostream.h>
#include<conio.h>
class shape
{
public:
int w,h;
void setvalue(int a ,int b)
{
w=a;
h=b;
}
};
class rect:public shape
{
public:
void display()
{
cout<<w<<endl<<h<<endl;
}
};
void main()
{
clrscr();
rect r;
r.setvalue(5,6);
r.display();
getch();
}

4.)  Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void display1()
{
cout<<"a"<<endl;
}
};
class B
{
public:
void display2()
{
cout<<"b"<<endl;
}
};
class C: public B,public A
{
public:
void display3()
{
cout<<"c"<<endl;
}
};
void main()
{
clrscr();
C ca;
ca.display1();
ca.display2();
ca.display3();
getch();
}
5.)  Polymorphism
#include<iostream.h>
#include<conio.h>
class A
{
private:
int a,b,c;
public:
void getdata()
{
cout<<"Enter the 2 values:";
cin>>a>>b;
c=a+b;
}
void getdata(int x,int y)
{
a=x;
b=y;
c=a+b;
}
void showdata()
{
cout<<endl<<"Two numbers "<<a<<endl<<endl<<b<<endl;
cout<<endl<<"sum"<<c<<endl;
}
};
void main()
{
A aa,ab;
clrscr();
aa.getdata();
aa.showdata();
ab.getdata(5,10);
ab.showdata();
getch();
}

6.) Operator overloading
#include<iostream.h>
#include<conio.h>

class complex
{
     int a,b,c;
    public:
        complex(){}
        void getvalue()
       {
                 cout<<"Enter the Two Numbers:";
                 cin>>a>>b;
       }
            
   

  void operator++()
      {
                 a=++a;
                 b=++b;
       }
            
       void operator--()
       {
                 a=--a;
                 b=--b;
        }
            
        void display()
        {
                 cout<<a<<"+\t"<<b<<"i"<<endl;
         }
};

void main()
{
     clrscr();
     complex obj;
     obj.getvalue();
     obj++;
     cout<<"Increment Complex Number\n";
     obj.display();
     obj--;
     cout<<"Decrement Complex Number\n";
     obj.display();
     getch();
}
7.) No arguement no return value
#include<iostream.h>
#include<conio.h>
Void message();
Void main()
{
Clrscr();
Cout<<”welcome”;
Message();
Cout<<endl<<”end”;
Getch();
}
Void message()
{
Cout<<endl<<”inside the function”;
}
8.)  Type casting
#include<iostream.h>
#include<conio.h>
void main()
{
int a=10,b;
float c=2.3;
clrscr();
b=int(a+c);
Cout<<"The value of b is <<b;
     getch();

}

0 comments: