Frequently Asked Questions on Arrays and Pointers

Frequently Asked Questions on Arrays and Pointers, FAQ on Array and pointers, Pointers FAQ, FAQ on Array, Array Pointers interview Questions

  • I had the definition char a[6] in one source file, and in another I declared extern char *a. Why didn't it work?
  • But I heard that char a[] was identical to char *a.
  • So what is meant by the ``equivalence of pointers and arrays'' in C?
  • If they're so different, then why are array and pointer declarations interchangeable as function formal parameters?
  • So arrays are passed by reference, even though the rest of C uses pass by value?
  • Someone explained to me that arrays were really just constant pointers.
  • I'm still mystified. Is a pointer a kind of array, or is an array a kind of pointer?
  • I came across some ``joke'' code containing the ``expression'' 5["abcdef"] . How can this be legal C?
  • Since array references decay into pointers, if arr is an array, what's the difference between arr and &arr?
  • How do I declare a pointer to an array?
  • How can I set an array's size at run time?
  • How can I avoid fixed-sized arrays?
  • How can I declare local arrays of a size matching a passed-in array?
  • How can I dynamically allocate a multidimensional array?

Related Links :

strategy pattern in c++, strategy pattern Program in Cpp

strategy pattern in c++, strategy pattern Program in Cpp



#include
#include

using namespace std;

class CStrategyPattern {
public:
virtual ~CStrategyPattern() {
}
virtual string m_format(const string &, const string &) const=0;
};

class Formatter : public CStrategyPattern {
public:
string m_format(const string & s1, const string & s2) const
{
return s1 + "" + s2 + "";
}
};

void display_message(const CStrategyPattern & strategy){
cout << strategy.m_format("c", "cplusplus.com") << endl;
}

int main() {
display_message(Formatter());
return 0;
}


Related Links :

stream iterator in c++, program using stream iterator in C++

stream iterator in c++, program using stream iterator in C++



#include
#include
#include
#include
#include

using namespace std;

int main()
{
vector coll;
copy (istream_iterator(cin), //start of the source
istream_iterator(), //end of the source
back_inserter(coll)); //Final destination... :)

//To sort the elements
sort (coll.begin(), coll.end());
unique_copy (coll.begin(), coll.end(), //source
ostream_iterator (cout, "\n"));
//destination
return 0;
}


Related Links :

Unix shell script to get cpu usage

Unix shell script to get CPU usage



#!/bin/bash

id=0; tot=0; p_id=0; p_tot=0;

for ((i=0; $i < 100; ++i )) ; do

p_id=$id; p_tot=$tot;
x=`cat /proc/stat | grep -w cpu`;
id=`echo $x | cut -d " " -f 5`;
tot=0;
for f in `echo $x`; do
tot=$(($tot + $f));
done;

echo "idle CPU usage is % = "$((($id - $p_id)*100/($tot - $p_tot)));

sleep 3;
done



Related Links :

Unix shell script to kill a process by name

Unix shell script to kill a process by name




if [ "$1" == "" ];then
echo "Usage : ./pkill.sh "
else
for i in `ps ax | grep $1 | grep -v grep | sed 's/ *//' | sed 's/[^0-9].*//'`
do
kill -9 $i
done
fi


Related Links :

C Program TO FIND OR COUNT TOTAL NUMBER OF PALINDROME IN A GIVEN STRING

C Program TO FIND OR COUNT TOTAL NUMBER OF PALINDROME IN A GIVEN STRING


#include
#include
int stpal(char str[50], int st, int ed);
void main()
{
char str[50];
int pal = 0, len = 0, i, start = 0, end;
clrscr();
printf("\n\n\t ENTER A SENTENCE...: ");
gets(str);
while(str[len]!='\0')
len++;
len--;
for(i=0;i<=len;i++)
{
if((str[i] == ' ' && str[i+1] != ' ') || i == len)
{
if(i == len)
end = i;
else
end = i - 1;
if( stpal (str, start, end ) )
pal++;
start = end + 2;
}
}
printf("\n\n\t THE TOTAL NUMBER OF PALINDROMES ARE..: %d",pal);
getch();
}
int stpal(char str[50], int st, int ed)
{
int i, pal=0;
for(i=0; i<=(ed-st)/2; i++)
{
if(str[st+i] == str[ed-i])
pal = 1;
else
{
pal = 0;
break;
}
}
return pal;
}


