Wednesday, 9 October 2013
Tuesday, 14 May 2013
10. To find the perimeter and area of a circle
10. /* To find the perimeter and area of a circle */
/* Area of a circle with radius 'r' is PI*r*r and Perimeter is 2*PI*r */
main()
{
float r,PI;
PI=3.14;
clrscr();
printf("Enter the radius of the circle -> ");
scanf("%f",&r);
printf("\nThe area is %7.2f and perimeter is %7.2f",PI*r*r,2*PI*r);
getch();
}
9. Program to convert temperature Fahrenheit into Celsius
9. /* Program to convert temperature Fahrenheit into Celsius */
/* degree c=5/9*(f-32) */
/* c-Celsius & f-Fahrenheit */
main()
{
float c,f;
clrscr();
printf("Enter the temperature in Fahrenheit ");
scanf("%f",&f);
c=5.0/9.0*(f-32.0);
printf("\nTemperature in Celsius is %f \n",c);
getch();
}
8. printf numbers from 1 to 100 with formatted output
8. /* printf numbers from 1 to 100 with formatted output */
main()
{
int i;
clrscr();
for(i=1;i<=100;i++)
printf("%4d",i);
getch();
}
7. String formatted output
7. /* String formatted output */
main()
{
clrscr();
printf("%4s\n","Computer Screen"); /* output -> 'Computer Screen' */
printf("%.8s\n","Computer Screen"); /* output -> 'Computer' */
printf("%16.8s\n","Computer Screen"); /* output -> 'Computer' */
getch(); /* with 8 leading blanks */
}
6. Format for string input
6. /* Format for string input */
main()
{
char str[7];
clrscr();
printf("Enter a string ");
scanf("%4s",str);
puts(str); /* if the input is 'computer', the output is 'comp' */
getch();
}
5. Find the sum and average of 3 numbers
5. /* Find the sum and average of 3 numbers */
main()
{
float a,b,c,sum,avg;
a=b=c=0;
clrscr();
printf("Enter the value of a -> ");
scanf("%f",&a);
printf("\n\nEnter the value of b -> ");
scanf("%f",&b);
printf("\n\nEnter the value of c -> ");
scanf("%f",&c);
sum=a+b+c;
avg=sum/3;
printf("\n\n Sum is %8.0f",sum);
printf("\n\n Average is %10.2f",avg);
getch();
}
4. Decimal to Octal & Hexa conversion
4. /* Decimal to Octal & Hexa conversion */
main()
{
int num;
clrscr();
printf("Enter a decimal number : ");
scanf("%d",&num);
printf("\n\nOctal equivalent of %d is %o ",num,num);
printf("\n\nHexa equivalent of %d is %x \n",num,num);
getch();
}
3. Examples for scanf & printf
3. /* Examples for scanf & printf */
main()
{
int age;
float basic_pay,hra,da;
char m_s;
clrscr();
printf("Enter your age \a");
scanf("%d",&age);
printf("\n\nEnter Basic pay, Hra, Da & Marital Status\n\a");
scanf("%f %f %f %c",&basic_pay,&hra,&da,&m_s);
printf("\n\nYour age is : %d",age);
printf("\n\nBasic pay = %f \tHra = %f \tDa = %f\n",basic_pay,hra,da);
printf("\n\nYour Marital Status is : %c\n",m_s);
getch();
}
1. /* Examples for printf */
1. /* Examples for printf */
main()
{
clrscr();
printf("Computer \n");
printf("Welcomes
you\n\n"); printf("Micky\r"); printf("Mouse\n\n");
printf("Mountain\v"); printf("Ocean\n\n");
printf("Tab-1\tTab-2\n\n");
printf("Backspace\b\b\b\b\bslash\n\n"); /* o/p ->
Backslash */ printf("Ok. Thankyou!\a\n\n");
getch();
}
Examples for getch() & putch()
2. /* Examples for getch() & putch() */
main()
{
char c,t; /* character type data name */
clrscr(); /* clear screen */
c=getch(); /* get and store a character to variable 'c' */
putch(c); /* output to screen */
t=putch(); /* output to screen and variable 't' */
getch(); /* get a character */
}
Subscribe to:
Posts (Atom)