the differences between structures and unions.

i) The amount of memory required to store a structure variable is the sum of all members. On the other hand, in the case of unions, the amount of memory required is always equal to that required by its largest member.

ii) Only one member of a union can be accessed at any given time, whereas this is not the case with structures.

Related Links :

What are types of I/O functions ?

The main three types of I/O functions are
1) Console I/O functions
2) Disk I/O functions
3) Port I/O functions.

Related Links :

What is recursion in c?

When a function is called from body of itself then the function is called recursive function and the process is called as recursion.


Related Links :

What is size of () operator.

The size of () operator is a library operator to find the size of variable, structure, etc. in bytes.

Related Links :

What is type casting?

Type casting in 'C' is the process by which one type of data forcibly converted into another type though is same cases, the Conversion from one type to another takes place automatically, there may be cases where the programmer forces the conversion of a data type to another.

Related Links :

What is difference between user defined function and library function ?

The user-defined functions are defined by a user as per its own requirement and library functions come with compiler.

Library Function User Defined Functions
  • LF(library functions) are Predefined functions.
  • UDF(user defined functions) are the function which r created by user as per his own requirements.
  • UDF are part of the program which compile runtime
  • LF are part of header file
    (such as MATH.h) which is called runtime.
  • In UDF the name of function id decided by user
  • in LF it is given by developers.
  • in UDF name of function can be changed any time
  • LF Name of function can't be
    changed.
Example : SIN, COS, Power Example : fibo, mergeme

Related Links :

What are various types of operators in 'C'?

1) Arithmetic operators
2) Logical operators
3) Relational operators
4) Unary operators
5) Conditional operators
6) Pointer operators
7) Structure operators.
8) Bitwise operators.

Related Links :

What are various storage classes in 'C' ?

There are four types of storage classes available in 'C'.
1) Automatic storage class
2) Register storage class
3) Static storage class
4) External storage class.

Related Links :

A 'C' program to reverse the content of an array without using additional array.

A 'C' program to reverse the content of an array without using additional array.



