0% found this document useful (0 votes)
13 views17 pages

C++ Basic Programming Examples

The document contains multiple C++ programs that demonstrate basic programming concepts such as arithmetic operations, control structures, functions, and data types. Each program performs specific tasks like calculating sums, factorials, and finding minimum/maximum values, as well as demonstrating the use of pointers and references. Overall, it serves as a practical guide for beginners to understand fundamental programming techniques in C++.

Uploaded by

retor82533
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)
13 views17 pages

C++ Basic Programming Examples

The document contains multiple C++ programs that demonstrate basic programming concepts such as arithmetic operations, control structures, functions, and data types. Each program performs specific tasks like calculating sums, factorials, and finding minimum/maximum values, as well as demonstrating the use of pointers and references. Overall, it serves as a practical guide for beginners to understand fundamental programming techniques in C++.

Uploaded by

retor82533
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

#include<iostream.

h>

#include<conio.h>

void main()

clrscr();

int p, q, sum, diff, mul, quo, rem;

cout<<"Enter the first number : ";

cin>>p;

cout<<"Enter the second number : ";

cin>>q;

sum = p + q;

diff = p - q;

mul = p * q;

quo = p / q;

rem = p % q;

cout<<"Addition = "<<sum<<endl;

cout<<"Difference = "<<diff<<endl;

cout<<"Product = "<<mul<<endl;

cout<<"Quotient = "<<quo<<endl;

cout<<"Remainder = "<<rem<<endl;

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a = 0, b = 0, c = 0;

cout<<"a\t"<<"b\t"<<"c\t"<<endl;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

a = ++b + ++c;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

a = ++b + c++;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

a = --b + c--;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

a = b-- + c--;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

a = -a;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

cout<<"Bytes occupied by character datatype are : "<<sizeof(char)<<endl;

cout<<"Bytes occupied by integer datatype are : "<<sizeof(int)<<endl;

cout<<"Bytes occupied by float datatype are : "<<sizeof(float)<<endl;

cout<<"Bytes occupied by double datatype are : "<<sizeof(double)<<endl;

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a, b, c, min, max;

cout<<"Enter the three numbers to be compared : "<<endl;

cin>>a>>b>>c;

max = ((a > b)&&(a > c)) ? a : ((b > a)&&(b > c)) ? b : c;

min = ((a < b)&&(a < c)) ? a : ((b < a)&&(b < c)) ? b : c;

cout<<"The biggest number is : "<<max<<endl;

cout<<"The smallest number is : "<<min<<endl;

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n, i = 1, fact = 1;

cout<<"Enter the number for which you want to find the factorial : ";

cin>>n;

while (i<=n)

fact = fact * i;

i++;

cout<<"Factorial of a given number is : "<<fact;

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n, i = 0, sum = 0;

cout<<"Enter the number up to which you want to find the sum : ";

cin>>n;

do

sum = sum + i;

i++;

} while (i<=n);

cout<<"The sum of first "<<n<<" natural numbers is : "<<sum;

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

cout<<"The numbers not divisible by 9 between 0 to 100 are \n";

for (int i=0; i<=100; i++)

if (i % 9 != 0)

cout<<i<<" ";

else

continue;

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

float marks;

char ans;

start :

cout<<"Enter marks obtained out of 100 : ";

cin>>marks;

if (marks>=40)

goto pass;

else

cout<<"The student has failed.";

goto end;

pass : cout<<"The student has passed.";

end :

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n;

cout<<"Enter the number of the day of week : ";

cin>>n;

switch(n)

case 1 : cout<<"The day of the week is Monday."<<endl;

break;

case 2 : cout<<"The day of the week is Tuesday."<<endl;

break;

case 3 : cout<<"The day of the week is Wednesday."<<endl;

break;

case 4 : cout<<"The day of the week is Thursday."<<endl;

break;

case 5 : cout<<"The day of the week is Friday."<<endl;

break;

case 6 : cout<<"The day of the week is Saturday."<<endl;

break;

case 7 : cout<<"The day of the week is Sunday."<<endl;

break;

default : cout<<"Invalid input."<<endl;

cout<<"Press any key to exit.";

getch();

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a, b, c, min, max;

cout<<"Enter the three numbers to find minimum and maximum : ";

cin>>a>>b>>c;

if (a > b)

if (a > c)

max = a;

else

max = c;

else

if (b > c)

max = b;

else

max = c;

}
}

if (a < b)

if (a < c)

min = a;

else

min = c;

else

if (b < c)

min = b;

else

min = c;

cout<<"The greatest number is : "<<max<<endl;

cout<<"The smallest number is : "<<min<<endl;

getch();

}
#include<iostream.h>

#include<conio.h>

#include<math.h>

const float pi = 3.14;

void main()

clrscr();

float radius, area, volume;

void sphere (float&, float&, float&);

cout<<"Enter radius : "<<endl;

cin>>radius;

sphere (radius, area, volume);

cout<<"Area of sphere = "<<area<<endl;

cout<<"Volume of sphere = "<<volume;

getch();

void sphere (float&r, float&a, float&v)

a = (4 * pi * pow(r, 2) );

v = (4 * pi * pow(r, 3) ) / 3;

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a, b;

void swapv (int, int);

cout<<"Enter the values of a and b : "<<endl;

cin>>a>>b;

swapv (a, b);

cout<<"Values of a and b after swapping : "<<a<<"\t"<<b<<endl;

getch();

void swapv (int x, int y)

int temp;

cout<<"Values of x and y before swapping : "<<x<<"\t"<<y<<endl;

temp = x;

x = y;

y = temp;

cout<<"Values of x and y after swapping : "<<x<<"\t"<<y<<endl;

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a, b;

void swapr (int&, int&);

cout<<"Enter the values of a and b : "<<endl;

cin>>a>>b;

swapr (a, b);

cout<<"Values of a and b after swapping : "<<a<<"\t"<<b<<endl;

getch();

void swapr (int&x, int&y)

int temp;

cout<<"Values of x and y before swapping : "<<x<<"\t"<<y;

temp = x;

x = y;

y = temp;

cout<<"Values of x and y after swapping : "<<x<<"\t"<<y;

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a, b;

void swapp (int*, int*);

cout<<"Enter the values of a and b :"<<endl;

cin>>a>>b;

swapp (&a, &b);

cout<<"Values of a and b after swapping : "<<a<<"\t"<<b<<endl;

getch();

void swapp (int*x, int*y)

int temp;

cout<<"Values of x and y before swapping : "<<*x<<"\t"<<*y<<endl;

temp = *x;

*x = *y;
*y = temp;

cout<<"Values of x and y after swapping : "<<*x<<"\t"<<*y<<endl;

}
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int i = 0;

char ch;

cout<<"Enter the line of text : "<<endl;

[Link](ch);

while (ch != '\n')

[Link](ch);

i++;

[Link](ch);

cout<<"\nThe total number of characters are : "<<i;

getch();

}
#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

clrscr();

int n, i = 0, words = 1;

char ch[40];

cout<<"Enter the line of text : "<<endl;

[Link](ch,40);

n = strlen(ch);

while (i<=n)

If (ch[i]==' ')

words++;

i++;

cout<<"The line entered is : ";

[Link](ch,n)<<endl;

cout<<"The total number of words are : "<<words;

getch();

You might also like