Related Links :

C Program to print System date

C Program to print System date


#include
#include
#include

main()
{
struct date d;

getdate(&d);

printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);
getch();
return 0;
}



Related Links :

C Program trace path of Any WebSite, Traceroot using C language


#include

main()
{
char nm[30];
printf("Enter Any URL...");
scanf("%s",nm);
system("C:\\Windows\\System32\\tracert " & nm);
system("pause");

return 0;
}



Related Links :

C Program to Print IP address of your computer

C Program to Print IP address of your computer


#include

main()
{
system("C:\\Windows\\System32\\ipconfig");
system("pause");

return 0;
}



Related Links :

Shutdown Computers using C Program in Windows 7

Shutdown Computers using C Program in Windows 7


#include
#include

main()
{
char ch;

printf("Do you want to shutdown your computer now (y/n)\n");
scanf("%c",&ch);

if( ch == 'y' || ch == 'Y' )
system("C:\\WINDOWS\\System32\\shutdown /s");

return 0;
}






Well After creating EXE File, Run EXE File by right click on it & select option "Run as Administrator" then your computer will be shut down...

Related Links :

shutdown or turn off computer using C programming code for Windows XP

shutdown or turn off computer using C programming code for Windows XP


#include
#include

main()
{
char ch;
printf("Do you want to shutdown your computer now (y/n) ");
scanf("%c",&ch);
if( ch == 'y' || ch == 'Y' )
system("C:\\WINDOWS\\System32\\shutdown -s");
return 0;
}


Related Links :

adapter design pattern example in C++

adapter design pattern example in C++, adapter design in C plus plus, CSquareShapeAdapter Class in C++, using CSquareShapeAdapter in C++





#include

class CRoundShape
{
public:
virtual ~CRoundShape() { }
virtual void m_doRound() const=0;
};

class CSquareShape
{
public:
virtual ~CSquareShape() { }
virtual void m_doSquare() const=0;
};

class CRound_Hole
{
public:
void m_insert(const CRoundShape & shape) const
{
shape.m_doRound();
}
};

class CSquareShapeAdapter : public CRoundShape
{
private:
CSquareShape & square_shape;
public:
CSquareShapeAdapter(CSquareShape & shape) : square_shape(shape){ }
void m_doRound() const
{
square_shape.m_doSquare();
}
};

class CDisplayMessage : public CSquareShape
{
public:
void m_doSquare() const
{
std::cout << "ccplusplus.com" << std::endl;
}
};

void display_message(const CRoundShape & shape)
{
CRound_Hole().m_insert(shape);
}

int main()
{
CDisplayMessage shape;
display_message(CSquareShapeAdapter(shape));
return 0;
}


Related Links :

c++ Program to Sort Array in ascending order

c++ Program to Sort Array in ascending order


#include
#include

using namespace std;

class CSortNumbers {
private:
int i, j;
int arr[10];
public:
int m_swap(int *b,int *c ) {
int temp;
temp = *b;
*b = *c;
*c = temp;
return 0;
}
void m_takeInput () {
for ( i = 0; i < 10; i++ ) {
printf (" Enter the number %d : ", i + 1 );
scanf ( "%d", &arr[i] );
}
}
void m_display ( ) {
for ( i = 0; i < 10 ; i++ ) {
printf ( " %d " , arr[i] );
}
}
void m_sort () {
for ( i = 0; i < 10; i++ ) {
for ( j = 0; j < 10 - i - 1; j++ ) {
if ( * ( arr + j ) > * ( arr + (j + 1) )) {
m_swap ( arr + j , arr + (j + 1) );
}
}
}
}
};

int main()
{
CSortNumbers sn;
sn.m_takeInput ();
printf ("Before sorting:\n" );
sn.m_display ();
printf ( "\n" );
sn.m_sort();
printf ( "After sorting :\n" );
sn.m_display ();
printf ( "\n" );
return 0;
}



Related Links :

sort numbers in ascending order in C

sort numbers in ascending order in C


#include

int swapNumber (int*, int*);