main()
{
int i, t,n,s; int a[30];
clrscr();
printf("Enter the number of elements in 1D array");
scanf("%d",&n);
printf("Enter the elements of an array");
for (i=0; i

Related Links :

If a five digit number is input throught the keyboard, write a program to calculate sum of its digits. Use % operator and do not use any loops.



main ()
{
long int n,d1,d2,d3, d4,d5, sum;
printf ("\n Enter the five digit number");
scanf ("%d",&n);
d1=n%10;
n=n/10;
d2=n%10;
n=n/10;
d3=n%10;
n=n/10;
d4=n%10;
n=n/10;
d5=n%10;
sum=d5 * 10000 + d4 * 1000 + d3 * 100 + d2 * 10 + d1;
printf("\n sum of digits is:%/d",sum);
}

Related Links :

Explain difference between while and do-while loop.

The syntax of while loop is

while(expression)
{
statement;
statement;
}


the syntax of do while loop is

do
{

statement;
statement;
}
while(expression);


From the above syntax it is clear that in while loop expression is tested first and then the body is executed.
If expression is evaluated to true it is executed otherwise not.


In do while loop, body is executed first and then the expression is tested. If test expression is evaluated to true then body of loop is again executed.

Thus it is clear that the body of do while loop gets executed at least once, where as, it is not necessary in while loop.

Related Links :

Program to print binary equivalent of a decimal number Note MSP should print first and LSB last.

main()
{
int n;
printf("\n Enter any number:");
scanf("%d", &n); dec2bin(n);
}


dec2bin(int n)
{
if(n == 0)
return ; else
{
dec2bin (n/2);
printf("%d", n%10);
}
}

Related Links :

Write a recursive function to find sum of digits of any number input through keyboard.

main()
{
int s, n;
printf("\nEnter any number:");
scanf("%d",&n);
s =sum(n);
printf("\n Sum of digits = %d",s);
}

sum(int n)
{
if(n<10)
return(n); else
return(n %10 + sum(n/10)) ;
}


Recursive function is generally used when basic process itself is reverse. For example conversion of decimal to binary. The remainders are printed is reverse order of occurrence.

Ex.
2 23 (10111)2
2111
2 5 11 2 2 1
2 1 0
0 |l

Related Links :

Whats is Defination of function.

A function is a subroutine that may include one or more statements to perform a specific task.

To write a 'C' program, we first create functions and then put them together.

'C' function can be classified into two categories namely library functions and user defined functions main() is an example of user defined functions and scanf() and printf() belongs to category of library functions. Same other examples of library functions are sqrt(), cos(), strcpy(), strcmp() etc. The main difference between library functions and user defined functions is that library functions are not required to be written by user whereas a user defined function has to be developed by the user at the time of writing a program.

Library functions are available to all users but user defined function must be defined and used in only a particular program and it is not accessible by all. Library functions are available in compiled object code form and cannot be viewed by user.

Related Links :

What is meant by explicity type casting ? When is it useful?

Type casting in C is the process by which one type of data is forcibly converted into another type though is same cases, the conversion from one type to another takes place automatically, there may be cases where the programmer forces the conversion of a data type to another. The later case is called explicit type casting. As for example let 'i' and 'j' be two integer variables and 'k' a float variable.


Thus


i=15; j=14; k=i/j; results is k = 3.0 (though 15/4 =3.75 ) as happens in integer-integer division. To assign 3.75 to 'k' would require type casting of one of 'i' and 'j' to float like k = (float)i/j; or k=i/(float)j;.

Type casting are useful in suitably casing a 'void' pointer to the proper type before use. (A 'void' pointer is a pointer that has no particular data type attached to it, and thus no pointer arithmetic applicable to it, though it can point to any data types.) The standard memory allocating function malloc() returns a void pointer pointing to the first block, which therefore needs to be suitably type casted before use, like


int *ptr;

ptr=(int*)malloc(10*sizeof(int));

Related Links :

Defination Identifiers and Keywords.

Identifiers are the variable names given to locations in the memory of computer where different constant are stored. These locations may be integer, real or character constants. By default the length of identifier is 32 characters.

Keywords are the words whose meaning has already been explained to the C compiler. These keywords can not be used as variable names because if we do so, we are trying to assign new meaning to the keyword, which is not allowed by the C compiler. There are also called as Reserved Words. There are only 32 keywords in C.

Related Links :

Briefly describe about precautions a programmer should take while using 'if'?




main()
{
int a;
printf("Enter a integer value for a:");
scanf("%d", &a);
if(a=15)
printf("Value of a is l5.");
else
printf("Value of a is other than 15.");
}

Related Links :

Write a program to display the entered decimal number into octal and hexadecimal number system.



#include "stdio.h"

main()
{
int x;
clrscr(); /* to clear the screen */

printf("Enter any decimal number");
scanf("%d", &x);
printf("\n in decimals %d ",x);

printf("\n in octal %o",x);

printf("\n in hexadecimal %x",x);
}



Output: If x = 10, the output will be
in decimals 10
in octal 12



in hexadecimal a
If x = 152,


the output will be


in decimals 152
in octal 230
in hexadecimals 98.


Related Links :

Project of ATM Banking System in C


#include
#include
char x;
int pin, repeat, menu;
int withdraw, deposit, transfer, amount;
main()


{ clrscr();
gotoxy(20,1);printf(":--------------------------------------:\n");
gotoxy(20,2);printf(": Welcome to ATM Banking System:\n");
gotoxy(20,3);printf(": Created by:\n");
gotoxy(20,4);printf(": ivan john Navasca:\n");
gotoxy(20,5);printf(": :\n");
gotoxy(20,6);printf(": Enter your PIN -->:\n");
gotoxy(20,7);printf(":--------------------------------------:\n");
gotoxy(40,6);
scanf(" %d",&pin);
if (pin==1234)
{printf("access granted");}
else
{exit();}
Loop:


{clrscr();
printf(":-------------------------------------------:\n");
printf(": Choose your Transaction :\n");
printf("::\n");
printf(": 1 Inquiry Balance :\n");
printf(": 2 Withdraw:\n");
printf(": 3 Deposit :\n");
printf(": 4 Transfer Fund:\n");
printf(": 5 Exit:\n");
printf(": -->:\n");
printf(":-------------------------------------------:\n");
gotoxy(6,9);
scanf(" %d",&menu);
printf("\n\n");
switch(menu)


{
case 1:{printf("Your balance %d\n\n",amount);
printf("Another transaction? y or n?: ");
scanf(" %c",&x);
if((x == 'y')||(x== 'Y'))


{
goto Loop;
}
else if((x =='n')||(x== 'N'))


{
printf("\nThank You for Banking");
getch();
exit();
}
break;}
case 2:{if(amount==0)
{printf("You have an Isuficient Fund\nPress Enter");
getch();
goto Loop;
}
else

{
printf("Your Balance %d\n\n",amount);
printf("Enter Amount to Withdraw: ");
scanf("%d",&withdraw);
amount = amount - withdraw;
printf("\nYour Current Balance %d\n\n",amount);
printf("Another Transaction? y or n : ");
scanf(" %c",&x);
if((x =='y')||(x=='Y'))


{
goto Loop;
}
else if ((x =='n')||(x=='N'))


{
printf("\nThank You for Banking");
getch();
exit();
} }
break;}
case 3:{printf("Your Balance %d\n",amount);
printf("\nEnter Amount to Deposit: ");
scanf("%d",&deposit);
amount= amount + deposit;
printf("\nYour Current Balance %d\n",amount);
printf("Another Transaction? y or n: ");
scanf(" %c",&x);
if ((x =='y')||(x=='Y'))


{
goto Loop;
}
else if ((x =='n')||(x=='N'))


{
printf("\n\nThank You for Banking");
getch();
exit();
}
break;}
case 4:{printf("Your Balance %d\n",amount);
printf("\nEnter Amount to Transfer: ");
scanf(" %d",&transfer);
amount = amount - transfer;
printf("\nYour Current Balance %d",amount);
printf("\n\nAnother Transaction? y or n: ");
scanf(" %c",&x);
if ((x =='y')||(x=='Y'))


{
goto Loop;
}
else if ((x =='n')||(x=='N'))


{
printf("Thank You for Banking");
getch();
exit();
}
break;}
case 5:{printf("You Want to Exit? y or n: ");
scanf(" %c",&x);
if ((x=='y')||(x=='Y'))


{
printf("\n\nThank You & Come Again");
getch();
exit();
}
else if ((x =='n')||(x=='N'))


{
goto Loop;
}
break;}
default:
exit();
}
}

while(repeat);
getch();
}





Related Links :

Insertion of Array



#include

#define length(x) (sizeof x / sizeof *x)

int insert(int *array, int val, size_t size, size_t at)
{
size_t i;

/* In range? */
if (at >= size)
return -1;

/* Shift elements to make a hole */
for (i = size - 1; i > at; i--)
array[i] = array[i - 1];
/* Insertion! */
array[at] = val;

return 0;
}

int main(void)
{
int a[11] = {0,1,2,3,4,5,6,7,8,9};
int i;

if (insert(a, 6, length(a), 3) != -1)
{

for (i = 0; i < length(a); i++)
printf("%-3d", a[i]);
printf("\n");
}
else
fprintf(stderr, "Insertion failed\n");
return 0;
}

Related Links :

BASE-CONVERSION Program In C BINARY CONVERSION, OCTAL CONVERSION,HEXA-DECIMAL CONVERSION

BINARY CONVERSION, OCTAL CONVERSION,HEXA-DECIMAL CONVERSION



#include
#include
int c,r,f,i[16],j,rw,cl;
void main(void)


{
int num,a;
char ch;
void bin(int);
void hex(int);
void oct(int);
do

{
j=15; rw=0; cl=3;
for(f=0;f<=j;f++)
i[f]=0;
clrscr();

fflush(stdin);
printf("\n\n\t\t\t\t BASE-CONVERSION");
printf("\n\t\t\t\t ===============\n");
printf("\n\n\t\t 1. BINARY CONVERSION ");
printf("\n\t\t 2. OCTAL CONVERSION ");
printf("\n\t\t 3. HEXA-DECIMAL CONVERSION ");
printf("\n\t\t 4. EXIT ");
printf("\n\n\t\t\t Enter Ur choice = ");
scanf("%d",&a);
fflush(stdin);
switch(a)
{
case 1:
printf("\n\n\t\tEnter a number = ");
scanf("%d",&num);
fflush(stdin);
bin(num);
break;

case 2:
printf("\n\n\t\tEnter a number = ");
scanf("%d",&num);
fflush(stdin);
oct(num);
break;

case 3:
printf("\n\n\t\tEnter a number = ");
scanf("%d",&num);
fflush(stdin);
hex(num);
break;

case 4:
clrscr();
exit(0);
}

printf("\n\n\n\n\n\t do U want to continue...(y/n) = ");
fflush(stdin);
ch=getch();
fflush(stdin);
}
while(ch=='y'||ch=='Y');
clrscr();
}
void bin(int no)
{
c=no/2;
r=no%2;
i[j--]=r;
if(c<2)
{
i[j--]=c;
printf("\n\n\t\t\t Ans = ");
for(f=0;f<=15;f++)
{
printf("%d",i[f]);
if(rw==cl)
{
printf(" ");
cl=cl+4;
}
rw++;
}
}
else
bin(c);
}
void oct(int no)
{
c=no/8;
r=no%8;

i[j--]=r;
if(c<8)
{
i[j--]=c;
printf("\n\n\t\t\t Ans = ");

for(f=0;f<=15;f++)

{

printf("%d",i[f]);

if(rw==cl)

{
printf(" ");
cl=cl+4;
} rw++; } } else oct(c); } void hex(int no) { c=no/16; r=no%16; i[j--]=r; if(c<16) ans = "); for(f=0;f<=15;f++) { if(i[f]==10) printf(" rw="="cl)" cl="cl+4;">

Related Links :

Program to print Pyramid of Numbers.




#include
#include
void main(){
int i,j;
clrscr();
for(i=1;i<=5;i++){ for(j=1;j<=i;j++){ printf(" %d",i); } printf("\n"); } getch(); }

Related Links :

Program to Print Pyramin of asterik / Star.


#include
#include
void main(){
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

Related Links :

Program to Reverse String & Count Letters in String.




#include
#include
#include
void main(){
int len;
char s[15];
clrscr();
printf("Enter a String : - ");
scanf("%s",s);
len=strlen(s);
printf("\n Length of String - %d ",len);
printf("\n Original String :- %s",s);
strrev(s);
printf("\n Reverse String :- %s",s);
getch();
}




Related Links :

Identify String is Palindrum or Not




#include
#include
#include
void main(){
int len;
char s[15],temp[15];
clrscr();
printf("Enter a String : - ");
scanf("%s",s);
strcpy(temp,s);
len=strlen(s);
printf("\n Length of String - %d ",len);
printf("\n Original String :- %s",s);
strrev(s);
printf("\n Reverse String :- %s",s);
if(strcmp(temp,s)==0){
printf("\n \tString is Palindrum ");
}else{
printf("\n \tString is not Palindrum ");
}
getch();
}



Related Links :

Program to print string Vertically from given string

Program to print string Vertically from given string.


#include
#include
void main(){
int i;
char string[5]="HELLO";
clrscr();
for(i=0;i<5;i++)
{
printf("\n %c",string[i]);
}
getch();
}



Related Links :

Program to show use of arithmetic operator (Addition,Multiplication,subtraction,division)

Program to show use of arithmetic operator (Addition,Multiplication,subtraction,division)




#include
#include
void main()
{
int a,b,result;
printf("\n Enter two numbers : - ");
scanf("%d %d",&a,&b);
result=a+b;
printf("\n Addition of %d and %d is %d ",a,b,result);
result=a-b;
printf("\n Substraction of %d and %d is %d ", a,b,result);
result=a*b;
printf("\n Multiplication of %d and %d is %d ",a,b,result);
result=a/b;
printf("\n Division of %d and %d is %d ",a,b,result);
getch();

}



Output :

Related Links :

Program to Sort the Marks Of Given Subject in C by using User Defined Function


Program to Sort the Marks Of Given Subject in C by using User Defined Function.


#include
void sort(int m, int x[]);
main()
{
int i;
int marks[5]={40, 90, 73, 81, 35};
clrscr();
printf("Marks before sorting\n");
for(i=0; i<5; i="0;" i="1;" j="1;j<=">=x[j]
{
temp=x[j-1];
x[j-1]=x[j];
x[j]=temp;
}
}
}
}






Related Links :

Program to calculate ratio and difference between given numbers



#include
float ratio(int x, int y, int z);
int difference(int x, int y);
main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%f \n", ratio(a,b,c));
getch();
}

float ratio(int x, int y, int z)
{
if(difference(y,z))
return(x/(y-z));
else
return(0.0);
}

int difference(int p, int q)
{
if(p!=q)
return(1);
else
return(0);
}




Related Links :

Program To calculate Salesman's Salary by using 2Dimension Arrays



#include
#define MAXSALESMAN 4
#define MAXITEMS 3
main()
{
int value[MAXSALESMAN][MAXITEMS];
int salesman_total[MAXSALESMAN], item_total[MAXITEMS];
int i, j, grand_total;
clrscr();
printf("Input data\n");
printf("Enter values, one at a time, row-wise\n\n");
for(i=0; i

Related Links :

Program to Find Real Numbers & Total Of Real Numbers..


#include
main()
{
int i;
float x[10], value, total;
clrscr();
printf("Enter 10 Real Numbers\n");
for(i=0; i<10;i++)
{
scanf("%f", &value);
x[i]=value;
}
total=0.0;
for(i=0;i<10;i++)
total+=x[i]*x[i];
printf("\n");
for(i=0;i<10;i++)
printf("x[%2d] = %5.2f\n", i+1, x[i]);
printf("\nTotal = %.2f\n", total); getch();
}

Related Links :

Program to find largest & smallest number from given numbers



#include
main()
{ int count=0;
float value, high, low, sum=0, average, range;
clrscr();
printf("Enter numbers in a line: input a NEGAATIVE number to end\n");
input:
scanf("%f",&value);
if(value<0)
goto output;
count+=1;
if(count==1)
high=low=value;
else if(value>high)
high=value;
else if(valuelow="value;"
goto=""
average="sum/count;"
total=""
range="high-low;"
naverage=""
n="">

Related Links :

Program to create Simple Structure



#include
main( )
{
struct {
char name [20] ;
int age ;
int test ;
int run ;
} play ;

FILE *p ;
char ch =’y’;
p =fopen ("rec.dat", "w") ;
while (ch = =’y’)
{
printf ("Name") ;
scanf("%s", play.name) ;
printf ("Age") ;
scanf("%d, &play.age) ;
printf ("Number of test played”) ;
scanf("%d", &play.test) ;
printf ("Total run") ;
scanf("%d", &play.run) ;
fprintf(p, "%s%d%d%d",play.name, play.age, play.test, play.run) ;
printf ("More ? :") ;
scanf("%c", &ch);
}
fclose(p) ;
}




Related Links :

Program to enter data in File


#include
Main()
{
FILE *f1;
char c;
printf("Data Input\n");

f1=fopen("INPUT","w");
while((c=getchar())!=EOF)
{
putc(c,f1);
}
fclose(f1);

f1=fopen("INPUT","r");
while((c=getc(f1))!=EOF)
{
printf("%c", c);
}
fclose(f1);
}




Related Links :

Program to print Local Date & Time in C


#include
#include
int current_day(void)
{ struct tm *local;
time_t t;

t = time(NULL);
local = localtime(&t);
return local->tm_wday;
}
int main(int argc, char *argv[])
{
printf("Today: %d\n", current_day());
exit(0);
}





Related Links :

Program to find element from given array by Using Pointers(by Finding Memory Address)



main()
{
int *p, sum=0, i=0;
int x[5]={3,4,7,8,5};
clrscr();
p=x;
printf("Element Value Address\n\n");
while(i<5)
{
printf(" x[%d] %d %u\n",i,*p,p);
sum=sum+*p;
i++, p++;
}
printf("\nSum: %d",sum);
printf("\n&x[0]: %u",&x[0]);
printf("\np: %u",p);
getch();
}

Related Links :

Program to change Address of Variable By using Pointers.



main()
{
int a, b, *p1, *p2, x, y, z;
clrscr();
a=10;
b= 5;
p1=&a;
p2=&b;
x= *p1 * *p2 -6;
y= 4 * - *p1 / *p2 + 10;
printf("Address of a: %u\n",p1);
printf("Address of b: %u\n",p2);
printf("\na: %d, b: %d\n",a,b);
printf("X: %d, y: %d\n",x,y);
*p2=*p2+3;
*p1=*p2-5;
z= *p1 * *p2-6;
printf("\na: %d, b: %d",a,b);
printf("\nz: %d", z);
getch();
}





Related Links :

Program to change the value of variable by using Pointer.


main()
{
int x, *p, y;
clrscr();
x=10;
p=&x;
y=*p;
printf("Value of X: %d\nValue of P: %u\nValue of Y: %d",x,p,y);
printf("\nAddress of X: %u\nAddress of P: %u\nAddress of Y: %u",&x,&p,&y);
*p=20;
printf("\nNow Value of X: %d and Y: %d",x,y);
getch();
}





Related Links :

Program to show use of VOID function


#include
int func1(void);
int func2(void);
int func3(void);
int x;
main()
{
x=10;
printf("x= %d\n", x);
prinf("x= %d\n", func1());
prinf("x= %d\n", func2());

prinf("x= %d\n", func3());
getch();
}
func1(void)
{
x+=10;
}
int func2(void)
{
int x;
x=1;
return(x);
}
func3(void)
{
x=x+10;
}





Related Links :

Program to Sort the predefined Array by using Function


#include
void sort(int m, int x[]);
main()
{
int i;
int marks[5]={40, 90, 73, 81, 35};
clrscr();
printf("Marks before sorting\n");
for(i=0; i<5; i++)
printf("%4d", marks[i]);
printf("\n\n");
sort(5,marks);
printf("Marks after sorting\n");
for(i=0; i<5; i++)
printf("%4d", marks[i]);
printf("\n");
getch();
}
void sort(int m, int x[])
{
int i, j, temp;
for(i=1; i<=m-1; i++)
{
for(j=1;j<=m-1;j++)
{
if(x[j-1]>=x[j]

{

temp=x[j-1];

x[j-1]=x[j];

x[j]=temp;

}

}

}
}





Related Links :

Program to determine the given input is a capital letter, small letter, digit or special symbol.

Program to determine the given input is a capital letter, small letter, digit or special symbol.




#include

int main(void)
{
char ch;

printf( "\nEnter a character : " );
scanf("%c", &ch);


if(ch >= 'A' && ch <= 'Z') /* Capital Letter */
{
printf( "\nCapital Letter" );
}
else if(ch >= 'a' && ch <= 'z') /* Small Letter */
{
printf( "\nSmall Letter" );
}
else if(ch >= '0' && ch <= '9') /* Digit */
{
printf( "\nDigit" );
}
else
{
printf( "\nSpecial Symbol" );
}
return 0;
}

Related Links :

A program to implement the binary search algorithm

A program to implement the binary search algorithm.


#include

void binary_search(const int *, const int, const int);

int main(void)
{
int arr[1000], i, v, cnt;

/* Store the values in the array */
for( i = 0; i < found =" 0;" fi="first" mi="milldle,li=" n =" 0;" fi =" 0;" li =" sz"> p[li])
{
printf( "\n%d not found in the array", val);
printf( "\nNo of attempts = 1" );
}
else
{
printf( "\nAttempts\tFirst index\tLast index\tMiddle index\tarr[mi]" );
while(1)
{
n++;
/* Calculate middle index */
mi = (fi + li) / 2;

if( mi == fi || mi == li )
break; /* Processing complete */

printf( "\n\t%d\t%d\t\t%d\t\t%d\t\t%d", n, fi, li, mi, p[mi] );
if(val == p[mi] )
{
found = 1;
break;
}

if(val < li =" mi;"> p[mi])
{
fi = mi;
continue;
}
} /*End of while loop */
if(found)
{
printf( "\n%d found in the array", val);
printf( "\nNo of attempts = %d", n );
}
else
{
printf( "\n%d NOT found in the array", val);
printf( "\nNo of attempts = %d", n );
}
} /*End of else */
}

Related Links :

Understanding the concept of code blocks

Understanding the concept of code blocks.




#include

int main(void)
{
int num = 100;

{ // beginning of a code block
int var = 200; // local variable of this block

/* inner block or nested block can access variable
declared in the outer block */
printf("\nInner block 1: num = %d var = %d", num, var);

// char ch; // error! In C variable declarations should
// appear at the top of the block
} // end of a code block

// var = 300; // error! var is not accessible here

{
/* variable declared in an inner block can have the same
name as that declared in an outer block */

int num = 500;

printf("\nInner block 2: num = %d", num);
}

printf("\nOuter block : num = %d", num);

return 0;
}




Related Links :

Program to show Study of bitwise operators.

Program to show Study of bitwise operators.


#include

int main(void)
{
unsigned int n = 2;

printf( "\nn = %u", n );
printf( "\nn << 2 = %u", n << 2 );
printf( "\nn >> 2 = %u", n >> 2 );
printf( "\nn & 3 = %u", n & 3 );
printf( "\nn | 3 = %u", n | 3 );
printf( "\nn ^ 3 = %u", n ^ 3 );
printf( "\n~n = %u", ~n );

return 0;
}

Related Links :

Program to Find the length of a given string.

Program to Find the length of a given string.



#include

int main(void)
{
char str[80];
int len;

printf("\nEnter a string : ");
gets(str);

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

printf("\nLength of the string is %d", len);

return 0;
}



Related Links :

Program to find a ASCII value of the given char.

Program to find a ASCII value of the given char


#include

int main(void)
{
char ch;

printf("\nEnter a character : ");
scanf("%c", &ch);

printf("\nCharacter = %c ASCII Value = %d", ch, ch);

return 0;
}




Related Links :

Program to store a list by using 2D arrays

Program to store a list by using 2D arrays


int main(void)
{
/* names can store 3 names i.e. 1 name per row */
char names[3][20] = { "Ajay", "Vijay", "Sanjay" };
int i;

printf( "\nDisplaying names : " );
for(i = 0 ; i < 3 ; i++)
printf("\n%s", names[i]);
return 0;

}

Related Links :

Program to show 3D array Demo.

Program to show 3D array Demo.



#include

int main(void)
{
int arr[2][3][4], i, j, k;

for(i = 0 ; i < 2 ; i++)
for(j = 0 ; j < 3 ; j++)
for(k = 0 ; k < 4 ; k++)
arr[i][j][k] = i + j + k;
printf("\nDisplaying 3D array :");
for(i = 0 ; i < 2 ; i++)
{
printf("\nRow %d", i + 1);
for(j = 0 ; j < 3 ; j++)
{
printf("\tColumn %d\n\t\tCells: ", j + 1);
for(k = 0 ; k < 4 ; k++)
(" %d ", arr[i][j][k]);
printf("\n");
}
}
return 0;
}

Related Links :

Program to show Study of 2D Array Demo.

Program to show Study of 2D Array Demo.



#include

int main(void)
{
int arr[3][2]; /* Array representing a matrix of 3 rows and 2 cols */
int i, j;

/* Assigning values to array elements */
arr[0][0] = 10; arr[0][1] = 20;
arr[1][0] = 30; arr[1][1] = 40;
arr[2][0] = 50; arr[2][1] = 60;

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

Related Links :

Program to show array of pointers

Program to show array of pointers


#include

int main(void)
{
float *ap[5], n1, n2, n3, n4, n5;
int x;

ap[0] = &n1;
ap[1] = &n2;
ap[2] = &n3;
ap[3] = &n4;
ap[4] = &n5;

for(x = 0 ; x < 5 ; x++)
{
*ap[x] = (x + 1) / 10.0;
}
printf("\n%f\t%f\t%f\t%f\t%f", n1, n2, n3, n4, n5);
return 0;
}

Related Links :

Menu based program which allows users to calculate the area of Shapes

Menu based program which allows users to calculate the area of Shapes.




#include

int main(void)
{
char choice;

do
{

printf("\n\'C\'ircle\n\'T\'riangle\n\'R\'ectangle\n\'S\'quare\n\'E\'xit\nChoice :");
fflush(stdin);
scanf("%c", &choice);

switch(choice)
{
case 'c':
case 'C':
{
float radius;
printf("Enter the radius :");
scanf("%f", &radius);
printf("\nArea of circle is %f", 3.14 * radius * radius);
printf("\nPerimeter of circle is %f", 2 * 3.14 * radius);
break;
}
case 't':
case 'T':
{
float base, ht;
printf("Enter the base and height :");
scanf("%f%f", &base, &ht);
printf("\nArea of triangle is %f", 0.5 * base * ht);
break;
}
case 'r':
case 'R':
{
float length, breadth;
printf("Enter the length and breadth :");
scanf("%f%f", &length, &breadth);
printf("\nArea of rectangle is %f", length * breadth);
printf("\nPerimeter of rectangle is %f", 2 * (length + breadth));
break;
}
case 's':
case 'S':
{
float side;
printf("Enter the side :");
scanf("%f", &side);
printf("\nArea of square is %f", side * side);
printf("\nPerimeter of square is %f", 4 * side);
break;
}
default :
printf("\nInvalid choice");
}
}while(choice != 'E' && choice != 'e');

return 0;
}




Related Links :

Create a 10 element array of integers and count the number of odd and even values in it.

Create a 10 element array of integers and count the number of odd and even values in it.




#include
int main(void)
{
int arr[10], i, odd, even;

printf("\nEnter ten integer values : ");
for(i = 0 ; i < 10; i++)
scanf("%d", &arr[i]); for(odd = even = i = 0; i < 10; i++)
{
if(arr[i] % 2 == 0)
even++;
else odd++;
}
printf("\nTotal even values is %d\nTotal odd values is %d", even, odd);
return 0;
}

Related Links :

Program to show implementation of singly linkedlist

Program to show implementation of singly linked list




/* linklist.c */
/* implementation of singly linkedlist */
#include
#include
#include "linklist.h"

void release(linkedlist *lp)
{
while(lp -> count > 0)
del(lp, lp -> count);
}

node **goto_node(linkedlist *lp, int pos)
{
node *n = lp -> base, **curr = &(lp -> base);
if(pos <> lp -> count)
return NULL;

while(pos > 0)
{
curr = &(n -> next);
n = n -> next;
pos--;
}
return curr;
}

int append(linkedlist *lp, char *data)
{
node **curr = goto_node(lp, lp -> count);
*curr = (node *) malloc(sizeof(node));
if(*curr == NULL)
return FALSE;

strcpy((*curr) -> name, data);
(*curr) -> next = NULL;
lp -> count++;
return TRUE;
}

int insert(linkedlist *lp, int pos, char *data)
{
node **curr = goto_node(lp, pos - 1), *tmp;
if(curr == NULL)
return FALSE;
tmp = *curr;
*curr = (node *) malloc(sizeof(node));
if(*curr == NULL)
return FALSE;

strcpy((*curr) -> name, data);
(*curr) -> next = tmp;
lp -> count++;
return TRUE;
}

int del(linkedlist *lp, int pos)
{
node **curr = goto_node(lp, pos - 1), *tmp;
if(curr == NULL)
return FALSE;
tmp = *curr;
free(*curr);
*curr = tmp -> next;
lp -> count--;
return TRUE;
}

void show(linkedlist *lp)
{
node *n = lp -> base;
while(n != NULL)
{
printf("\n%s", n -> name);
n = n -> next;
}
}

int main(void)
{
int choice, pos;
char value[80];
linkedlist l = { NULL, 0 };
do
{
printf("\n ** SINGLE LINKED LIST **");
printf("\n1. Add\n2. Insert\n3. Remove\n4. Show\n5. Exit\nOption [1 - 5] ? ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nName :");
fflush(stdin);
gets(value);
if(!append(&l, value))
printf( "\nNode creation failed" );
else
printf( "\nNew node created successfully" );
break;
case 2:
printf("\nInsert position ? " );
scanf( "%d", &pos );
printf("\nEnter value :");
fflush(stdin);
gets(value);
if(!insert(&l, pos, value))
printf( "\nNode insertion failed" );
else
printf( "\nNode inserted successfully" );
break;
case 3:
printf("\nDelete position ? " );
scanf( "%d", &pos );
if(!del( &l, pos ))
printf( "\nNode removal failed" );
else
printf("\nNode removed successfully");
break;
case 4:
show(&l);
break;
case 5:
release(&l);
break;
}
}while(choice != 5);

return SUCCESS;
}




Related Links :

program to create a 10 element array of integers and search for a value in it.

program to create a 10 element array of integers and search for a value in it.



#include

int main(void)
{
int arr[10], i, val, n;

printf("\nEnter ten integer values : ");
for(i = 0 ; i < 10 ; i++ )
scanf("%d", &arr[i]);

printf("\nEnter value to search : ");
scanf("%d", &val);

/* Search the value in the array */
n = 0;
for(i = 0 ; i < 10 ; i++)
{
n++;
if(arr[i] == val)
break;
}
if(i == 10)
{
printf("\n%d not found in the array", val);
printf("\nNo of attempts = %d", n);
}
else
{
printf("\n%d found in the array", val);
printf("\nNo of attempts = %d", n);
}

return 0;
}




Related Links :

Program to count the number of capital letters, small letters, digits and special symbols in given String


/* Take input into a string and count the number of capital letters,
small letters, digits and special symbols in it. */

#include

int main(void)
{
char str[80];
int x, cap, small, dig, sym;

printf("\nEnter a string : ");
gets(str);

x = cap = small = dig = sym = 0;

for( ; str[x] != '\0' ; x++)
{
if(str[x] >= 'A' && str[x] <= 'Z') cap++;
else if(str[x] >= 'a' && str[x] <= 'z') small++;
else if(str[x] >= '0' && str[x] <= '9') dig++;
else sym++;

}

printf("\nCapital letters %d\nSmall letters %d", cap, small);
printf("\nDigits %d\nSpecial symbols %d", dig, sym);

return 0;
}




Related Links :

Program to Find the absolute value of an integer taken as input.



/* absolute.c */
/* Find the absolute value of an integer taken as input. */

#include

int main(void)
{
int num;

printf("\nEnter a number : ");
scanf("%d" ,&num);

if(num <>

Related Links :

Program to Show implementation of singly linkedlist


/* linklist.c */
/* implementation of singly linkedlist */
#include
#include
#include "linklist.h"

void release(linkedlist *lp)
{
while(lp -> count > 0)
del(lp, lp -> count);
}

node **goto_node(linkedlist *lp, int pos)
{
node *n = lp -> base, **curr = &(lp -> base);
if(pos <> lp -> count)
return NULL;

while(pos > 0)
{
curr = &(n -> next);
n = n -> next;
pos--;
}
return curr;
}

int append(linkedlist *lp, char *data)
{
node **curr = goto_node(lp, lp -> count);
*curr = (node *) malloc(sizeof(node));
if(*curr == NULL)
return FALSE;

strcpy((*curr) -> name, data);
(*curr) -> next = NULL;
lp -> count++;
return TRUE;
}

int insert(linkedlist *lp, int pos, char *data)
{
node **curr = goto_node(lp, pos - 1), *tmp;
if(curr == NULL)
return FALSE;
tmp = *curr;
*curr = (node *) malloc(sizeof(node));
if(*curr == NULL)
return FALSE;

strcpy((*curr) -> name, data);
(*curr) -> next = tmp;
lp -> count++;
return TRUE;
}

int del(linkedlist *lp, int pos)
{
node **curr = goto_node(lp, pos - 1), *tmp;
if(curr == NULL)
return FALSE;
tmp = *curr;
free(*curr);
*curr = tmp -> next;
lp -> count--;
return TRUE;
}

void show(linkedlist *lp)
{
node *n = lp -> base;
while(n != NULL)
{
printf("\n%s", n -> name);
n = n -> next;
}
}

int main(void)
{
int choice, pos;
char value[80];
linkedlist l = { NULL, 0 };
do
{
printf("\n ** SINGLE LINKED LIST **");
printf("\n1. Add\n2. Insert\n3. Remove\n4. Show\n5. Exit\nOption [1 - 5]

? ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nName :");
fflush(stdin);
gets(value);
if(!append(&l, value))
printf( "\nNode creation failed" );
else
printf( "\nNew node created successfully" );
break;
case 2:
printf("\nInsert position ? " );
scanf( "%d", &pos );
printf("\nEnter value :");
fflush(stdin);
gets(value);
if(!insert(&l, pos, value))
printf( "\nNode insertion failed" );
else
printf( "\nNode inserted successfully" );
break;
case 3:
printf("\nDelete position ? " );
scanf( "%d", &pos );
if(!del( &l, pos ))
printf( "\nNode removal failed" );
else
printf("\nNode removed successfully");
break;
case 4:
show(&l);
break;
case 5:
release(&l);
break;
}
}while(choice != 5);

return SUCCESS;
}





Related Links :

Program to Find Quality of Steel and give Grades

A certain grade of steel is graded according to the following conditions:

1)Hardness must be greater than 50
2)Carbon content must be less than 0.7
3)Tensile strength must be greater than 5600

The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if (1) and (2) are met
Grade is 8 if (2) and (3) are met
Grade is 7 if (1) and (3) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions is met

Write a program which will require the user to give the values of hardness,
carbon content and tensile strength of the steel under consideration and output
the grade of the steel.



#include
main()
{
int hardness, ts, grade;

float carbon;

printf("Enter the values of hardness, tensile strength and carbon content in the steel:");
scanf("%d %d %f", &hardness, &ts, &carbon);

if ((hardness>50) && (carbon<0.7)>5600))

printf("Grade 10");
else if ((hardness>50) && (carbon<0.7))
printf("Grade 9"); else if ((carbon<0.7)>5600))

printf("Grade 8");
else if ((hardness>50) && (ts>5600))

printf("Grade 7");
else if ((hardness>50) || (carbon<0.7)>5600))

printf("Grade 6");
else
printf("Grade 5");

}




Related Links :

Program To Calculate Insurance using Logical Operators.

Program To Calculate Insurance using Logical Operators.



#include
main()
{
int age,premium, max_amount;

char health, location, sex;


printf("Enter Health - g for good / b for bad\nEnter Location - c for city / v for village");

printf("\nEnter sex - m for male / f for female");

printf("\nEnter the health, age, location and sex of the person:");

scanf ("%c %d %c %c", &health, &age, &location, &sex);



if ((health=='g') && ((age>=25)&&(age<=35)) && (location=='c') && (sex=='m'))
{
premium=4; max_amount=2;
printf("This person is insured.\nThe payable premium is Rs. %d per thousand\n and the max policy amount is Rs. %d Lakhs", premium, max_amount);
}
else
if ((health=='g') && ((age>=25)&&(age<=35)) && (location=='c') && (sex=='f'))
{
premium=3;
max_amount=1;
printf("This person is insured.\nThe payable premium is Rs. %d per thousand\nand the max policy amount is Rs. %d Lakhs", premium, max_amount);
}
else
if((health=='b') && ((age>=25)&&(age<=35)) && (location=='v') && (sex=='m'))
{
premium=6;
max_amount=10000;
printf("This person is insured.\nThe payable premium is Rs. %d per thousand \nand the max policy amount is Rs. %d ", premium, max_amount);
}
else
{
printf("This person is not insured.");
}
}

Related Links :

Program to Find Character Group using ASCII And Conditional Operator.


#include
main()
{
char charac;


printf("Enter the character to be Find:");
scanf("%c", &charac);

((charac>=97) && (charac<=122))?(printf("\nLower Case")):(printf("\nNot a Lower Case character"));

/*For detection of Special Symbols*/

(((charac>=0) && (charac<=47)) || ((charac>=58) && (charac<=64))|| ((charac>=91) && (charac<=96))|| ((charac>=123) && (charac<=127)))? (printf("\nSpecial Symbol")):(printf("\nNot a Special Symbol"));

}







Related Links :

Program to Reverse the number and check for Equality.

Program to Reverse the number and check for Equality.



#include
main()
{

int last_digit, number, next_digit, rev_num;

printf ("Enter the five-digit number that has to be reversed and checked for equality:");

scanf("%d", &number);

last_digit = number - ((number / 10) * 10); /*units place*/

rev_num = last_digit; /* 5 */

next_digit = (number / 10) - ((number / 100) * 10); /*tenth's place*/

rev_num = (rev_num * 10) + next_digit; /*54*/

next_digit = (number / 100) - ((number / 1000) * 10); /*hundred's place*/

rev_num = (rev_num * 10) + next_digit; /*543*/

next_digit = (number / 1000) - ((number / 10000) * 10); /*thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*5432*/

next_digit = (number / 10000) - ((number / 100000) * 10); /*ten thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*54321*/

printf ("The Reversed Number is: %d",rev_num);

if (rev_num==number)

{
printf("\nEntered number and reversed number are equal\n");
}
else

{
printf("\nEntered number and reversed number are not equal\n");
}
}




Related Links :

Program to calculate Gross Salary using if-else statement

In a company if an employee is paid as under:

  • If his basic salary is less than Rs. 1500, then HRA=10% of basic salary and DA=90% of basic salary.
  • If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary.
  • If the employee's salary is input through the keyboard write a program to find his gross salary.





#include
main()
{

float bs, gs, da, hra;

printf ("Enter basic salary:");

scanf ("%f", &bs);

if (bs<1500)

{
hra=bs*10/100;
da=bs*90/100;

}
else
{
hra=500;
da=bs*98/100;

}

gs=bs + hra + da;

printf ("gross salary = Rs. %f", gs);
}





Related Links :

Program To Calculate Discount using 'if' statement.




#include
main()

{
int qty,dis=0; /*initializing is required as if tot<1000, then discount is 0*/
float rate, tot;

printf ("Enter quantity and rate: ");
scanf ("%d %f", &qty, &rate);

if (qty>1000)
dis=10;

tot=(qty*rate) - (qty*rate*dis/100);

printf("Total expenses = Rs. %f", tot);

}



Related Links :

Program To Calculate Driver insurance using if-else statement

A company insures its drivers in the following cases:
- If the driver is married.
- If the driver is unmarried, male and above 30 years of age.
- If the driver is unmarried, female and above 25 years of age.

In all the other cases, the driver is not insured.
If the marital status, sex and age of the driver are the inputs,
write a program to determine whether the driver is insured or not.






#include
main()
{

char sex,ms;
int age;

printf ("Enter age, sex, marital status:");

scanf ("%d %c %c", &age, &sex, &ms);

if (ms=='M')

printf ("The driver is insured");
else
{
if (sex=='M')

{
if (age>30)
printf ("Driver is insured");

else
printf ("Driver is not insured");
}
else
{

if (age>25)
printf ("Driver is insured");
else
printf ("Driver is not insured");

}
}
}




Related Links :

Program To Calculate Employees Bonus In C



#include
main ()
{

int bonus, cy, yoj, yr_of_ser;

printf ("Enter current year and year of joining: ");

scanf ("%d %d", &cy, &yoj);

yr_of_ser = cy - yoj;

if (yr_of_ser>3)
{
bonus = 2500;

printf ("Bonus = Rs. %d", bonus);
}
}



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!!!