0% found this document useful (0 votes)
4 views

C Programming

The document contains a series of C and C++ programming examples, including programs for finding the greatest of three numbers, generating multiplication tables, creating pyramids, and calculating salaries with various conditions. It also includes C++ concepts such as hierarchical inheritance, operator overloading, parameterized constructors, and polymorphism. Each program demonstrates fundamental programming concepts and syntax in both languages.

Uploaded by

SHAHID SHAIKH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Programming

The document contains a series of C and C++ programming examples, including programs for finding the greatest of three numbers, generating multiplication tables, creating pyramids, and calculating salaries with various conditions. It also includes C++ concepts such as hierarchical inheritance, operator overloading, parameterized constructors, and polymorphism. Each program demonstrates fundamental programming concepts and syntax in both languages.

Uploaded by

SHAHID SHAIKH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

C Programming

1. Ladder

Program :-
#include <stdio.h>
int main ()
{

int num1,num2,num3;
printf("Enter 3 numbers:\n");
scanf("%d%d%d",&num1,&num2,&num3);
if((num1>num2)&&(num1>num3))
{
printf("The greatest number is %d",num1);
}
if((num2>num1)&&(num2>num3))
{
printf("The greatest number is %d",num2);
}
if((num3>num1)&&(num3>num2))
{
printf("The greatest number is %d",num3);
}
return 0;}
2. Tables

Program :-
#include<stdio.h>
int main(){
int a,b,c;
printf("Enter table number\n");
scanf("%d",&b);
for(a=1;a<=10;a++)
{
c=a+b;
printf("%dX",b);
printf("%d=",a);
printf("%d\n",c);
}
return 0;
}
3. Pyramit

Program :-
#include<stdio.h>
int main()
{
int i,j,k,m;
for(i=1;i<=9;i++)
{
for(j=9;j>=i;j--)
{
printf(" ");
}
for(k=1;k<=i+j;k++)
{
printf("*");
}
for(m=9;m>=i;m--)
{
printf(" ");
}
printf("\n");
}
return 0;
}
4. Switch for weeks

Program :-
#include<stdio.h>
int main ()
{
char week;
printf("Enter week number\n");
scanf("%d",&week);
switch(week)
{
case 1:
printf("Sunday\n");
break;

case 2:
printf("Monday\n");
break;

case 3:
printf("Tuesday\n");
break;

case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;

case 6:
printf("Friday\n");
break;

case 7:
printf("Saturday\n");
break;
default:
printf("Invalid\n");
}
return 0;
}
5. Salary if else

Program :-
#include<stdio.h>
int main(){
float basic,da,hra,salary;
char d[15];
printf("Enter Your Name:\n");
scanf("%s",&d);
printf("enter the basic salary:\n");
scanf("%f",&basic);
da=basic*40/100;
hra=basic*25/100;
salary=basic+da+hra;
printf("your name is:%c\n",d);
printf("salary details:\n");
printf("Basic salary is:%f\n",basic);
printf("Dearness Allowance is:%f\n",da);
printf("House Rent Allowance is:%f\n",hra);
printf("Total salary earned %f\n",salary);
if(salary>=80000)
{
printf("\n You are ceo ");
}
else if(salary>=50000)
{
printf("\n You are purchase manager");
}
else if(salary>=25000)
{
printf("\n you are sales manager");
}
else
{
printf("\n you are clerk");
}
return 0;
}
6. Functions

Program :-
#include<stdio.h>
int triangle();
int rectangle();
int circle();
int main()
{
triangle();
rectangle();
circle();
return 0;
}
int triangle()
{
int area,b,h;
printf("Enter two number\n");
scanf("%d%d",&b,&h);
area=0.5*b*h;
printf("area of triangle is %d\n",area);
return 0;
}

int rectangle()
{
int area,l,b;
printf("Enter two number\n");
scanf("%d%d",&l,&b);
area=l*b;
printf("area of rectangle is %d\n",area);
return 0;
}

int circle()
{
int area,r;
printf("Enter the number\n");
scanf("%d",&r);
area=3.14*r*r;
printf("area of circle is %d\n",area);
return 0;
}
C++
1. Salary

Program :-
#include<iostream>
using namespace std;
class Salary{
int basic , hra , ta , gross , pf , tax , n ;
public :
int input(){
cout<<"\nEnter Basic Salary";
cin>>basic;
}
int process(){
gross=basic+hra+ta;
hra=basic*0.10;
pf=basic*0.010;
tax=basic*0.05;
ta=basic*0.010;
n=pf+ta-gross;
}
int output(){
cout<<"\nHRA = "<<hra;
cout<<"\nTravelling Allowence = "<<ta;
cout<<"\nGross Salary = "<<gross;
cout<<"\nPF = "<<pf;
cout<<"\nTax Deducation = "<<tax;
cout<<"\nNet Salary = "<<n;
}
};
int main(){
Salary obj;
obj.input();
obj.process();
obj.output();
return 0;
}
2. Hierarchical Inheritence

