CHAPTER2_PRO


    PROGRAMMING IN ANSI C

                                                             CHAPTER-2
  Constants, Variables,
  and Data Types               Programming Exercises
2.1: Write a program to determine and print the sum of the following harmonic series for a given value of n:
1+1/2+1/3+………………+1/n
The value of n should be given interactively through the terminal.
Solve:
#include<stdio.h>
#include<conio.h>
Void main()
{
int n;
float I, sum, t;
clrscr();
printf(“1+1/2+1/3+……………+1/n\n”);
printf(“Enter the value of n\n”);
scanf(“%d”,&n);
sum=0;
for(i=1;i<=n;i++)
{
t=1/i;
sum=sum+t;
}
printf(“%f”,sum);
getch();
}
Output:
1+1/2+1/3+………….+1/n
Enter the value of n
4
2.083333

2.2: Write a program to read the price of an item in decimal form (  like 15.95 ) and print the output in paisa ( like 1595 paisa) .
Solve:
#include<stdio.h>
#include<conio.h>
void main()
{
int b;
float a;
a=15.95;
clrscr();
b=100*a;
printf("%d",b);
getch();
}
Output:
1595

 2.3: Write a program that’s prints the even numbers from 1 to 100.
 #include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=100;i++)
{
if(i%2==0)
printf("  %d",i);
}
getch();
}
Output
2  4  6  8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42 44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82 84  86  88  90  92  94  96  98  100

2.4: Write a program that request two float type numbers from the users and then divides the first number by the second and display the result along with the numbers.
Solve:
#include<stdio.h>
#include<conio.h>
void main()
{
float  number1, number2, number3;
clrscr();
printf("Enter the value of number1 and number2\n");
scanf("%f %f",&number1,&number2);
number3=number1/number2;
printf("%f/%f=%f",number1,number2,number3);
getch();
Output:
Enter the value of number1 and number2
15.5
6.6
15.5/6,6=2.348484
2.5: The price of one kg of rice is Rs. 16.75 and one kg of sugar is Rs. 15. Write a program to get these values from the user and display the prices as follows:
         ***LIST OF ITEMS***
            Item                 Price
            Rice                 Rs 16.75
            Sugar               Rs 15.00
Solve:
#include<stdio.h>
#include<conio.h>
void main ()
{
float Rice,Sugar;
Rice=16.75;
Sugar=15.00;
clrscr();
printf("***LIST OF ITEMS***\n");
printf("Item \tPrice\n");
printf("Rice\tRs%.2f\n",Rice);
printf("Sugar\tRs%.2f\n",Sugar);
getch();
}
Output:
***LIST OF ITEMS***
Item            Price
Rice             Rs16.75
Question Others: Identify syntax errors in the following program. After correcting, what output would you expect when you execute it.
#include<stdio.h>
#include<conio.h>
#define PI 3.14159
void main()
{
int R,C;
float perimeter;
float area;
C=PI;
R=5;
perimeter=2.0*C*R;
Area = C*R*R;
printf("%f", "%d",&perimeter,&area)
}
Errors:
Cpp 10: Undefined symbol Area.
Cpp 12:statement missing,in function{}
Cpp12: compound statement missing.
Solve:
#include<stdio.h>
#include<conio.h>
#define PI 3.14159
void main()
{
int R,C;
float perimeter,Area;

C=PI;
R=5;
perimeter=2.0*C*R;
Area = C*R*R;
printf("%f %f",perimeter,Area);
getch();
}