int main()
{
int i, j;
int nNumArray[10];

for ( i = 0; i < 10; i++ ) {
printf (" Enter the number %d : ", i + 1 );
scanf ( "%d", &nNumArray[i] );
}

printf (" Before sorting:\n" );
for ( i = 0; i < 10 ; i++ ) {
printf ( " %d " , nNumArray[i] );
}
printf ( "\n" );

for ( i = 0; i < 10; i++ ) {
for ( j = 0; j < 10 - i - 1; j++ ) {
if ( * ( nNumArray + j ) > * ( nNumArray + (j + 1) )) {
swapNumber ( nNumArray + j , nNumArray + (j + 1) );
}
}
}
printf ( "After sorting :\n" );
for ( i = 0 ; i < 10 ; i++) {
printf ( "%d ", nNumArray [i] );
}

printf ( "\n" );
return 0;
}

int swapNumber(int *npFirst,int *npSecond )
{
int temp;
temp = *npFirst;
*npFirst = *npSecond;
*npSecond = temp;
return 0;
}



Related Links :

C Program to copy one file contents to another file

C Program to copy one file contents to another file



void main()
{
FILE *fp1, *fp2, *fopen();
int c ;
fp1 = fopen( "ccplusplus.com", "r" ); /* open for reading */
fp2 = fopen( "prog.old", "w" ) ; /* open for writing */
if ( fp1 == NULL ) /* check does file exist etc */
{
printf("Cannot open ccplusplus.com for reading \n" );
exit(1); /* terminate program */
}
else if ( fp2 == NULL )
{
printf("Cannot open prog.old for writing \n");
exit(1); /* terminate program */
}
else /* both files O.K. */
{
c = getc(fp1) ;
while ( c != EOF)
{
putc( c, fp2); /* copy to prog.old */
c = getc( fp1 ) ;
}
fclose ( fp1 ); /* Now close files */
fclose ( fp2 );
printf("Files successfully copied \n");
}
}


Related Links :

c program to sort given numbers in ascending order

c program to sort given numbers in ascending order



int swapNumber (int*, int*);

int main()
{
int i, j;
int nNumArray[10];

for ( i = 0; i < 10; i++ ) {
printf (" Enter the number %d : ", i + 1 );
scanf ( "%d", &nNumArray[i] );
}

printf (" Before sorting:\n" );
for ( i = 0; i < 10 ; i++ ) {
printf ( " %d " , nNumArray[i] );
}
printf ( "\n" );

for ( i = 0; i < 10; i++ )
{
for ( j = 0; j < 10 - i - 1; j++ )
{
if ( * ( nNumArray + j ) > * ( nNumArray + (j + 1) ))
{
swapNumber ( nNumArray + j , nNumArray + (j + 1) );
}
}
}
printf ( "After sorting :\n" );
for ( i = 0 ; i < 10 ; i++) {
printf ( "%d ", nNumArray [i] );
}

printf ( "\n" );
return 0;
}

int swapNumber(int *npFirst,int *npSecond )
{
int temp;
temp = *npFirst;
*npFirst = *npSecond;
*npSecond = temp;
return 0;
}


Related Links :

C Program On Pointers & 2 - Dimensional Array.

C Program On Pointers & 2 - Dimensional Array, 2D Array & Pointers



#include
#include

void input(int**, int, int);
void show(int**, int, int);

main()
{

int a[3][3];
int row = 3, col = 3;
clrscr();

input(( int** )a, row, col);
show(( int** )a, row, col);

getch();
return 0;

}

void input(int **p, int r, int c)
{

int i, j;
for( i = 0; i < r; i++ )
{
for( j = 0; j < c; j++ )
{
printf(" \n Enter p[%d][%d] element: ", i, j);
scanf(" %d ", *(p + i) + j);
}
}

}

void show(int **p, int r, int c)
{

int i, j;
printf(" \n Matrix is\n ");

for( i = 0; i < r; i++ )
{
for( j = 0; j < c; j++ )
printf(" %d ", *(*(p + i) + j));

printf(" \n ");

}

}


Related Links :

Floyd’s Triangle printing using C.

Floyd’s Triangle printing using C.


#include
#include
void main()
{

int n, i, sum = 0;
clrscr();

printf(" Enter any number: " );
scanf(" %d ", &n);

for(i = 1; i<n; i = i + 2 )
{
printf(" %d + ", i);
sum = sum + i;
}
printf(" %d ", n);
printf(" \nSum = %d ", sum + n );
getch();

}



