Advertisement

A programme to convert a decimal number into Hexadecimal number

#include<stdio.h>
#include<conio.h>
#include<math.h>
void dtoh(int d);
main()
{

int d;
printf("Enter a no. in decimal system:- ");
scanf("%d",&d);
dtoh(d);

getch();
}

void dtoh(int d)
{
int b,c=0,a[5],i=0;
b=d;
while (b>15)
{
a[i]=b%16;
b=b/16;
i++;
c++;
}
a[i]=b;
printf("Its hexadecimal equivalent is ");
for (i=c;i>=0;--i)
{
if (a[i]==10)
printf("A");
else if (a[i]==11)
printf("B");
else if (a[i]==12)
printf("C");
else if (a[i]==13)
printf("D");
else if (a[i]==14)
printf("E");
else if (a[i]==15)
printf("F");
else
printf("%d",a[i]);
}
return;
}

May also visit : http://myitzn.blogspot.com/ for more tech and study materials

A programme to convert decimal number to binary number

#include <stdio.h>
#include <conio.h>
void main()
{int num,bnrydigit;
printf("Enter the decimal number:= ");
scanf("%d",&num);
for( ;num!=0;  )
{bnrydigit=num%2;
printf("%d",bnrydigit);
num=num/2;
}
getch();
}

Discussion of array


An array is a group of related data items that share a common name. For instance, we
can define array name salary to represent a set of salary of a group of employees. A particular
value is indicated by writing a number called index number or subscript in brackets after the
array name.
Eg: salary[10]
ONE DIMENSIONAL ARRAY
int number[5];
The values to array elements can be assigned as follows.
Eg:
 number[0] = 35;
number[1] = 40;
number[2] = 20;
This types of array can also be::
1) float height[50];
2) int group[10];
3)char name[10]

STRING - HANDLING FUNCTIONS
Function                                Action
strcat( )                            Concatenates two strings
strcmp( )                         Compares two strings
strcpy( )                          Copies one string over another
strlen( )                          Finds the length of the string

Eg::
Program
/*Illustration of string-handling functions*/
#include<string.h>
main()
{clrscr();
char s1[20],s2[20],s3[20];
int x, l1, l2, l3;
printf(“Enter two string constants \n”);
printf(“?”);
scanf(“%s %s”, s1, s2);
x = strcmp(s1, s2);
if(x != 0)
printf(“Strings are not equal \n”);
strcat(s1, s2);
else
printf(“Strings are equal \n”);
strcpy(s3,s1);
l1 = strlen(s1);
l2 = strlen(s2);
l3 = strlen(s3);
printf(“\ns1 = %s \t length = %d characters \n”,s1, l1);
printf(“\ns2= %s \t length = %d characters \n”,s2, l2);
printf(“\ns3 = %s \t length = %d characters \n”,s3, l3);
}
OUTPUT
Enter two string constants
? New York
Strings are not equal
s1 = New York length = 7 characters
s2 = York length = 4 characters
s3 = New York length = 7 characters

Discussion of loop


The C language provides for three loop constructs for performing loop operations. They are
Ø The while statement
Ø The do statement
Ø The for statement

THE WHILE STATEMENT

The basic format of the while statement is
The while is an entry–controlled loop statement. The test-condition is evaluated and if
the condition is true, then the body of the loop is executed. After execution of the body, the
test-condition is once again evaluated and if it is true, the body is executed once again. This
process of repeated execution of the body continues until the test-condition finally becomes
false and the control is transferred out of the loop.
Body of the loop
test
condn?
while(test condition)
{
body of the loop
}
Sample::
sum = 0;
n = 1;
while(n <= 10)
{
sum = sum + n* n;
n = n + 1;
}
printf(“sum = %d \n”,sum);
-----------

THE DO STATEMENT
In while loop the body of the loop may not be executed at all if the condition is not
satisfied at the very first attempt. Such situations can be handled with the help of the do
statement.
Since the test-condition is evaluated at the bottom of the loop, the do…..while construct
provides an exit-controlled loop and therefore the body of the loop is always executed at least
once.
Eg:
-----------
do
{
printf(“Input a number\n”);
number = getnum();
}
while(number > 0);



THE FOR STATEMENT

Simple ‘for’ Loops
The for loop is another entry-controlled loop that provides a more consise loop control
structure. The general form of the for loop is

for(initialization ; test-condition ; increment
{
body of the loop

}
Example::
for(x = 0; x <= 9; x = x + 1)
{
printf)”%d”,x);
}
printf(“\n”);



Nesting of For Loops
C allows one for statement within another for statement.
----------
----------
for(i = 1; i < 10; ++ i)
{
---------
---------
for(j = 1; j! = 5; ++j)
{ Inner Outer
--------- loop loop
---------
}
---------
---------
}
----------
----------
Eg:
----------
----------
for(row = 1; row <= ROWMAX; ++row)
{
for(column = 1; column < = COLMAX; ++column)
{
y = row * column;
printf(“%4d”, y);
}
printf(“\n”);
}
----------
----------
JUMPS IN LOOPS
C permits a jump from one statement to another within a loop as well as the jump out
of a loop.
Jumping out of a Loop
An early exit from a loop can be accomplished by using the break statement or the
goto statement.
When the break statement is encountered inside a loop, the loop is immediately
exited and the program continues with the statement immediately following the loop. When
the loops are nested, the break would only exit from the loop containing it. That is, the break
will exit only a single loop.

Skipping a part of a Loop
Like the break statement, C supports another similar statement called the continue
statement.


continue;

The use of continue statement in loops.
 while(test-condition)
{
---------
if(--------)
continue;
----------
----------
}