Thursday, March 3, 2011

Do - while loop in cplusplus , using Do-while in c++

A do-while loop is a loop which runs at least once , because it first runs a chunk of code and at the end checks the condition so of the code wasn't true , even then it runs once , here is a simple use of do-while loop in cplusplus (c++).

#include"iostream.h" // input output stream
#include"conio.h" // console input output

void main()
{
int n;


char c;

do
{
cout<<"\n\nEnter a number"; // cout is used to print something on screen
cin>>n;
cout<<"You entered :: "<cout<<"\n\nDo you wanna enter number again ? press 1 for yes and 2 for no";
c=getch();
}
while(c=='1');


getch();
}


In the above program , user enters a number and the program prints the entered value on screen , then the program asks whether user want to enter more values or not , if user enters 1 , he is again asked to enter a new value else loop exists...

Saturday, February 26, 2011

Finding grade using swith statement , finding grade in school program

Here is a simple program to find your school grade by entering your marks using switch statement in c++ , A simple c++ program to find grade using cpp switch statement

#include"iostream.h"
#include"conio.h"

void main()
{
clrscr();
int marks,x;
char grade;
cout<<"Enter your marks :: \n"; // marks should be out of 100

x=marks/10;

switch(marks)
{
case 9:
{
grade='a';
break;
}


case 8:
{
grade='b';
break;
}


case 7:
{
grade='c';
break;
}


case 6:
{
grade='d';
break;
}


case 5:
{
grade='e';
break;
}

default:
{
grade='f';
break;
}

}


cout<<"Your grade is :: "<
getch();
}

Saturday, February 19, 2011

Nested Loop Example ( A simple star program )

When Loops are used within loops then it is called as nested loops. Nested loops are almost used in most of the programs because a single loop can't fulfill our needs. For example making a program for finding prime numbers between a given range will require nested loops.

Here is a simple star program which will print the following shape

***
***
***


#include"conio.h"
#include"iostream.h"

void main()
{
clrscr();


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

for(int j=1;j<=3;j++)
{
cout<<"*";
}
cout<}

getch();
}

Friday, February 4, 2011

Finding prime numbers in c++

Prime numbers are those numbers who have only two divisors , 1 and itself , Making a c++ program for prime numbers is too easy , you just have to check the number by dividing it with all the numbers from 1 to this number , if the remainder was zero for more than 2 times then the number isn't prim else number is prime.
Following is a program to find prime numbers between a range in c++

void main()
{
int num1=2,num2=10; // you can input these numbers from the user
int count=0;

for(int i=num1;i<=num2;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
else
{}
}

if(count==2)
{
cout<<"\nNumber "<}

}



}


LOGIC : divide each number with all the numbers from 1 to this given number , if its remainder comes 0 for 2 times then it is a prime number

Finding Even|Odd numbers

Hi , This post is to show how conditions work ,

For example you wanna make a program which should take a number as an input and should print whether its even or odd , here is the program

#include"iostream.h"
#include"conio.h"

void main()
{
clrscr();

int num;

cin>>num;

if(num%2==0)
{
cout<<"Number is Even";
}
else
{
cout<<"Number is odd";
}

getch();
}

LOGIC : Those numbers whose remainder by dividing with 2 is 0 is an even number otherwise the number is odd.