0% found this document useful (0 votes)
144 views14 pages

CS-1 (C++ Programs For Practice)

The document contains code snippets demonstrating various C++ programming concepts like constants, arithmetic operators, conditional statements, loops, functions, arrays etc. It includes 14 code examples showing concepts like basic input/output, constants using const and define, character constants, operators, conditional statements like if-else, switch, goto, loops like for, nested loops, function calls, arrays etc. Each code example is independent and focuses on demonstrating a specific programming concept.

Uploaded by

Aarya Shisode
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)
144 views14 pages

CS-1 (C++ Programs For Practice)

The document contains code snippets demonstrating various C++ programming concepts like constants, arithmetic operators, conditional statements, loops, functions, arrays etc. It includes 14 code examples showing concepts like basic input/output, constants using const and define, character constants, operators, conditional statements like if-else, switch, goto, loops like for, nested loops, function calls, arrays etc. Each code example is independent and focuses on demonstrating a specific programming concept.

Uploaded by

Aarya Shisode
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/ 14

Print Hello world

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout <<"Hello World\n";
getch();
}

Program for Constants(using const keyword)


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const float pie=3.14;
float r,area,circum;
cout<<"Enter radius:\t";
cin>>r;
area=pie*r*r;
cout<<"Area of a circle is:\t"<<area<<endl;
circum=2*pie*r;
cout<<"Circumference of a circle is:\t"<<circum<<endl;
getch();
}

Program for Constants(using define)


#include<iostream.h>
#include<conio.h>
#define pi 3.14
void main()
{
clrscr();
float a, r=5;
a=pi*r*r;
cout<<"area of circle: "<<a;
getch();
}

Character Constant
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout <<"Hello World\n";
cout<<"This is CS class\t";
cout<<"Welcome";
getch();
}

Arithmetic Operators
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n1,n2,result;
cout<<"Enter the 1st Number:\t";
cin>>n1;
cout<<"\nEnter the 2nd Number:\t";
cin>>n2;
result=n1+n2;
cout<<"Addition of two numbers is:\t"<<result<<"\n";
result=n1-n2;
cout<<"Subtraction is:\t"<<result<<endl;
result=n1*n2;
cout<<"Multiplication is:\t"<<result<<endl;
result=n1/n2;
cout<<"Division is:\t"<<result<<endl;
result=n1%n2;
cout<<"Modulus is:\t"<<result<<endl;
getch();
}

Ternary or conditional operator


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
cout<<"Enter 2 numbers:\t";
cin>>x>>y;
(x>y) ? cout<<"x is greater" : cout<<"y is greater";
getch();
}

Sizeof
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
cout << "Size of char : " << sizeof(char)<<endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
getch();
}

Scope resolution operator


#include <iostream.h>
#include<conio.h>
char a = 'm';
static int b = 50;
void main() {
clrscr();
char a = 's';
cout << "The static variable : "<< ::b;
cout << "\nThe local variable : " << a;
cout << "\nThe global variable : " <<::a;
getch();
}

Type cast
#include <iostream.h>
#include<conio.h>
void main() {
clrscr();
double a = 21.09399;
float b = 10.20;
int c;
c=(int)a;
cout << "Line 1 - Value of (int)a is :" << c << endl ;
c = (int) b;
cout << "Line 2 - Value of (int)b is :" << c << endl ;
getch();
}

if – else statement

#include<iostream.h>

#include<conio.h>
void main()

clrscr();

int a,b;

cout<<"Enter the 1st number:\t";

cin>>a;

cout<<"Enter the 2nd number:\t";

cin>>b;

cout<<"\nTwo numbers are:\t"<<a<<"\t"<<b<<endl;

if(a>b)

cout<<"1st no is greater"<<endl;

else

cout<<"2nd no is greater"<<endl;

getch(); }
Compound Conditions in if(multiple if’s)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter the 3 numbers\t";
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<"a is greater";
}
if(b>a && b>c)
cout<<"b is greater";
if(c>a && c>b)
cout<<"c is greater";
getch();
}

Nested selection statements (nested if else)

#include<iostream.h>

#include<conio.h>

void main()

int a,b,c;

cout << "Enter three numbers: ";

cin >> a>> b >> c;

if (a >= b)
{

if (a >= c)

cout << "a is greater " << a;

else

cout << "c is greater " << c;

else {

if (b >= c)

cout << "b is greater " << b;

else

cout << "c is greater " << c;

getch();

else-if ladder

#include<iostream.h>

#include<conio.h>

void main()

{
clrscr();

int t;

cout<<"enter total marks\t";

cin>>t;

if(t>=70 && t<=100)

cout<<"Distinction";

else if(t>=60 && t<70)

cout<<"First class";

else if(t>=35 && t<60)

cout<<"second class";

else if(t>0 && t<35)

cout<<"fail";

else

cout<<"invalid";

getch();

SWITCH STATEMENT
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int day;
cout<<"Enter the day of week between 1-7:\t";
cin>>day;
switch(day)
{
case 1:
cout<<"\n Monday";
break;
case 2:
cout<<"\n Tuesday";
break;
case 3:
cout<<"\n Wednesday";
break;
case 4:
cout<<"\n Thursday";
break;
case 5:
cout<<"\n Friday";
break;
case 6:
cout<<"\n Saturday";
break;
case 7:
cout<<"\n Sunday";
break;
default:
cout<<"\n Invalid input";
break;
}
getch();
}
UNCONDITIONAL CONTROL STRUCTURE (goto)

#include <iostream.h>

#include<conio.h>

void main()

int num;

cout<<"Enter a number: ";

cin>>num;

if (num % 2==0){

goto even;

else {

goto odd;

even:

cout<<"Even Number";

goto end;

odd:

cout<<"odd number";

end:
getch();

for statement
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=10;i++)
{
cout<<i<<endl;
}
getch();
}
Nested for loop

#include <iostream.h>

#include<conio.h>

void main() {

int rows = 5;

int columns = 3;

for (int i = 1; i <= rows; ++i) {

for (int j = 1; j <= columns; ++j) {

cout << "* ";

cout << endl;

getch();

Swapping of two number


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a,b,temp;

cout<<"Enter the two numbers\t";

cin>>a>>b;

cout<<"\nNumbers before swapping are\t"<<a<<"\t"<<b<<endl;

temp=a;

a=b;

b=temp;

cout<<"Numbers after swapping are\t"<<a<<"\t"<<b;

getch();

You might also like