Sie sind auf Seite 1von 23

DEBUGGING PROGRAMS ORIGINAL C++

1.Odd Or Even

#include<iostream.h>

#include<conio.h>

void main()

int no;

clrscr();

cout<<"Enter any num: ";

cin>>no;

if(no%2==0)

cout<<"Even num";

else

cout<<"Odd num";

getch();

2.Prime number program in C++

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

void main()

int i,no;

clrscr();

cout<<"Enter any num: ";

cin>>no;

if(no==1)

cout<<"Smallest prime num is 2";

for(i=2;i<no;i++)

if(no%i==0)

cout<<"Not prime num";

break;

if(no==i)

{
cout<<"Prime num";

getch();

3.Factorial of a Number in C++

#include<iostream.h>

#include<conio.h>

void main()

int i, no, fact=1;

clrscr();

cout<<"Enter the any no. : ";

cin>>no;

for(i=1;i<=no;i++)

fact=fact*i;

cout<<"Factorial: "<<fact;

getch();

4.C++ Program to Reverse a Number


#include<iostream.h>

#include<conio.h>

void main()

int no,rev=0,r,a;

clrscr();

cout<<"Enter the num: ";

cin>>no;

a=no;

while(no>0)

r=no%10;

rev=rev*10+r;

no=no/10;

cout<<"\nReverse of "<<a<<" is: "<<rev;

getch();

5.Program to Check String is Palindrome or not

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

void main()

int i,j,len,flag=1;

char a[20];

cout<<"Enter a string:";

cin>>a;

for(len=0;a[len]!='\0';++len);

for(i=0,j=len-1;i<len/2;++i,--j)

if(a[j]!=a[i])

flag=0;

if(flag==1)

cout<<"\nString is Palindrome";

}
else

cout<<"\nString is not Palindrome";

getch();

6.Square Root of a Number

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

int num,ans;

clrscr();

cout<<"Enter any Num: ";

cin>>num;

ans=pow(num,0.5);

cout<<"\n Squre of "<<num<<" is: "<<ans;

getch();

7.Operator Overloading
#include<iostream.h>

#include<conio.h>

class temp

private:

int count;

public:

temp():count(10)

{}

void operator ++()

count=count+1;

void Display()

cout<<"Count: "<<count;

};

void main()

