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 For Loop. Show all posts
Showing posts with label For Loop. 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();
}


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



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


Thursday, 25 July 2013

Program to print the String pattern 2

#include<stdio.h>
int main()
{
char *c="ABCDEF";
char *c1="FEDCBA";
int i,j=0;
clrscr();
for(i=6;i>0;i--)
{
printf("%*.*s",i,i,c);
printf("%*.*s\n",i,i,c1+j);
j++;
}
getch();
}


Program to print the Star pattern 6 (Right-angled Triangle)

#include <stdio.h>
void main()
{
int n, c, k;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("*");
printf("\n");
}
getch();
}




Tuesday, 2 July 2013

Program to find the length of the string without using strlen() function

#include <stdio.h>
void main()
{
char s[1000],i;
clrscr();
printf("Enter a string: ");
scanf("%s",s);
for(i=0; s[i]!='\0'; ++i);
printf("Length of string: %d",i);
getch();
}


Program to print the Floyd's Triangle

#include<stdio.h>
int main()
{
int i,j,n,k=1;
clrscr();
printf("Enter the number of rows of Floyd's triangle: ");
scanf("%d",&n);
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
getch();
}


Program to print the String pattern 1

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[100];
int i;
cout<<"Enter the string:";
cin>>str;
for(i=0;i<=strlen(str);i++)
{
cout.write(str,i);
cout<<"\n";
}
getch();
}


Monday, 1 July 2013

Program to print the Star pattern 5 (Diamond)

#include <stdio.h>
void main()
{
int n, c, k, space = 1;
clrscr();
printf("Enter the number of rows in Diamond:");
scanf("%d", &n);
space = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= 2*k-1; c++)
printf("*");
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");
printf("\n");
}
getch();
}


Friday, 28 June 2013

Program to print the Star pattern 4 (Pyramid)

#include <stdio.h>
void main()
{
int row, c, n, temp;
clrscr();
printf("Enter the number of rows in Pyramid: ");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}
getch();
}