This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Showing posts with label Numbers. Show all posts
Showing posts with label Numbers. Show all posts

Saturday, 15 March 2014

C++ program to get the Arithmetic Mean of first 10 natural numbers (from 1 to 10) using FOR loop, WHILE loop and DO WHILE loop

C++ program to get the Arithmetic Mean of first 10 natural numbers (from 1 to 10) using FOR loop, WHILE loop and DO WHILE loop:

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i, a;
float mean, sum=0;
cout<<"Mean of first 10 natural rumbers (1 to 10): ";
i=1;
cout<<"\n\nEnter your choice:"<<"\n"<<"1-> Using for loop"<<"\n"<<"2-> Using while loop"<<"\n"<<"3-> Using do...while loop"<<"\n\n";
cin>>a;
switch(a)
{
case 1:
for(i=1; i<=10; i++)
sum=sum+i;
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 2:
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 3:
do
{
sum=sum+i;
i++;
}
while(i<=10);
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
default:
break;
}
getch();
}

Monday, 21 October 2013

C++ program to Add two Matrices

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3], b[3][3], c[3][3], i, j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of a["<<i+1<<"]["<<j+1<<"]...";
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of b["<<i+1<<"]["<<j+1<<"]...";
cin>>b[i][j];
}
}
cout<<"\n";
cout<<"Resultant Matrix\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}

C++ program to Subtract two Matrices

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3], b[3][3], c[3][3], i, j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of a["<<i+1<<"]["<<j+1<<"]...";
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of b["<<i+1<<"]["<<j+1<<"]...";
cin>>b[i][j];
}
}
cout<<"\n";
cout<<"Resultant Matrix\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}

Friday, 11 October 2013

C program using Dynamic Memory Allocation to Sort 'n' integers in Descending Order

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *a,n,i,j,t;
clrscr();
printf("\nEnter the number of integers to be sorted in descending order: ");
scanf("%d",&n);
a=(int *)malloc(n *sizeof(int));
printf("\nEnter the %d numbers to be sorted in descending order one by one: ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", (a+i));
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
printf("\nAfter sorting in descending order: ");
for(i=0;i<n;i++)
printf("\n%d",*(a+i));
getch();
}


C program using Dynamic Memory Allocation to Sort 'n' integers in Ascending Order

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *a,n,i,j,t;
clrscr();
printf("\nEnter the number of integers to be sorted in ascending order: ");
scanf("%d",&n);
a=(int *)malloc(n *sizeof(int));
printf("\nEnter the %d numbers to be sorted in ascending order one by one: ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", (a+i));
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(*(a+i)<*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
printf("\nAfter sorting in ascending order: ");
for(i=0;i<n;i++)
printf("\n%d",*(a+i));
getch();
}


Tuesday, 20 August 2013

C++ program to check whether the given number is Adam Number or not

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int r,p,m,b,n,rev,a,c,i,z;
r=0;
rev=0;
cout<<"Enter any number between 10 and 100: ";
cin>>z;
n=z;
c=n*n;
while(n!=0)
{
m=n%10;
r=r*10+m;
n=n/10;
}
cout<<"\n"<<"The square of "<<z<<"is "<<c;
cout<<"\n"<<"The reverse of "<<z<<"is "<<r;
p=r*r;
cout<<"\n"<<"The square of "<<r<<"is "<<p;
while(c!=0)
{
a=c%10;
rev=rev*10+a;
c=c/10;
}
if(rev==p)
cout<<"\n"<<z<<" is an Adam Number";
else
cout<<"\n"<<z<<" is not an Adam Number";
getch();
}


Tuesday, 13 August 2013

C++ program to implement Hybrid Inheritance

Hybrid Inheritance:
Hybrid Inheritance is a method where one or more types of inheritance are combined together and used.

Program:
#include<iostream.h>
#include<conio.h>
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"\nEnter the first number: ";
cin>>num1;
cout<<"\nEnter the second number: ";
cin>>num2;
}
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"\nFor Subtraction:";
cout<<"\nEnter the first number: ";
cin>>n1;
cout<<"\nEnter the second number: ";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public minus
{
public:
void display()
{
cout<<"\nSum of "<<num1<<" and "<<num2<<"= "<<sum;
cout<<"\nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
void main()
{
clrscr();
result z;
z.getdata();
z.add();
z.sub();
z.display();
getch();
}


C++ program to implement Stack ADT using Array

#include<iostream.h>
#include<conio.h>
#define maxsize 10
class stack
{
private:
int a[maxsize];
int top;
public:
stack();
void push(int i);
void pop();
void display();
}
stack::stack()
{
top=0;
}
void stack::push(int i)
{
if(top==maxsize)
cout<<"\nStack is full";
else
{
top++;
a[top]=i;
}
}
void stack::pop()
{
if(top==0)
cout<<"\nStack is empty";
else
{
int i=a[top];
top--;
}
}
void stack::display()
{
if(top==0)
cout<<"Stack is empty";
else
{
cout<<"\nThe elements in the stack are: ";
for(int i=0; i<=top;i++)
cout<<a[i]<<'\t';
}
}
int main()
{
stack s;
int x,ch;
do
{
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>x;
s.push(x);
cout<<"\nThe element is inserted";
break;
case 2:
s.pop();
break;
case 3:
s.display();
case 4:
break;
}
}while(ch!=4);
getch();
}

Monday, 12 August 2013

C program using Dynamic Memory Allocation to Subtract two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,p,q,r,s,*m1, *m2, *a;
clrscr();
printf("Enter the order of matrix A");
printf("\n");
printf("m=");
scanf("%d", &p);
printf("n=");
scanf("%d", &q);
printf("\nEnter the order of matrix B");
printf("\n");
printf("m=");
scanf("%d", &r);
printf("n=");
scanf("%d", &s);
if(q==r)
{
m1=(int*)calloc(p*q, sizeof(int));
m2=(int*)calloc(r*s, sizeof(int));
a=(int*)calloc(p*s, sizeof(int));
printf("\nEnter the elements of matrix A:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",(m1+i*q+j));
}
}
printf("\nEnter the elements of matrix B:");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",(m2+i*r+j));
}
}
printf("\nSubtraction:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
*(a+i*q+j)=*(m1+i*q+j)-(*(m2+i*r+j));
}
}
printf("\nDifference of the Matrices A and B:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",*(a+i*q+j));
}
}
}
getch();
}


