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 If Statement. Show all posts
Showing posts with label If Statement. Show all posts

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


Monday, 19 August 2013

C++ program to check whether the given string is Palindrome or not (3)

Palindrome:
Palindromes are words or phrases that read the same in both directions.
Some examples of common palindromic words: civic, dewed, deified, dad, mom, devoved, hannah, repaper, radar, level, rotor, kayak, reviver, racecar, redder, madam, redder, bob, pop, tot, malayalam, noon, rotator, rotavator, stats, solos, tenet and refer.

Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[50],m[50];
int p=1,i;
clrscr();
cout<<"Enter the string: ";
cin>>s;
strcpy(m,s);
strrev(m);
for(i=0;i<=strlen(s)-1;i++)
{
if(*(m+i)!=*(s+i))
{
p=0;
break;
}
}
if(!p)
cout<<"\n"<<s<<" is not a Palindrome";
else
cout<<"\n"<<s<<" is a Palindrome";
getch();
}



C program to demonstrate or solve Hanoi Tower

The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another rod, obeying the following rules: 
  • Only one disk must be moved at a time.
  • Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.
  • No disk may be placed on top of a smaller disk.
The puzzle can be played with any number of disks, although many toy versions have around seven to nine of them. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks. With three disks, the puzzle can be solved in seven moves.
 
Program:
#include<stdio.h>
void main()
{
void honoi(int, char, char, char);
int n;
clrscr();
printf("How many disk? ");
scanf("%d",&n);
honoi(n,'L','R','C');
getch();
}
void honoi(int n, char from, char to, char temp)
{
if(n>0)
{
honoi(n-1, from, temp, to);
printf("Move the disk %d from %c to %c\n",n,from,to);
honoi(n-1,temp,to,from);
}
}


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


Sunday, 23 June 2013

Program to print the Star pattern 3

#include<stdio.h>
main()
{
char prnt = '*';
int i, j, k, s, nos = -1;
clrscr();
for (i = 5; i >= 1; i--)
{
for (j = 1; j <= i; j++)

printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{
printf("  ");
}
for (k = 1; k <= i; k++)
{
if (i == 5 && k == 5)
{   
continue;  
}  
printf("%2c", prnt); 

nos = nos + 2; 
printf("\n");
}
nos = 5;
for (i = 2; i <= 5; i++)

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

printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{  
printf("  "); 

for (k = 1; k <= i; k++)
{  
if (i == 5 && k == 5)
{   
break;  
}  
printf("%2c", prnt); 

nos = nos - 2; 
printf("\n");
}
getch();
}


Saturday, 22 June 2013

Program to print the Star pattern 2

#include<stdio.h>
main()
{
char prnt = '*';
int i, j, k, s, c = 1, nos = 9;
clrscr();
for (i = 1; c <= 4; i++)
{
if ((i % 2) != 0)
{
for (j = 1; j <= i; j++)

printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{
if (c == 4 && s == 1)
{
break;
}
printf("  ");
}
for (k = 1; k <= i; k++)
{
if (c == 4 && k == 5)
{
break;
}
printf("%2c", prnt);
}
printf("\n");
nos = nos - 4;
++c;
}
}
getch();
}


Wednesday, 5 June 2013

Program to check whether the given number is Adam Number or not


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