Related Links :

C Program To Print Size Of Commonly Used Data Types.

C Program To Print Size Of Commonly Used Data Types.



#include
#include

main ()
{

clrscr();
printf ("\n An int is %d bytes", sizeof (int));
printf ("\n A char is %d bytes", sizeof (char));
printf ("\n A short is %d bytes", sizeof (short));

printf ("\n A long is %d bytes", sizeof (long));
printf ("\n A float is %d bytes", sizeof (float));
printf ("\n A double is %d bytes", sizeof (double));

printf ("\n An unsigned char is %d bytes", sizeof (unsigned char));
printf ("\n An unsigned int is %d bytes", sizeof (unsigned int));
return 0;

}

Related Links :

C Program To Accept Floating Point Numbers & Print Average.

C Program To Accept Floating Point Numbers & Print Average.



#include
#include

int main(void)
{

float a, b, c, d, avg=0;
clrscr();
printf("\n Enter the four floating point no's.\n");

scanf(" %f %f %f %f " ,&a, &b, &c, &d);
avg = ( a + b + c + d ) / 4;
printf("\n The average is = %10.5f",avg);
getch();
return 0;

}


Related Links :

Program to Concatenation Two Strings In C

Program to Concatenation Two Strings In C


#include
#include
void stcat(char *str1, char *str2);
void main()
{
char *str1, *str2;
clrscr();
printf("\n\n\t ENTER THE FIRST STRING...: ");
gets(str1);
printf("\n\n\t ENTER THE SECOND STRING...: ");
gets(str2);
stcat(str1,str2);
printf("\n\t THE CONCATENATED STRING IS...: ");
puts(str1);
getch();
}
void stcat (char *str1, char *str2)
{
int i = 0,len = 0;
while(*(str1+len)!='\0')
len++;
while(*(str2+i)!='\0')
{
*(str1+len) = *(str2+i);
i++;
len++;
}
*(str1+len) = '\0';
}




Related Links :

Program to Find HCF Using C Program

Program to Find HCF Using C Program

#include
#include
int hcf(int , int );
void main()
{
int a,b,c;
char ch;
do{

clrscr();
printf("\n Enter 1st no:");
scanf("%d",&a);
printf("\n enter 2nd no:");
scanf("%d",&b);
c=hcf(a,b);
printf(" \n <%d> and <%d> HCF value [%d]",a,b,c);
printf(" \n Continue (Y/N) :");
ch=getch();
}while(ch=='y'||ch=='Y');
}
int hcf(int x,int y)
{
int b,s,r;

if(x>y)
{
b=x;
s=y;
}
else
{
b=y;
s=x;
}
r=b%s;
while(r!=0)
{
b=s;
s=r;
r=b%s;
}
return s;
}




Related Links :

C Program To Multiply Two Matrix

C Program To Multiply Two Matrix, Multiplication of 3x3 Matrix in C


#include
#include
#include
main()
{
int a[3][3], b[3][3], c[3][3];
int i, j, k;
clrscr();
printf(" Enter elements of first matrix\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
printf(" \n Enter a[%d] [%d] element: ", i, j);
scanf(" %d ", &a[i] [j] );
}
}
printf(" Enter elements of second matrix\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
printf(" \n Enter b[%d] [%d] element: ", i, j );
scanf(" %d ", &b[i][j]);
}
}
printf(" \n\n First matrix is\n\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
printf(" %2d ", a[i][j]);
printf(" \n ");
}

printf(" \n\nSecond matrix is\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
printf(" %2d ", b[i][j]);
printf(" \n ");
}
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
c[i][j] = 0;
for( k = 0; k < 3; k++ )
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
printf(" \n\n Multiplication of two matrices is\n ");
for( i = 0; i < 3; i++ )
{
for(j = 0; j < 3; j++ )
printf(" %2d ", c[i][j]);
printf(" \n ");
}
getch();
}



Related Links :

C Program To Insert An Element In An Array, insertion in Array

C Program To Insert An Element In An Array, insertion in Array


#include
#include
#include
#define S 7

