You are on page 1of 2

Program to enter a string and find its length

#include <iostream.h>
#include <conio.h>
#include <string.h>

void main()
{
clrscr();
int slength;
char x[81]; //Allowing the user to input a maximum of 80 characters.
cout << "Enter the string : " << endl;
cin>>x;
slength=strlen(x);
cout << "The length of the string " << x << " is " << slength << "." <<
endl;
getch();
}

Program to plot pixels


#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

void main (int)


{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,x;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,"d:\\bc3\\bgi");
//Reading result of initialization.
errorcode=graphresult();
if(errorcode!=grOk)
//An error occurred.
{
printf("Graphics error occurred : %s \n",grapherrormsg(errorcode));
printf("Press any key to stop : ");
getch();
exit(1); //Terminate the program due to error.
}
for(x=40;x<120;x=x+10)
{
putpixel(x,200,14); //Plots 1 pixel at position(x,200) with YELLOW
color(14)
getch();
}
}

Program to enter 10 integers in a single-dimension array and then print out


the array in ascending order
#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int array[10],t;
for(int x=0;x<10;x++)
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10;x++)
{
for(int y=0;y<9;y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<10;x++)
cout << endl << array[x];
getch();
}

Program to find the sum of either of the diagonals of a 4 x 4 matrix


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int x;
int A[4][4],sum=0; //Reading the matrix.
cout << "Enter the elements of the matrix : " << endl;
for(int y=0;y<4;y++)
for (int x=0;x<4;x++)
{
cout << "Element " << x+1 << ", " << y+1 << " : ";
cin>>A[x][y];
}
//Sum of either of the diagonal elements.
for(x=0;x<4;x++)
for(y=0;y<4;y++)
if(x==y)
sum+=A[x][y];
else if(y==4-(1+1));
sum+=A[x][y];
cout << "Sum of either of the diagonal elements is : " << sum;
getch();
}

You might also like