{
clrscr();

temp t;

++t; /* operator function void operator ++() is called */

t.Display();

getch();

8.Convert Uppercase to Lowercase

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

char ch;

cout<<"Enter a Character in Uppercase: ";

cin>>ch;

ch=ch+32;

cout<<"Character in Lowercase: "<<ch;

getch();

9.Sum of Natural Numbers

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

void main()

int N=0, num, i, sum=0;

clrscr();

cout<<"Enter the Number of Integers to Add: ";

cin >> N;

cout<<"Enter "<<N<<" Numbers: ";

for(i= 0; i< N; i++){

cin>>num;

sum = sum + num;

cout<<"Sum is: "<<sum;

getch();

10.Check Leap Year

#include<iostream.h>

#include<conio.h>
void main()

int y;

cout<<"Enter a year: ";

cin>>y;

if(y%4==0)

cout<<"Leap Year";

else

cout<<"Not a Leap Year";

getch();

11.Addition of Two Matrix Program

#include<iostream.h>

#include<conio.h>

void main()

int x[3][3],y[3][3],z[3][3],i,j;
clrscr();

cout<<"ENTER ELEMENTS OF 1st MATRIX\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>x[i][j];

cout<<"ENTER ELEMENTS OF 2nd MATRIX\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>y[i][j];

cout<<"MATRIX [X]";

for(i=0;i<3;i++)

cout<<"\n\n";

for(j=0;j<3;j++)

cout<<x[i][j];

cout<<"\nMATRIX [Y]";

for(i=0;i<3;i++)
{

cout<<"\n\n";

for(j=0;j<3;j++)

cout<<y[i][j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

z[i][j]=x[i][j]+y[i][j];

cout<<"\nMATRIX [Z]";

for(i=0;i<3;i++)

cout<<"\n\n";

for(j=0;j<3;j++)

cout<<z[i][j];

getch();

12.Convert Celsius to Fahrenheit

#include<iostream.h>

#include<conio.h>
void main()

float cel, far;

clrscr();

cout<<"Enter temp. in Celsius: ";

cin>>cel;

far = cel * 9/5 + 32;

cout<<"Temp. in Fahrenheit: "<<far;

getch();

13.Sum of Diagonal Elements of Matrix

#include<stdio.h>

#include<conio.h>

void main()

int i, j, matrix[10][10], row, col;

int sum = 0;

clrscr();
cout<<"\nEnter the number of Rows : ";

cin>>row;

cout<<"\nEnter the number of Columns : ";

cin>>col;

//Accept the Elements in m x n Matrix

for (i = 0; i < row; i++)

for (j = 0; j < col; j++)

cout<<"\nEnter the Element a[%d][%d] : ", i, j;

cin>>matrix[i][j];

//Addition of all Diagonal Elements

for (i = 0; i < row; i++)

for (j = 0; j < col; j++)

if (i == j)
sum = sum + matrix[i][j];

//Print out the Result

cout<<"\nAddition of Diagonal Array Elements in the Matrix is: "<<sum;

getch();

14.Multiply Two Matrices

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int mat1[2][2], mat2[2][2], mat3[2][2], sum=0, i, j, k;

cout<<"Enter First Matrix Element (2*2): ";

for(i=0; i<2; i++)

for(j=0; j<2; j++)

cin>>mat1[i][j];
}

cout<<"Enter Second Matrix Element (2*2): ";

for(i=0; i<2; i++)

for(j=0; j<2; j++)

cin>>mat2[i][j];

cout<<"Multiplying two Matrices........\n";

for(i=0; i<2; i++)

for(j=0; j<2; j++)

sum=0;

for(k=0; k<2; k++)

sum = sum + mat1[i][k] * mat2[k][j];

mat3[i][j] = sum;

}
}

cout<<"\nMultiplication of Two Matrices : \n";

for(i=0; i<2; i++)

for(j=0; j<2; j++)

cout<<mat3[i][j]<<" ";

cout<<"\n";

getch();

15.Reverse an Array

#include<iostream.h>

#include<conio.h>

void main()

int a[20],b[20],i,j,n;

clrscr();

cout<<"How many elements you want to enter: ";

cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";

for(i=0; i<n ;i++)

cin>>a[i];

cout<<"Reverse of Array: ";

for(i=n-1,j=0; i>=0;i--,j++)

b[i]=a[j];

for(i=0; i<n ;i++)

cout<<b[i];

getch();

16.Convert Binary to Decimal

#include<iostream.h>

#include<conio.h>

void main()
{

clrscr();

long int bin_number, dec_number=0, i=1, rem;

cout<<"Please Enter any Binary Number: ";

cin>>bin_number;

while(bin_number!=0)

rem=bin_number%10;

dec_number=dec_number+rem*i;

i=i*2;

bin_number=bin_number/10;

cout<<"Equivalent Decimal value:"<<dec_number;

getch();

17.Print Triangle of Stars

#include<iostream.h>

#include<conio.h>

void main()

int i,j,k;
clrscr();

for(i=1; i<=5; i++)

for(j=4; j>=i; j--)

cout<<" ";

for(k=1; k<=(2*i-1); k++)

cout<<"*";

cout<<"\n";

getch();

18.Print number Pattern

#include<iostream.h>

#include<conio.h>

void main()

int i,j, k=1;


clrscr();

for(i=1;i<=5;i++)

for(j=1;j<i;j++)

cout<<k;

k++

cout<<"\n";

getch();

19.Print number Series

#include<iostream.h>

#include<conio.h>

void main()

int i,n,sum=0;

clrscr();

n=10;

for(i=1;i<=n;i++)
{

sum+=i;

cout<<"Sum: "<<sum;

getch();

20.Print Floyd Triangle

#include<iostream.h>

#include<conio.h>

void main()

int i, j, rows, counter;

cout <<"Enter The Number of Rows of Floyd's Triangle\n";

cin>>rows;

// Print Floyd's triangle

for (counter = 1, i = 1; i <= rows; i++) {

// Print ith row

for (j = 1; j <= i; j++) {

cout << counter++ << " ";


}

cout<<endl;

getch();

Das könnte Ihnen auch gefallen