C program using Dynamic Memory Allocation to Add two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,p,q,r,s,*m1, *m2, *a;
clrscr();
printf("Enter the order of matrix A");
printf("\n");
printf("m=");
scanf("%d", &p);
printf("n=");
scanf("%d", &q);
printf("\nEnter the order of matrix B");
printf("\n");
printf("m=");
scanf("%d", &r);
printf("n=");
scanf("%d", &s);
if(q==r)
{
m1=(int*)calloc(p*q, sizeof(int));
m2=(int*)calloc(r*s, sizeof(int));
a=(int*)calloc(p*s, sizeof(int));
printf("\nEnter the elements of matrix A:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",(m1+i*q+j));
}
}
printf("\nEnter the elements of matrix B:");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",(m2+i*r+j));
}
}
printf("\nAddition:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
*(a+i*q+j)=*(m1+i*q+j)+(*(m2+i*r+j));
}
}
printf("\nSum of the Matrices A and B:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",*(a+i*q+j));
}
}
}
getch();
}


Program to check whether the given number and its reverse are same

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int dig(int n, int *a);
revnum(int m, int *a);
int n,m;
void main()
{
int x[10], *a=&x[0], newno;
clrscr();
printf("Enter the number:");
scanf("%d", &n);
m=dig(n, a);
newno=revnum(m,a);
printf("\nReverse of the given number %d= %d",n,newno);
if(n==newno)
printf("\n\nEntered number and reverse number are same");
else
printf("\n\nEntered number and reverse number are not same");
getch();
}
int dig(int n, int *a)
{
int i=0, y;
while(n>0)
{
y=n%10;
*(a+i)=y;
n=n/10;
i++;
}
return i;
}
revnum(int m, int *a)
{
int z=0, s=0, d=m;
for(z=0; z<m; ++z, d--)
s=s+*(a+z)* pow(10, d-1);
return s;
}


Thursday, 8 August 2013

Program to check whether the given number is Palindrome or not


#include<stdio.h>
void main()
{
int n, rev=0, rem,temp;
clrscr();
printf("Enter any number: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
rev=rev*10+rem;
temp=temp/10;
}
if(rev==n)
printf("%d is a Palindrome",n);
else
printf("%d is not a Palindrome",n);
getch();
}





Wednesday, 7 August 2013

Program to calculate the Power of a number using WHILE loop (2)

#include<stdio.h>
void main()
{
int bas, exp, a, power=1;
clrscr();
printf("Enter the value of base number: ");
scanf("%d",&bas);
printf("\nEnter the value of exponent: ");
scanf("%d", &exp);
a=exp;
while (a!=0)
{
power*=bas;
--a;
}
printf("\nThe value of %d power %d = %d", bas, exp, power);
getch();
}


Program to calculate the Power of a number using WHILE loop (1)

#include<stdio.h>
#include<conio.h>
void main()  
{  
int bas, exp, count = 1, power = 1 ;  
clrscr() ;  
printf("Enter the value of base number: ") ;
scanf("%d", &bas) ;  
printf("\nEnter the value of exponent : ") ;  
scanf("%d", &exp) ;
while(count <= exp)  
{  
power = power * bas ;  
count ++ ;  
}  
printf("\nThe value of %d power %d = %d", bas, exp, power) ;
getch() ;  
}


Program to find the number of vowels, consonants, digits, spaces and other characters

#include<stdio.h>
main()
{
char text[100];
int nv=0, nc=0, nd=0, ns=0, no=0;
void input(char text[], int *pv, int *pc, int *pd, int *ps, int *po);
clrscr();
printf("Enter a line of text: ");
gets(text);
input(text, &nv, &nc, &nd, &ns, &no);
printf("\nNo of Vowels:%d", nv);
printf("\nNo of Consonants:%d", nc);
printf("\nNo of Digits:%d", nd);
printf("\nNo of Spaces:%d", ns);
printf("\nNo of Other Characters:%d", no);
getch();
}
void input(char text[], int *pv, int *pc, int *pd, int *ps, int *po)
{
char c;
int i=0;
while((c=toupper(text[i]))!='\0')
{
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
++*pv;
else if(c>='A'&&c<='Z')
++*pc;
else if(c>='0'&&c<='9')
++*pd;
else if(c==' '||c=='\t')
++*ps;
else
++*po;
++i;
}
}