Saturday, September 28, 2013

Misc C programs

WRITE A PROGRAM WHICH PRINTS A TEXT OF 4 LINES CONSISTING OF CHARACTERS, INTEGER VALUES AND FLOATING POINT VALUES USING “printf” STATEMENT?

PROGRAM:

#include <stdio.h>#include <conio.h>void main(void){
clrscr();
float north = 23.30, north2 = 36.45;
int east1 = 61,float east2 = 75.30;
printf(“Pakistan is located between %f degrees and\n”, north1);
printf(“%f degrees north latitude and %d\n”, north2, east1);
printf(“Degrees and %f degrees east longitude in the\n”, east2);
printf(“northern hemisphere in south-east Asia.\n”);
getch();
}

WRITE A PROGRAM TO READ IN 3 INTEGERS AND PRINT THEIR SUM?
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int value1, value2, value3, sum;
printf(“Enter first value:”);
scanf(“%d”,&value1);
printf(“\n Enter second value:”);
scanf(“%d”,&value2);
printf(“\n Enter third value:”);
scanf(“%d”,&value3);
sum = value1 + value2 + value3;
printf(“\nSum of 3 integers = %d, sum);
getch();
}

WRITE A PROGRAM THAT READS AND PRINTS USING ESCAPE SEQUENCE, ASKING THE NAME, AGE, HEIGHT AND GENDER OF STUDENTS USING “scanf” AND “printf” STATEMENTS?
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
char name[20], gender[6];
int age;float height;printf(“Enter your name:”);
scanf(“%s”,& name);
printf(“\n Enter your age:”);
scanf(“%d”,& age);
printf(“\n Enter your height:”);
scanf(“%f”,& height);
printf(“\n Enter your gender:”);
scanf(“%s”,& gender);
printf(“\n\n Your name is %s.“, name);
printf(“\n You are %d years old.“, age);
printf(“\n Your height is %f feet.“, height);
printf(“\n You are %s.“, gender);
getch();
}

WRITE A PROGRAM WHICH USES OPERATORS (CALCULATE THE AREA OF TRIANGLES, VOLUME OF SPHERE AND ARRANGE THE RESULTANT VALUES IN ASCENDING ORDER)?
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(void)
{
clrscr();
float a, b, c, s, area, d, vol;
printf(“Enter triangle side A:”);
scanf(“%f”,& a);
printf(“\n Enter triangle side B:”);
scanf(“%f”,& b);
printf(“\n Enter triangle side C:”);
scanf(“%f”,& c);
printf(\n Enter diameter of sphere:”);
scanf(“%f”,& d);
s = (a + b + c) /2;
area = sqrt (s * (s - a) * (s - b) * (s - c));
vol = (3.14 / 6) * (d * d * d);
if(vol> area)
{
 printf(“\n\n Area of triangle = %f”, area);
printf(“\n Volume of sphere = %f”, vol);
}
else
{
 printf(“\n Volume of sphere = %f”, vol);
printf(“\n Area of tringle = %f”, area);
}
getch();
}

WRITE A PROGRAM TO READ IN 5 NUMBERS AND COMPUTE THE AVERAGE, MAXIMUM AND MINIMUM VALUES?
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
float in, sum = 0.0, max = 1000000.0, min = -1000000.0;
int i;
for(i = 1; i<=5; i++)
printf(“\n Enter number %d:”, i + 1);
scanf(“%f”,& in);
if(min < in)
min = in;
if(max > in)
{ 
max = in;
sum = sum + in;
}
printf(“\n Largest = %f, Smallest = %f”, min, max);
printf(“\n Average %f \n”, sum / 5);
getch();
}

WRITE A PROGRAM WHICH USES “for” LOOP STATEMENT (GENERATE THE MULTIPLICATION TABLE FROM 2 TO 20)?
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int x, y, num;
for(x = 2; x<21; x++)
{
 for(y = 1; y<=10; y++)
{
 num = x * y;
printf(%d * %d = %d \n”, x, y, num);
}
}
getch();
}

WRITE A PROGRAM WHICH USES “while” LOOP AND NESTED “while” LOOP(USE “for” LOOP AND CONTINUE THE PROCESS IN “while” LOOP SATISFYING THIS CONDITION)?PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int x, loop, count;
for(x = 1; x <3; x++)
{
 loop = 1;
while(loop <= 5)
{
 count = 1;
while(count <= loop)
{
 printf(“%d”, count);count++;
}
printf(“\n”);
loop++;
}
}
getch();
}

FINDING THE FACTORIAL OF ‘N’ USING “while” LOOP, READ VALUE OF ‘N’ USING “scanf” AND “printf” FACTORIALS OF VARIOUS ‘N’?
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int n, result = 1, counter = 2;
printf(“Enter the value of N:”);
scanf(“%d”,& n);
while(counter <= n)
{
 result = counter * result;
counter += 1;
}
printf(“\n\n Factorial of N is %d”, result);
getch();
}

WRITE A PROGRAM WHICH USES A “switch” STATEMENT AND BREAKS THE PROGRAM IF CERTAIN CONDITION IS OBSERVED. REPEAT THE PROGRAM WITH CASE STAEMENT? 
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int month;
do
{
 printf(“Type the number of a month:”);
scanf(“%d”,& month);
printf(“\n\n Name of month %d is “, month);
switch (month)
{
case 1 : printf(“January”);
 break;
case2 : printf(“February”);
 break;
case3 :
 printf(“March”);
 break;
case4 : 
printf(“April”);
 break;
case5 : 
printf(“May”); 
break;
case6 :
printf(“June”);
 break;
case7 :
 printf(“July”);
 break;
case8 :
 printf(“August”); 
break;
case9 :
 printf(“September”); 
break;
case10 :
 printf(“October”);
 break;
case11 : 
printf(“November”);
 break;
case12 :
 printf(“December”);
 break;
}
}
while (month < 1 || month > 12);
getch();
}

WRITE A FUNCTION, WHICH GENERATES FACTORIAL OF ‘N’ AND CALLS THIS FUNCTION IN THE “main” PROGRAM?
PROGRAM:
#include <stdio.h>
#include <conio.h>
int factorial(int x);
void main(void)
{
clrscr();
int n, f;
printf(“Enter the value of N :”);
scanf(“%d”,& n);
f = factorial(n);
printf(“\n\n Factorial of N is %d”, f);
}
int factorial(int x)
{
int i, fact = 1;
for(i = 2; i<=x; i++)
fact = fact * i;
return fact;
}

WRITE A PROGRAM WHICH USES MULTIPLE ARGUMENTS IN A FUNCTION (DEVELOP A USER-DEFINED FUNCTION TO GENERATE A RECTANGLE). USE THE FUNCTION FOR PASSING ARGUMENTS TO DRAW DIFFERENT SIZES OF RECTANGLES AND SQUARES?
PROGRAM:
#include <stdio.h>
#include <conio.h>
void box(int length, int width);
void main(void)
{
clrscr();
box(5,5);
box(5,10);
box(7,7);
box(3,8);
}
void box(int length, int width)
{
int a, b, x;
for(a = 1; a <= width; a++)
print(“*”);
for(b = 1; b <= length; b++)
{
x = 0;
printf(“\n*”);
}
while(x <= (width – 3))
{
x = x + 1;
printf(“  ”);
 }
printf(“\n”);
for(a = 1; a <= width; a==)
printf(“*”);
printf(“\n”);
}
getch();
}

No comments:

Post a Comment