main()
{
int arr[10];
int i, pos, num, j;
clrscr();
for( i = 0; i < S; i++ )
{
printf("\n Enter arr[%d] element: ", i);
scanf(" %d ", &arr[i] );
}

clrscr();
printf(" \n\nArray is \n\n ");

for( i = 0; i < S; i++ )
printf(" %2d ", arr[i] );

printf(" \nEnter the element which you want to insert\n ");
scanf(" %d ", &num );
printf(" \nEnter the position (first is 0)\n " );
scanf("%d", &pos);

if( pos > = 0 && pos <= 9)
{
for( j = S;j > pos; j-- )
arr[j] = arr[j-1];

arr[j] = num;
}
else
{
printf(" Wrong position selected\n ");
getch();
exit(1);
}

printf(" Array after insertion\n ");
for(i = 0; i < S; i++ )
printf(" %d ", arr[i]);

getch();
}



Related Links :

C Program For Entering An Array Of Elements.

C Program For Entering An Array Of Elements.


#include
#include
#define S 5

main()
{
int a[S];
int i;
clrscr();
for( i = 0; i < S; i++ )
{
printf("\n Enter a[%d] element: ",i);
scanf("%d", &a[i]);
}
printf("Array elements are\n");
for( i = 0; i < S; i++ )
{
printf("a[%d] = %d\n", i, a[i]);
}
getch();
}

Related Links :

C Program To Find Transpose a 3 x 2 matrix.

C Program To Find Transpose a 3 x 2 matrix.



#include
#include
#include

main()
{

int arr[3] [2];
int i, j;
clrscr();

printf(" Enter the 6 elements of matrix\n ");
for( i = 0; i < 3; i++ )
for( j = 0; j < 2; j++ )
scanf(" %d ", &arr[i] [j]);

printf(" The matrix is\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 2; j++ )
printf(" %d ", arr[i] [j]);
printf(" \n ");
}

printf(" The Transposed Matrix is\n ");

for( i = 0; i < 2; i++ )
{
for( j = 0; j < 3; j++ )
printf(" %d ", arr[j] [i]);
printf("\n");
}

getch();

}


Related Links :

c program to find out the sum of G.P series

c program to find out the sum of G.P series, GP Series in C, C Program to find G.P. Series.


#include
#include

int main(){

float a,r,i,tn;
int n;
float sum=0;

printf("Enter the first number of the G.P. series: ");
scanf("%f",&a);

printf("Enter the total numbers in the G.P. series: ");
scanf("%d",&n);

printf("Enter the common ratio of G.P. series: ");
scanf("%f",&r);

sum = (a*(1 - pow(r,n+1)))/(1-r);
tn = a * (1 -pow(r,n-1));

printf("tn term of G.P.: %f",tn);
printf("\nSum of the G.P.: %f",sum);

return 0;
}



Related Links :

c program to find the area of a trapezium

c program to find the area of a trapezium , Area of trapezium in C, C Language to find trapezium area.




#include

int main(){

float b1,b2,h;
float area;

printf("Enter the size of two bases and height of the trapezium : ");
scanf("%f%f%f",&b1,&b2,&h);

area = 0.5 * ( b1 + b2 ) * h ;

printf("Area of trapezium is: %.3f",area);

return 0;
}


Related Links :

C Program On Pointers and 2 - Dimensional Array.

C Program On Pointers and 2 - Dimensional Array.



#include
#include
void main ()
{
int i, total;
int arr[10];
int *a;
a = arr;
for(i=0;i<10;i++ )
{
printf ("Enter the number %d:",i+1);
scanf ("%d",&arr[i] );
}
for(i=0;i<10;i++)
{
printf("%d---",*a);
total=total+*a;
a=a+1;
}
printf("\nTotal = %d \n",total);
}


Related Links :

C Program To Accept & Add Ten Numbers Using Pointers.

C Program To Accept & Add Ten Numbers Using Pointers.



#include
#include

void main ()
{

int i, total;
int arr[10];
int *a;

a = arr;
for(i=0;i<10;i++)
{
printf ("Enter the number %d:", i+1 );
scanf ("%d", &arr[i] );
}

for(i=0;i<10;i++)
{
printf ( " %d--- ", *a );
total = total + *a;
a = a + 1;
}

printf ("\nTotal = %d \n",total);
getch();

}



