KnR Solutions


This is for the question ex 1-3:


//WAP to print celsius to fahrenheit table with heading
#include<stdio.h>

int main(void)
{
float cels,fahr,upr=300.0,lwr=0.0,stp=20.0;

printf("\nFahrenheit\tCelsius\n");

fahr = lwr;
while(fahr<=upr)
{
cels = (5.0/9.0) * (fahr-32);
printf("%10.0f\t%7.1f\n",fahr,cels);
fahr = fahr + stp;
}
return 0;
}

This is for the question ex 1-4



//WAP to print table of celsius to fahrenheit and also print d headings
#include<stdio.h>
int main(void)
{
float cels,fahr,upr=300,lwr=0,step=20;

cels = lwr;

printf("\nCelsius\tFahrenheit\n");

while(cels<=upr)
{
fahr = ((9.0/5.0)*cels) +32;
printf("%7.0f\t%10.1f\n",cels,fahr);
cels += step;
}
return 0;
}

This is for the question ex 1-5



#include<stdio.h>
int main(void)
{
int cels;
printf("\nCelsius\tFahrenheit\n");
for(cels = 300 ; cels >= 0 ; cels -= 20)
printf("%7d\t%10.1f\n",cels,( ((9.0/5.0)*cels)+32 ));

return 0;
}

This is for the question ex 1-6



#include<stdio.h>
int main(void)
{

int c;
c = (getchar() != EOF);
printf("%d\n",c);
return 0;
}


This is for the question ex 1-7



#include<stdio.h>
int main(void)
{
printf("%d",EOF);
return 0;
}


This is for the question ex 1-8



#include<stdio.h>

int main(void)
{
int count=0,c;
printf("\nEnter the input as long as you want, ^d to stop.\n");

while((c=getchar()) != EOF)
if(c == ' ' || c == '\t' || c == '\n')
++count;

printf("\nThe space character count is %d\n",count);
return 0;
}


This is for the question ex 1-9



//Enter a string at coomand line with as many spaces as you want in between the words
//and this program will replace all the spaces with one single sapce
#include<stdio.h>

int main(void)
{
int c,d,flg=0;

while((c=getchar()) != EOF) //take input character wise
{
if(c!=' ' && c!='\t') //if the character is not a space character
{
if(flg) //first check the flag status
{
putchar(' '); //if flag is one then insert a space in output stream
flg=0; //reset the flag
}
putchar(c); //keep pushing the character in output stream
}
else //if character is blank space
{
flg = 1; //first set the flag
d=getchar(); //read the next character
if(d!=' ' && d!='\t') //if this character is not a blank space
{
if(flg) //check flag status
{
putchar(' '); //if flag is 1,insert the space character
flg=0; //and set the flag to zero
}
putchar(d); //now push d onto stream
}
else flg=1; //if d were not blank then set the flag
}
if (c == '\n')
return 0;
}
return 0;
}


This is for the question ex 1-10

No comments:

Post a Comment