Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

Wednesday, August 21, 2013

Program to illustrate the working of new and delete operators

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<"Enter the number of students in the class : ";
cin>>n;
int *p=new int[n];
int *q=new int[n];
cout<<"Input the class numbers : ";
for(i=0;i<n;i++)
cin>>*(p+i);
cout<<"Input the marks : ";
for(i=0;i<n;i++)
cin>>*(q+1);
cout<<"The list of students with marks : ";
for(i=0;i<n;i++)
cout<<"\n"<<*(p+i)<<"\t"<<*(q+i);
delete [] p;
delete [] q;
getch();
}

For more details about pointers click here

Wednesday, August 7, 2013

Program to delete an element from an array

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int s,n,i,p,a[50];
clrscr();
cout<<"Enter number of students : ";
cin>>n;
cout<<"Enter the marks : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the marks to be deleted : ";
cin>>s;
for(i=0;i<n;i++)
if(a[i]==s)
    {
     p=i;
     break;
    }
for(i=p;i<n-1;i++)
a[i]=a[i+1];
cout<<"The new mark list is : ";
for(i=0;i<n-1;i++)
cout<<a[i];
getch();
}

Friday, August 2, 2013

Program to search element in an array - Linear search

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n,p=-1;
char a[100],s;
cout<<"Enter the line of text : ";
gets(a);
cout<<"Enter the character to be searched : ";
cin>>s;
for(int i=0;a[i]!='\0';++i)
if(a[i]==s)
p=i;
if(p>=0)
cout<<"Character found in "<<p<<"th position";
else
cout<<"Character not found ";
getch();
}

Saturday, July 27, 2013

Program to display all prime numbers less than 100

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,f;
for(i=2;i<100;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
    {
    f=1;
    break;
    }
else
    f=0;
}
if(!flag)
cout<<i;
}
getch();
}

Tuesday, July 23, 2013

Program to access the marks of 3 students and display it out

#include<iostream.h>
#include<conio.h>
void main()
{
int n[3],i;
cout<<"Enter the marks\n";
for(i=0;i<3;i++)
cin>>n[i];
cout<<The given marks are\n";
for(i=0;i<3;i++)
cout<<"\n"<<n[i];
getch();
}

Monday, July 22, 2013

Program to input characters and change their case using " do-while loop "

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char ch;
do
{
    cout<<"\n Enter a character (0 to stop) : ";
    cin>>ch;
    if(ch=='\n')
    {
        ch=getchar();
        cout<<end1;
    }
    else
    if(ch>=65 && ch<=90)
    ch=ch+32;
    else
    if(ch>=97 && ch<=122)
    ch=ch-32;
    putchar();
}while(ch!='0');
getch();
}

Program using " Nested loop "

#include<iostream.h>
#include<stdio.h>
void main()
{
clrscr();
char c;
int i,j;
c='A';
for(i=1;i<=5;i++)
{
    for(j=1;j<=i;j++)
    {
        putchar(c);
    }
    putchar('\n');
    fflush(stdout);
}
}

The output of this program will be:-

A
BB
CCC
DDDD
EEEEE

Sunday, July 21, 2013

Program to check whether a given number is prime or not using "for loop"

#include<iostream.h>
#include<process.h>
void main()
{
int num,i;
clrscr();
cout<<"Enter the number ";
cin>>num;
for(i=2;i<=num/2;++i)
if(num%i==0)
{
    cout<<"Not a prime number ";
    exit(0);
}
cout<<"It is prime number ";
getch();
}

Program to print the sum of first n natural numbers using " while loop "

#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,sum;
clrscr();
cout<<"Enter the limit  ";
cin>>n;
sum=0;
i=1;
while(i<n)
{
    sum=sum+i;
    ++i;
}
cout<<"sum of first "<<n<<" natural number is "<<sum;
getch();
}

Program to print the multiplication of a given numbers using "for loop"

#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr();
cout<<"Enter an integer number : ";
cin>>n;
for(i=1;i<=10;i++)
cout<<i<<" * "<<n<<" = "<<i+n;
getch();
}

output of this program if i enter number 5 :-

Enter an integer number :
5
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

To know about loop click here