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

Wednesday, November 20, 2013

Program to print the first 10 lines of Pascal's Triangle


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
long triangle(int x,int y);
int main()
{
clrscr();
const lines=10;
for (int i=0;i<lines;i++)
for (int j=1;j<lines-i;j++)
cout << setw(2) << " ";
for (int j=0;j<=i;j++)
cout << setw(4) << triangle(i,j);
cout << endl;
getch();
}
long triangle(int x,int y)
{
if(x<0||y<0||y>x)
return 0;
long c=1;
for (int i=1;i<=y;i++,x--)
c=c*x/i;
return c;
}

To know about Pascal's Triangle Click Here

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();
}

Sunday, August 4, 2013

Program to create a square matrix and find the sum of diagonal elements

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20][20],sum=0,m,n,i,j;
cout<<"Enter the size : ";
cin>>m>>n;
if(m==n)
{
cout<<"Enter the elements : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
sum+=a[i][j];
}
}
cout<<sum;
}
else
cout<<"Not a square matrix";
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();
}

Friday, July 26, 2013

Program to insert new element in an array

#include<iostream.h>
#include<conio.h>
void main()
{
int s,n,i,a[50],p;
clrscr();
cout<<"Enter the number of elements of array : ";
cin>>n;
cout<<"Enter the elements : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the new element and its position : ";
cin>>s>>p;
for(i=n;i>=p;i--)
a[i]=a[i-1];
a[p]=s;
cout<<"The array after inserting new element is : ";
for(i=0;i<n+1;i++)
cout<<a[i]<<"  ";
getch();
}

To know about array click here

Program to reversing a string

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20],rev[20];
int len,k=0;
cout<<"Enter the string  ";
cin.getline(name,20);
for(len=0;name[len]!='\0';len++)
for(i=len;i>=0;i--)
{
rev[k]=name[i];
k++;
}
rev[k]='\0';
cout<<"Reversed string  ";
cout.write(rev,20);
getch();
}

Program for multiplication of matrix using " class "

To know about 'class' click here


#include<iostream.h>
#include<conio.h>
class matrix
 {
 int a[10][10];
 int m,n;
 public:
 void input();
 void output();
 void multiply(matrix,matrix);
 };
 void matrix::input()
 {
 cout<<"Enter the number of row : ";
 cin>>m;
 cout<<"Enter the number of column : ";
 cin>>n;
 cout<<"Matrix"<<"\n";
 for(int i=0;i<m;i++)
 {
 for(int j=0;j<n;j++)
 {
 cin>>a[i][j];
 }
 }
 }
 void matrix :: output()
 {
 for(int i=0;i<m;i++)
 {
 cout<<"\n";
 for(int j=0;j<n;j++)
 {
 cout<<a[i][j]<<"\t";
 }
 }
 }
 void matrix :: multiply(matrix m1, matrix m2)
 {
 if(m1.n!=m2.m)
    {
     cout<<"matrix multiplication is not possible";
    }
 else
    {
     for(int i=0;i<m1.m;i++)
    {
     for(int j=0;j<m2.n;j++)
    {
     a[i][j]=0;
     for(int k=0;k<m1.n;k++)
     {
     a[i][j]=a[i][j] +( m1.a[i][k]*m2.a[k][j]);
     m=m1.m;
     n=m2.n;
    }
 }
 }
 }
 }
 void main()
 {
 clrscr();
 matrix m1,m2,m3;
 m1.input();
 m2.input();
 m3.multiply(m1,m2);
 m3.output();
 getch();
 }

Program to find out the sum of two matrices



#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n;
clrscr();
cout<<"Enter the number of raws and columns of two matrices : ";
cin>>m>>n;
cout<<"Enter the elements into matrix A\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"\nEnter the elements into matrix B\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>b[i][j];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<m;i++)
    {
    cout<<"\n";
    for(j=0;j<n;j++)
    cout<<c[i][j];
    cout<<"\t";
    }
getch();
}


Thursday, July 25, 2013

Program to accept 20 numbers and display it in ascending order and descending order - Bubble sort


#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,j,t;
clrscr();
cout<<"Enter 20 numbers : ";
for(i=0;i<20;i++)
cin>>a[i];
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
 if(a[i]>a[j])
{
    t=a[i];
    a[i]=a[j];
    a[j]=t;
}
}
}
cout<<"Ascending order : "<<"\n";
for(i=0;i<20;i++)
cout<<a[i]<<"\n";
cout<<"Descending order : "<<"\n";
for(i=19;i>=0;i--)
cout<<a[i]<<"\n";
getch();
}

Program to find the sum of all the elements of a matrix


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[20][20],sum=0,m,n,i,j;
 cout<<"Enter the size of matrix : "
 cin>>m>>n;
 cout<<"Enter the elements : ";
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 {
     cin>>a[i][j];
     sum+=a[i][j];
 }
 cout<<"Sum of elements of the matrix is : "<<sum
 getch();
}

Program to access the elements in two dimensional array and display it


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[20][20],m,n,i,j;
 cout<<"Enter the size of matrix : "
 cin>>m>>n;
 cout<<"Enter the elements : ";
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 cin>>a[i][j];
 cout<<"The matix is : "<<"\n";
 for(i=0;i<m;i++)
 {
 for(j=0;j<n;j++)
 {
 cout<<a[i][j]<<"\t";
 }
 }
 getch();
}

Program to generate the first n elements of a Fibonacci series


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,i,n,c;
a=0;
b=1;
cout << "Enter the number of terms : ";
cin>>n;
cout << a <<"\t"<<b<<"\t";
for(i=2;i<n;i++)
{
    c=a+b;
    a=b;
    b=c;
    cout<<c<<"\t";

}
getch();
}

Wednesday, July 24, 2013

Program to count the number of lower case vowels in a string


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
{
 clrscr();
 char st[100];
 int l,n,i;
 cout<<"Enter the string\n";
 cin.getline(st,100);
 l=strlen(st);
 for(i=0;i<=l;i++)
 {
   if(islower(st(i))) && ((st(i)==a)||(st(i)==e)||(st(i)==i)||(st(i)==o)||(st(i)==u))
   n++;
 }
 cout<<"The number of lower case vowels is : "<<n;
 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();
}

Program to convert Binary into Decimal number


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
  int a[10],i,n,sum=0,j,k=0;
  clrscr();
  cout<<"Enter binary number : ";
  cin>>n;
  i=0;
  while(n>0)
  {
      a[i]=n%10;
      n=n/10;
      i++;
  }
  for(j=0;j<i;j++)
  {
      sum=sum+a[j]*pow(2,k);
       k++;
  }
  cout<<"Decimal number is : "<<sum;
  getch();
}

Monday, July 22, 2013

Program to read a string and to count the number of words

#include<iostream.h>
#include<stdio.h>
int main()
{
clrscr();
char str[80];
int i;
int wordcount=0;
puts("Enter a string upto 80 characters : ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
    if(str[i]==' ')
    wordcount++;
}
cout<<"The number of words are "<<wordcount<<"\n";
return0;
}

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();
}