Professional Documents
Culture Documents
9 6093461787225621084
9 6093461787225621084
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
//programme to demostrate control structure
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"Here the choices for demostration are :"<<endl<<"1 : while loop with break statement"<<endl
<<"2 : do while loop with continue statement"<<endl<<"3 : for loop with goto statement"<<endl
<<"Now enter your choice"<<endl;
cin>>a;
switch(a){
case 1:{
int num = 1;
while(num>0){
cout<<"Number : "<<num<<endl;
num++;
if(num == 7)//Here when num = 7 then the break statement encountered which terminates the loop.
break;
}
break;
}
case 2:{
int n = 1;
do{
if(n == 7)
//Here when n = 7 then the continue statement is encountered due to which the loop jump to next iteration.
continue;
cout<<"Number : "<<n<<endl;
n++;
}while(n>0);
break;
}
case 3:{
int m, prime = 1;
cout<<"Enter the number"<<endl;
cin>>m;
for(int i=2;i< m;i++){
if(m%i==0){
prime = 0;
break;
}
}
if(prime == 0 || m == 1 || m == 0)
goto print;//goto statement transfer control to another line of code when encountered as in case of print.
else
cout<<"The number is prime"<<endl;
print:
cout<<"The number is not prime"<<endl;
}
break;
}
return 0;
}
/*output
Here the choices for demostration are :
1 : while loop with break statement
2 : do while loop with continue statement
3 : for loop with goto statement
Now enter your choice
2
Number : 1
Number : 2
Number : 3
Number : 4
Number : 5
Number : 6
*/
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
//integer to char
int number=5;
char numberr=number+48;
cout<<"Integer>Character= "<<numberr<<endl;
//char to integer
char num='5';
int n=num-48;
cout<<"Character->Integer = "<<n<<endl;
double p = 1.2;
float f = 3.5;
cout << b;
}
/*output
x = 107
y=a
z = 108
Integer>Character= 5
Character->Integer = 5
float->int = 2
Sum = 23*/
c.sum_complex(a, b);//This function is used to compute the sum of the above two complex numbers.
c.print_number();
//Now the demostration of structure is there.
input();
display();
//Here union has been described.
m.x = 14;
m.y = 'e';
m.z = 78.54;
cout<<"x : "<<m.x<<endl<<"y : "<<m.y<<endl<<"z : "<<m.z<<endl;//Here only the value of z is stored. Rest x a
nd y values are garbage values.
//Demostartion of enum
type = rectangle;
cout<<"The shape number is : "<<type<<endl;
return 0;
}
void input(){
cout<<"Enter the roll number of the student"<<endl;
cin>>d.roll_number;
cout<<"Enter the name of the student"<<endl;
cin>>d.name;
cout<<"Enter the branch of the student"<<endl;
cin>>d.branch;
cout<<"Enter the section of the student"<<endl;
cin>>d.section;
}
void display(){
cout<<"Roll number : "<<d.roll_number<<endl<<"Name : "<<d.name<<endl<<"Branch : "<<d.branch<<endl<<"Se
ction : "<<d.section<<endl;
}
//output
/*2
3
The complex number is : 2 + 3i
Enter the real and imaginary part of second complex number
The complex number is : 4201888 + 6422400i
The complex number is : 4201890 + 6422403i
Enter the roll number of the student
ue203026
Enter the name of the student
Ashish
Enter the branch of the student
cse
Enter the section of the student
1
Roll number : ue203026
Name : Ashish
Branch : cse
Section : 1
x : 1117590651
y:{
z : 78.54
The shape number is : 1*/
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
//programme to demonstrate symbolic const , enumeration,reference var
#include <bits/stdc++.h>
using namespace std;
#define val 10
#define floatVal 4.5
#define charVal 'G'
enum week
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main()
{ // for symbolic const that above int main defined
cout << "Integer Constant: " << val << "\n";
cout << "Floating point Constant: " << floatVal << "\n";
cout << "Character Constant: " << charVal << "\n";
int a = 10;
int &b = a;
return 0;
}
Integer Constant: 10
Floating point Constant: 4.5
Character Constant: G
Value of a: 10
Value of b: 10
Day: 5
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
//demonstrate expression
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"The choices are :"<<endl<<"1 : constant expression"<<endl<<"2 : integer expression"<<endl
<<"3 : float expression"<<endl<<"4 : Logical and relational expression"<<endl<<"5 : Assignment and compound
expression"<<endl
<<"6 : Pointer expression"<<endl<<"7 : Bitwise expression"<<endl<<"Now enter your choice"<<endl;
cin>>a;
switch(a){
case 1:{
const int b = 45;
//Here if we assign b = 78 then compiler throws an error because b is read only value it cannot be changed.
cout<<"The value of b is : "<<b<<endl;
break;
}case 2:{
int x;
float y;
cout<<"Enter the two numbers"<<endl;
cin>>x>>y;
cout<<"The sum is :"<<x + (int)y<<endl;//here with the help of (int) the value comes out to be integer.
break;
}case 3:{
int z;
double l;
cout<<"Enter the two numbers"<<endl;
cin>>z>>l;
cout<<"The multiplication is : "<<(float)z * float(l)<<endl;//here with the help of (float) the value come out in f
loat type.
break;
}case 4:{
int x, y, z, a;
cout<<"Enter the two numbers"<<endl;
cin>>x>>y;
z = x<10 && y != 5 && y > 20;//here z = 1 if all the conditions gets true otherwise z = 0
a = x != 10 || y == 20;//here a = 1 if any of the conditions gets true and a = 0 if neither of the conditions get true.