Download 1) Write a program to shift the entered number by three bits left and

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Positional notation wikipedia , lookup

Arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Transcript
Solution for Chapter 6 Exercise
1) Write a program to shift the entered number by three bits left and display the
result.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the number:-");
scanf("%d",&b);
a=b<<3;
printf ("\n a=%d",a);
}
OUTPUT
Enter the number:-8
a=64
Explanation In the above program an integer is entered. Using left shift operator bits are
shifted and obtained number is displayed.
2) Write a program to shift the entered number by five bits right and display the
result.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the number:-");
scanf("%d",&b);
a=b>>5;
printf ("\n a=%d",a);
}
OUTPUT
Enter the number:-64
a=2
Explanation The above program is same as last one. Here, right shift operator is used.
3) Write a program to mask the most significant digit of the entered number. Use
AND operator.
#include <stdio.h>
#include <conio.h>
void main ()
{
int x,n;
clrscr();
printf ("\n Enter numbers : ");
scanf ("%x",&n);
x=n&0xf;
printf ("x=%d",x);
}
OUTPUT
Enter numbers: 89
x=9
Explanation In the above program a decimal number is entered. The decimal number is
converted to hexa-decimal equivalent using %x format string in the scanf () statement.
Using & operator with 0xf number the most significant digit of enter number is masked.
4) Write a program to enter two numbers and find the smallest out of them. Use
conditional operator.
# include <stdio.h>
# include <conio.h>
main()
{
int a,b;
clrscr();
printf("Enter 1st integer:");
scanf("%d",&a);
printf("Enter 2nd integer:");
scanf("%d",&b);
printf("The smallest number =%d",(a<b?a:b));
getche();
}
OUTPUT
Enter 1st integer:35
Enter 2nd integer:18
The smallest number =18
OR
Enter 1st integer:14
Enter 2nd integer:82
The smallest number =14
Explanation In the above program two integers are entered. Using conditional operator
their comparison is done.The conditional operator also contains two statements, which
are executed after the expression is solved. Thus, the smallest number is tested and
displayed.
5) Write a program to enter a number, carry out modular division operation by 2,3
and 4, and display the
remainders.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a number:-");
scanf("%d",&a);
printf ("Modular Division by 2: %d",a%2);
printf ("\nModular Division by 3: %d",a%3);
printf ("\nModular Division by 4: %d",a%4);
getch();
}
OUTPUT
Enter a number:-23
Modular Division by 2: 1
Modular Division by 3: 2
Modular Division by 4: 3
Explanation In the above program an integer number is entered. Modular division by 2,4
and 4 is performed and result is displayed.
6) Attempt the program (5) with division operation and find the quotients.
#include<stdio.h>
#include<conio.h>
void main()
{
float a;
clrscr();
printf("Enter a number:-");
scanf("%f",&a);
printf ("Division by 2: %.2g",a/2);
printf ("\nDivision by 3: %.2g",a/3);
printf ("\nDivision by 4: %.2g",a/4);
getch();
}
OUTPUT
Enter a number:-24
Division by 2: 12
Division by 3: 8
Division by 4: 6
Explanation The above program is same as last one. Here, instead of modular division,
division operation is performed.
7) Write a program to enter an integer number and display its equivalent values in
octal and hexadecimal.
# include <stdio.h>
# include <conio.h>
void main()
{
int x;
clrscr();
printf ("\n Enter a number : ");
scanf ("%d",&x);
printf ("\n Hexadecimal number : %x",x);
printf ("\nOctal number : %o",x);
}
OUTPUT
Enter a number : 95
Hexadecimal number : 5f
Octal number : 137
Explanation: In the above program an integer number is entered. The format string %x
converts decimal number to hexadecimal and %o converts decimal number to octal.
8) Write a program to convert hexadecimal to decimal numbers. Enter the numbers
such as 0x1c, 0x18, 0xbc, 0xcd etc.
# include <stdio.h>
# include <conio.h>
void main()
{
int x;
clrscr();
printf ("\n Enter a number : ");
scanf ("%x",&x);
printf ("\n Decimal Number : %d",x);
}
OUTPUT
Enter a number: 5f
Decimal Number: 95
Explanation The Above program is same as last one. Here, the format string %x is used
in scanf () statement to accept number in hexadecimal format. The format string %d
converts hexadecimal number to decimal equivalent.
9) Write a program to find the average temperature of five sunny days. Assume the
temperature in Celsius.
# include <stdio.h>
# include <conio.h>
void main()
{
float a,b,c,d,e,avg;
clrscr();
printf("Enter 1st sunny day temperature:");
scanf("%f",&a);
printf("Enter 2nd sunny day temperature:");
scanf("%f",&b);
printf("Enter 3rd sunny day temperature:");
scanf("%f",&c);
printf("Enter 4th sunny day temperature:");
scanf("%f",&d);
printf("Enter 5th sunny day temperature:");
scanf("%f",&e);
avg=(a+b+c+d+e)/5;
printf("\nThe average of five sunny days temperature =%.2f oC ",avg);
getche();
}
OUTPUT
Enter 1st sunny day temperature: 28.2
Enter 2nd sunny day temperature: 27.5
Enter 3rd sunny day temperature: 29.0
Enter 4th sunny day temperature: 26.8
Enter 5th sunny day temperature: 28.1
The average of five sunny day temperature =27.92 oC
Explanation In the above program temperature of five days are entered. Sum of
temperature is calculated. Average is calculated and displayed.
10) Write a program to enter two numbers. Make the comparison between them
with conditional operator. If the first number is greater than second perform
multiplication otherwise division operation.
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf("Enter two numbers:-");
scanf("%f%f",&a,&b);
(a>b) ? (c=a*b) : (c=a/b);
(a>b)?printf("Multiplication =%g",c):printf("Division =%g",c);
getch();
}
OUTPUT
Enter two numbers:-20 30
Division =0.666667
Enter two numbers:-30 20
Multiplication =600
Explanation In the above program two numbers are entered. Using conditional operator
numbers are tested. If the first number is greater than second multiplication of numbers is
calculated otherwise division is performed.