Related Links :

using binary minus operator Write a c program to find largest among three numbers

using binary minus operator Write a c program to find largest among three numbers


#include
int main()
{
int a,b,c;
printf("\n Please Enter the 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a-b>0 && a-c>0)
printf("\n Greatest is a :%d",a);
else
if(b-c>0)
printf("\n Greatest is b :%d",b);
else
printf("\n Greatest is c :%d",c);
return 0;
}



Related Links :

c program to subtract two numbers without using subtraction operator

c program to subtract two numbers without using subtraction operator


#include

int main(){

int a,b;
int sum;

printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

sum = a + ~b + 1;

printf("Difference of two integers: %d",sum);

return 0;
}

Related Links :

c program to add two numbers without using addition operator

c program to add two numbers without using addition operator





#include

int main()
{

int a,b;
int sum;

printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

//sum = a - (-b);
sum = a - ~b -1;

printf("Sum of two integers: %d",sum);

return 0;
}



Well Friend the Algorithm of this program is as follows :


Algorithm:
In c ~ is 1's complement operator. This is equivalent to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b


Related Links :

C program to print perfect numbers from 1 to 100

#include
int main(){
int n,i,sum;

printf("Perfect numbers are: ");
for(n=1;n<=100;n++){
i=1;
sum = 0;

while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}

if(sum==n)
printf("%d ",n);
}

return 0;
}






Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number.

Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128

Related Links :

C program to check perfect number Or Not

C program to check perfect number Or Not


#include
int main(){
int n,i=1,sum=0;

printf("Enter a number: ");
scanf("%d",&n);

while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);

return 0;
}



Related Links :

Sum of diagonal elements of a matrix in c


#include

int main(){

int a[10][10],i,j,sum=0,m,n;

printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);

printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");

for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\n Sum of the diagonal elements of a matrix is: %d",sum);

return 0;
}



Related Links :

Program on array manipulations using c, array manipulations in C, C array manupulations

Program on array manipulations using c, array manipulations in C

#include

main()