Program :-
#include<iostream>
using namespace std;
class base {
public:
int x;
int disp(){
cout<<"\nBASE CLASS\n";
}
};
class d1 : public base {
public:
int disp2(){
x=50;
cout<<"DERIVED CLASS 1 = "<<x;
}
};
class d2 : public base {
public:
int show(){
cout<<"DERIVED CLASS 2\n";
}
};
int main(){
d1 obj1;
obj1.disp();
obj1.disp2();

d2 obj2;
obj2.disp();
obj2.show();
return 0;
}
3. Operator overloading

Program :-
#include<iostream>
using namespace std;
class Box
{
public:
double getVolume(int)
{
return length*breadth*height;
}
int setLength(double len)
{
length=len;
}
int setBreadth(double bre)
{
breadth=bre;
}
int setHeight(double hei)
{
height=hei;
}
Box operator+(const Box&b)
{
Box box;
box.length=this->length+b.length;
box.breadth=this->breadth+b.breadth;
box.height=this->height+b.height;
return box;
}
private:
double length;
double breadth;
double height;
};
int main()
{
Box Box1;
Box Box2;
Box Box3;
double volume=0.0;

Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
volume=Box1.getVolume(10.0);
cout<<"VolumeofBox1:"<<volume<<endl;
volume=Box2.getVolume(10.0);
cout<<"VolumeofBox2:"<<volume<<endl;
Box3=Box1+Box2;
volume=Box3.getVolume(10.0);
cout<<"VolumeofBox3:"<<volume<<endl;
return 0;
}
4. Parameterized Constructor

Program :-
#include<iostream>
#include<string.h>
using namespace std;

class student{

public:
student
(char name[50] , int age , int rol , char clas[50])
{
cout<<"\nR.NO.\tAge\tName\tClass";
cout<<"\n"<<rol<<"\t"<<age<<"\t"<<name<<"\t"<<clas;
}
};
int main(){
char name[50] , clas[50] ;
int age , rol ;
cout<<"\nStudent name , age , roll number and class\n";
cin>>name>>age>>rol>>clas;
class student s(name,age,rol,clas);
return 0;
}
5. Polymorphism

Program :-
#include<iostream>
using namespace std;
class CPolygon
{
protected:
int width,height;
public:
int set_values(int a,int b)
{
width=a;
height=b;
}
};
class CRectangle:public CPolygon
{
public:
int area()
{
return(width*height);
}
};
class CTriangle:public CPolygon
{
public:
int area()
{
return(width*height/ 2);
}
};
int main()
{
CRectangle rect;
CTriangle trgl;
CPolygon*ppoly1=&rect;
CPolygon*ppoly2=&trgl;
ppoly1->set_values(4,5);
ppoly2->set_values(4,5);
cout<<rect.area()<<endl;
cout<<trgl.area()<<endl;
return 0;
}
6. Class Shopping

Program :-
#include<iostream>
using namespace std;
class shoping
{
int c,p;
static int s;
char name[150];
public:
int input();
int process();
int output();
};
int shoping::s;
int shoping::input()
{
cout<<"\nEnter Item Name=";
cin>>name;
cout<<"\nEnter Item Code=";
cin>>c;
}
int shoping::process()
{
if(c<=25)
{
p=100;
s=s+p;
cout<<"\nPriceIs "<<p;
}
else if(c<=50)
{
p=200;
s=s+p;
cout<<"\nPriceIs "<<p;
}
else if(c<=75)
{
p=300;
s=s+p;
cout<<"\nPriceIs "<<p;
}
else if(c<=100)
{
p=400;
s=s+p;
cout<<"\nPriceIs "<<p;
}
else if(c>=100)
{
cout<<"\nWrongCode";
}
}
int shoping::output()
{
cout<<"\nYour Item Name is : "<<name;
cout<<"\nYour Codeis:"<<c;
cout<<"\nPlz Pay:"<<p;
cout<<"\nTotal Amount Is="<<s;
}
int main()
{
int i;
shoping sobj[4];
for(i=0;i<=3;i++)
{
cout<<"\n--------------*-----------";
cout<<"\nDetail Of Purchased Material"<<i+1;
sobj[i].input();
}
cout<<"\nDetail Of Purchased";
for(i=0;i<=3;i++)
{
cout<<"\n--------------*-----------";
cout<<"\nPurechased"<<i+1;
sobj[i].process();
}
for(i=0;i<=3;i++)
{
cout<<"\n--------------*-----------";
cout<<"\nPurechased"<<i+1;
sobj[i].output();
}
return 0;
}

You might also like