174 Programming and Data Structures
6.59 Write a program to convert binary number to equivalent decimal number.
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
void main ()
{
long n;
int x,y*0,p=0;
clrscrO;
printf ("tt Enter a Binary Number
scanf ("% ld",8zn);
while (n!=0)
I
x=n%10;
if(x>l I I x<0)
/
printf ("n Invalid Digit);
exit{l);
}
y=y+x*pozv(2,p);
n-nJIO;
p++;
printf ("n Equivalent Decimal Number is %d.”,y);
jjetcheO;
QUTP1TE
Enter a Binary Number: 1111
Equivalent Decimal Number is 15
Enter a Binary Number: 120
Invalid Digit
Explanation In the above program binary number is entered. The equationy=y+x*pow (2,p) is
used in each iteration where 1 y ' is iniatlized to 0. The variable ' x ' is obtained by mod operation
which separates individual digit of the entered number and the variable % p ' acts as an exponent for
base 2. It is initially zero and increased by one in each iteration,: where p varies from 0 to (number of
digits-1). The product ( x*pow (2,p) ) is added with the previous value of'y#
6.60 Write a program to read a positive integer number 'n' and generate the numbers in the
following way. If entered number is 5 the output will be as follows. OUTPUT: 5 4 3 21
0 1 2 3 4 5.
# include <stdio.h>
# include <conio.h>
# include <math.h>
Loop Control Statements 175
void main ()
{
int n,i,k*0;
clrscrO;
printf ("Enter a number
scanf ("%d ", &cn);
i=n+l;
k-k-n;
while (i!=k)
{
printf ("%3d",abs(k));
fc++;
/
OUTPUT;
Enter a number: 3
3210123
Explanation The value of 'n ' is entered through the keyboard. The variable 'k ' is initially dedared
as zero. The equation k=k-n i.e. k=0-n=-n. The value of 'k ' is negative, hence abs () function is used
to print positive (absolute) value. The w hile loop checks the condition for (i 1 =k). Variable % k# is
printed and increased till it's value becomes n+1. In this way, the numbers are printed as asked in the
problem.
6.61 Write a program to enter a number through keyboard and find the sum of its digits.
# include <stdio.h>
# include <conio.h>
void main ()
{
in t nfk sl/ sump:0;
clrscrO ;
printf ("Enter a Number
scanf ("%d",&cn);
while (n!=0)
I
k-n%10;
sum=sum+k;
k=n/10;
n-k;
I
printf ("Sum of digits %d",sum);
I
OUTPUT:
Enter a Number: 842
Sum of digits 14
176 Programming and Data Structures
Explanation In the above program a number is entered by the user. It is assigned to the variable ' n'.
In the while loopmod and division operations are done on the entered number. At each rotation of
while loop, remainder is obtained and added to the variable ' sum'. The original number is also
reduced by the division operation. Due to continuos division, at one stage the entered number is
reduced to zero. At this stage the while loop condition mismatches and it is terminated. The variable
'sum' displays the 'sum' of all digits of the entered number.
6.62 Write a program to enter few numbers and count the positive and negative numbers
together with their sums. When 0 is entered program should be terminated.
# include <stdio.h>
# include <conio.h>
void mainO
{
Int j*l,p«0,n»0,s*0,ns»0;
clrscrO;
printf ("n Enter Numbers (0) Exit:");
while (j!-0)
/
scanf r%d",Scj);
if(j>0)
I
p++;
s-s+j;
I
else if(j<0)
I
«++;
ns=ns+j;
I
I
printf ("n Total Positive Numbers: %d",p);
printf (" n T o ta l Negative Numbers :%d",tt);
printf ("n Sum of Positive Numbers: %d",s);
printf("n Sum of Negative Numbers: %d",ns);
I
Q u m n i
Enter Numbers (0) Exit :1 2 3 4 5 -5 -4 -8 0
Total Positive Numbers : 5
Total Negative Numbers : 3
Sum of Positive Numbers : 15
Sum of Negative Numbers : -17
Loop Control Statements 177
Explanation Numbers are entered through the keyboard. The i f statement checks whether the
entered numbers are greater or less than zero. If the numbers are greater than zero i f block is executed
otherwise e ls e block. The output shows a number of positive and negative numbers and their sums.
When 0 is entered while loop terminates.
6.63 Read an integer through the keyboard. Sort odd & even numbers by using while loop.
Write a program to add sum of odd & even numbers separately & display the results.
main()
i
int a,c=l,odd=0,even=0;
float b;
clrscrO;
printf ("Enter a Number
scanf ("%d", &a);
printf ("ODD
printf ("n");
EVEN");
while (c<=a)
{
/* A
b=c%2;
while (b==0){ /* B
printf ("t%d ",c);
even=even+c;
b=l;}
C++;
}
printf ("n
-----
;
printf (" %d
)
b=c%
2;
while(b!=0M c
printf (" %d",c);
odd=odd+c;
b=0;}
=
------------------
");
%d",odd,even);
OUTPUT;
Enter a Number: 10
ODD EVEN
1 2
3 4
5 6
7 8
9 10
25 30
178 Programming and Data Structures
Explanation The above program is an example of nested whi le loops._When the entered number is
even the first loop (B loop) is executed and a number is added to variable ' even' , otherwise the
whileloop (C loop) is executed and the same job of addition is done with variable 'odd' . Thus,
variables 'odd' and 'even' gives the sum of even and odd numbers.
6,64 Write a program to print the entered number in reverse order.
mainQ
{
int n,d,xsl;
int i;
clrscrO;
printf("Enter the number of digits
scanf ("%d",&cd);
printf(" Enter the number which is to be rev ersed ");
scanf("%d",&n);
printf ("nThe Reversed Number i s ");
while (x<=d)
I
i=«%10;
printf("%d",i);
n=n/10;
x++;
}
getcheO;
I
QUIPIUT;
Enter the number of digits4
Enter the number which is to be reversed:- 5428
The Reversed Number is :- 8245
Explanation The statements following thewhile loop are i=n%10 andn=n/10. They provide remainder
and quotient values respectively. By taking repeatedly remainders & quotients we get the number in
the reverse order. For repeating the loop the ' x ' is to be increased.
6.65 Write a program to enter a statement entering a combination of capital, small, symbols and
numerical. Carry out separation of capitals, small, symbols and numerical by using ASCII values
from 48 to 122.
# include <conio.h>
# include <stdio.h>
# include <ctype.h>
void mainO
{
s ta tic char scan [40], cap [20], small [2 0 ],num[20] ,oth[20];
in t i=0,c=0,s=0,h=0,n=0;
c lrs c rO ;
putsC'Enter Text Here : ");
gets(scan);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.154.252