{

int a[20],b[20],n,i,j,temp,sb,ss,sbp,ssp;

clrscr();

printf("ENTER HOW MANY ELEMENTS\n");

scanf("%d",&n);

printf("ENTER %d ELEMENTS\n",n);

for (i=0;i

scanf("%d",&a[i]);

for (i=0;i

b[i]=a[i];

for (i=0;i

{

for (j=i+1;j

if (a[i]

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}
}
sb=a[1];
ss=a[n-2];
for (i=0;i
{
for (j=i+1;j
{
if (b[i]==sb)
sbp=i;
if (b[i]==ss)
ssp=i;
}
}
b[sbp]=ss;

b[ssp]=sb;

printf("AFTER INTERCHANGING THE SECOND BIGGEST ELEMENT AND\n");

printf("THE SECOND SMALLEST ELEMENT THE ARRAY IS\n");

for (i=0;i

printf("%d\n",b[i]);

getch();

}











Related Links :

C Program to find out the given number is automorphic or not

C Program to find out the given number is automorphic or not, Automorphism Numbers in C, automorphic Numbers finding in C



#include

main()

{

int n;

clrscr();

puts("ENTER ANY NUMBER");

scanf("%d",&n);

if (n*n%10==n || n*n%100==n || n*n%1000==n)

puts("THIS IS AN AUTOMORPHIC NUMBER");

else

puts("THIS IS NOT AN AUTOMORPHIC NUMBER");

getch();

}


Related Links :

C Program to find the biggest of the given numbers

C Program to find the biggest of the given numbers

#include

main()

{

int a[30],big=0,n;

register int i;

clrscr();

printf("HOW MANY NUMBERS YOU WANT TO ENTER\n");

scanf("%d",&n);

printf("ENTER %d NUMBERS\n",n);

for (i=0;i

scanf("%d",&a[i]);

for (i=0;i

{

if (big

big=a[i];

}

printf("THE BIGGEST ELEMENT IS %d",big);

getch();

}






Related Links :

C Program to print the multiplication table

C Program to print the multiplication table



#include

main()

{

int n,m;

register int i;

clrscr();

printf("WHICH TABLE YOU WANT\n");

scanf("%d",&n);

printf("UPTO WHICH NUMBER\n");

scanf("%d",&m);

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

printf("%d * %d = %d\n",n,i,n*i);

getch();

}



Related Links :

Program to read 9 elements and print them in 3 x 3 matrix form using c Language

Program to read 9 elements and print them in 3 x 3 matrix form using c Language


#include

main()

{

int a[9],c=0;

register int i;

clrscr();

printf("ENTER 9 ELEMENTS\n");

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

scanf("%d",&a[i]);

printf("THE ELEMENTS IN 3*3 MATRIX FORM IS\n");

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

{

printf("%2d",a[i]);

c++;

if (c%3==0)

printf("\n");

}

getch();

}



Related Links :

C program to print Pascal triangle using for loop, Pascal triangle program in c language

Pascal triangle program in c language



#include

long fact(int);
int main(){
int line,i,j;

printf("Enter the no. of lines: ");
scanf("%d",&line);

for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");

for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}

long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}

Related Links :

using binary minus operator find largest among three numbers in C

using binary minus operator find largest among three numbers in C

#include
int main(){
int a,b,c;
printf("\nEnter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a-b>0 && a-c>0)
printf("\nGreatest is a :%d",a);
else
if(b-c>0)
printf("\nGreatest is b :%d",b);
else
printf("\nGreatest is c :%d",c);
return 0;
}





Related Links :

Convert string to int without using library functions in c

Convert string to int without using library functions in c


#include

int stringToInt(char[] );
int main()
{
char str[10];
int intValue;
printf("Please Enter any integer as a string: ");
scanf("%s",str);
intValue = stringToInt(str);
printf("Equivalent integer value: %d",intValue);
return 0;
}

int stringToInt(char str[])
{
int i=0,sum=0;
while(str[i]!='\0'){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else
{
sum = sum*10 + (str[i] - 48);
i++;
}

}

return sum;

}



Related Links :

c Program to print 1 to 100 without using loop

c Program to print 1 to 100 without using loop


#include

int main(){
int i= 1;

print(i);

return 0;
}
int print(i){
if(i<=100){
printf("%d ",i);
print(i+1);
}
}




Related Links :

C Program to calculate Electric Bill

C Program to calculate Electric Bill


#include
#include
void main()
{
int cno,pmr,cmr,cu;
float total;
char sec;
char cname[10];
clrscr();
printf(" ENTER the SECTOR : ");
scanf("%c",&sec);
printf(" Enter customer name : ");
scanf("%s",&cname);
printf(" Enter customer number : ");
scanf("%d",cno);
printf(" Enter the previous units : ");
scanf("%d",&pmr);
printf(" Enter the current units : ");
scanf("%d",&cmr);
cu=cmr-pmr;
if(sec == 'a')
{
if(cu>300)
total=cu*2.00;
if(cu>200 && cu<=300)
total=cu*1.50;
if(cu>100 && cu<=200)
total=cu*1.00;
if(cu<=100)
total=cu*0.50;
}
if(sec=='i')
{
if(cu>300)
total=cu*4.00;
if(cu>200 && cu<=300)
total=cu*3.00;
if(cu>100 && cu<=200)
total=cu*2.00;
if(cu<=100)
total=cu*1.00;
}
if(sec == 'a')
{
if(cu>300)
total=cu*2.00;
if(cu>200 && cu<=300)
total=cu*1.50;
if(cu>100 && cu<=200)
total=cu*1.00;
if(cu<=100)
total=cu*0.50;
}
if(sec == 'd')
{
if(cu>300)
total=cu*2.00;
if(cu>200 && cu<=300)
total=cu*1.50;
if(cu>100 && cu<=200)
total=cu*1.00;
if(cu<=100)
total=cu*0.50;
}
printf("\n Customer name : %s ",cname);
printf("\n Customer number : %d",cno);
printf("\n Previous Units : %d",pmr);
printf("\n Current Units : %d",cmr);
printf("\n Category : %c",sec);
printf("\n Consumed Units : %d",cu);
printf("\n Electric Bill : %f",total);
getch();
}





Related Links :

C Program To Print Sum Of Series 1 + 3 + 5 + …. N.

C Program To Print Sum Of Series 1 + 3 + 5 +.... N


#include
#include
void main()
{

int n, i, sum = 0;
clrscr();

printf(" Enter any number: " );
scanf(" %d ", &n);

for(i = 1; i<n; i = i + 2 )
{
printf(" %d + ", i);
sum = sum + i;
}
printf(" %d ", n);
printf(" \nSum = %d ", sum + n );
getch();

}



Related Links :

C Program To Display The Days In A Week using Switch Case

C Program To Display The Days In A Week.



#include
#include
void main()
{

char ch;
clrscr();
printf(" Enter m for Monday\n t for Tuesday\n w for Wednesday\n h for Thursday\n f for Friday\n s for Saturday\n u for Sunday ");

scanf(" %c ", &ch);

switch(ch)
{
case 'm':
case 'M':
printf("The day is Monday");
break;

case 't':
case 'T':
printf("The day is Tuesday");
break;

case 'w':
case 'W':
printf("The day is Wednesday");
break;

case 'h':
case 'H':
printf("The day is Thursday");
break;

case 'f':
case 'F':
printf("The day is Friday");
break;

case 's':
case 'S':
printf("The day is Saturday");
break;

case 'u':
case 'U':
printf("The day is Sunday");
break;

default :
printf("Wrong input!");
break;
}
getch();

}



Related Links :

C Program To Find The Reverse Of A Number.

C Program To Find The Reverse Of A Number.


#include
#include

void main()
{

int n, a, r=0;
clrscr();

printf(" Enter any number to get its reverse: ");
scanf("%d",&n);
while(n>=1)
{
a = n % 10;
r = r * 10 + a;
n = n / 10;
}
printf(" Reverse = %d",r);
getch();

}




Related Links :

C Program To Convert Celsius Temperature To Fahrenheit.

C Program To Convert Celsius Temperature To Fahrenheit.



#include
#include

void main()
{

float c, f;
clrscr();

printf(" Please Enter Temperature in centigrade: ");
scanf("%f",&c);

f = ( 1.8 * c ) + 32;
printf(" Temperature in Fahrenheit = %f", f);
getch();

}


Related Links :

C Program To Accept Floating Point Numbers & Print Average.

C Program To Accept Floating Point Numbers & Print Average.



#include
#include

int main(void)
{

float a, b, c, d, avg=0;
clrscr();
printf("\n Enter the four floating point no's.\n");

scanf(" %f %f %f %f " ,&a, &b, &c, &d);
avg = ( a + b + c + d ) / 4;
printf("\n The average is = %10.5f",avg);
getch();
return 0;

}

Related Links :

C Program To Sort Numbers In Ascending Numbers.


#include
#include

swap (int*, int*);

main()
{

clrscr();
int i, j;
int arr[10];
int *a;
a=arr;

for ( i = 0; i < 10; i++ )
{
printf (" Enter the number %d : ", i + 1 );
scanf ( "%d", &arr[i] );
}

printf (" Before sorting:\n" );

for ( i = 0; i < 10 ; i++ )
printf ( " %d " , arr[i] );

printf ( "\n" );

for ( i = 0; i < 10; i++ )
for ( j = 0; j < 10 - i - 1; j++ )
if ( * ( arr + j ) > * ( arr + (j + 1) )
swap ( arr + j , arr + (j + 1) );

printf ( "After sorting :\n" );

for ( i = 0 ; i < 10 ; i++)
printf ( "%d ", arr [i] );

printf ( "\n" );
return 0;

}

swap(int *b,int *c )
{

int temp;
temp = *b;
*b = *c;
*c = temp;
return 0;

}



Related Links :

C Program to Print Memory Size of Variable

C Program To Specify Size Of Commonly Used Data Types, C Program to Find Memory size Require for Data types, Program to Print Memory Size of Variable.



#include
#include

main ()
{

clrscr();
printf ("\n An int is %d bytes", sizeof (int));
printf ("\n A char is %d bytes", sizeof (char));
printf ("\n A short is %d bytes", sizeof (short));

printf ("\n A long is %d bytes", sizeof (long));
printf ("\n A float is %d bytes", sizeof (float));
printf ("\n A double is %d bytes", sizeof (double));

printf ("\n An unsigned char is %d bytes", sizeof (unsigned char));
printf ("\n An unsigned int is %d bytes", sizeof (unsigned int));
return 0;

}


Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!