This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Showing posts with label Do While Loop. Show all posts
Showing posts with label Do While Loop. Show all posts

Saturday, 15 March 2014

C++ program to get the Arithmetic Mean of first 10 natural numbers (from 1 to 10) using FOR loop, WHILE loop and DO WHILE loop

C++ program to get the Arithmetic Mean of first 10 natural numbers (from 1 to 10) using FOR loop, WHILE loop and DO WHILE loop:

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i, a;
float mean, sum=0;
cout<<"Mean of first 10 natural rumbers (1 to 10): ";
i=1;
cout<<"\n\nEnter your choice:"<<"\n"<<"1-> Using for loop"<<"\n"<<"2-> Using while loop"<<"\n"<<"3-> Using do...while loop"<<"\n\n";
cin>>a;
switch(a)
{
case 1:
for(i=1; i<=10; i++)
sum=sum+i;
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 2:
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 3:
do
{
sum=sum+i;
i++;
}
while(i<=10);
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
default:
break;
}
getch();
}

Tuesday, 13 August 2013

C++ program to implement Stack ADT using Array

#include<iostream.h>
#include<conio.h>
#define maxsize 10
class stack
{
private:
int a[maxsize];
int top;
public:
stack();
void push(int i);
void pop();
void display();
}
stack::stack()
{
top=0;
}
void stack::push(int i)
{
if(top==maxsize)
cout<<"\nStack is full";
else
{
top++;
a[top]=i;
}
}
void stack::pop()
{
if(top==0)
cout<<"\nStack is empty";
else
{
int i=a[top];
top--;
}
}
void stack::display()
{
if(top==0)
cout<<"Stack is empty";
else
{
cout<<"\nThe elements in the stack are: ";
for(int i=0; i<=top;i++)
cout<<a[i]<<'\t';
}
}
int main()
{
stack s;
int x,ch;
do
{
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>x;
s.push(x);
cout<<"\nThe element is inserted";
break;
case 2:
s.pop();
break;
case 3:
s.display();
case 4:
break;
}
}while(ch!=4);
getch();
}

Wednesday, 5 June 2013

Program to find the Fibonacci Series using DO WHILE loop (3)



#include<stdio.h>
void main()
{
int a,b,c,n,i;
clrscr();
a=0;
b=1;
i=3;
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("%d\n%d",a,b);
do
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
i=i+1;
}
while (i<=n);
getch();
}

Tuesday, 4 June 2013

C++ program to implement Stack ADT using Linked List


#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *link;
};
class stack
{
private:
struct node *top;
public:
stack();
void push(int n);
void pop();
void display();
};
stack::stack()
{
top=NULL;
}
void stack::push(int n)
{
node *tmp;
tmp= new node;
if(tmp==NULL)
cout<<"\nStack is empty";
else
{
tmp->data=n;
tmp->link=top;
top=tmp;
}
}
void stack::pop()
{
if(top==NULL)
cout<<"\nStack is empty";
else
{
node *tmp;
int n;
tmp=top;
n=tmp->data;
top=top->link;
delete tmp;
}
}
void stack::display()
{
node *tmp;
tmp=top;
if(tmp==NULL)
cout<<"\nStack is empty";
else
{
cout<<"\nThe elements in the stack are:";
while(tmp!=NULL)
{
cout<<tmp->data;
tmp=tmp->link;
}
}
}
void main()
{
clrscr();
stack s;
int x,ch;
do
{
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>x;
s.push(x);
cout<<"\nThe element is inserted";
break;
case 2:
s.pop();
break;
case 3:
s.display();
case 4:
break;
}
}while(ch!=4);
getch();
}