Lab Session - 1 [Data Types, Input-output Functions]
Lab Session - 1 [Data Types, Input-output Functions]
--------------------------------------------------
----
1. Write a program (WAP) to display "Hello World"
#include <stdio.h>
#include <conio.h> //pre-processor directives
void main() /*main program starts*/
{
clrscr();
printf("This is my first C program\n");//output function
printf("Hello World");
}
Alternatively:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("This is my first C program\n");
printf("Hello World");
getch(); //holds the output screen until a character is entered by
the user. This allows us to view the output.
}
Press: Ctrl + F9
2.
/*Program to initialize int, char, float data types*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n=78; //variable declaration
float j=3.0; //variable declaration
char x='y'; //variable declaration
clrscr();
printf("Integer=%d\tFloat Value=%f\tCharacter=%c",n,j,x);
getch();
}
3.
/*Program to accept values of int, char, float data types from user
and Display*/
#include<stdio.h>
#include<conio.h>
void main()
{
char x;
int num;
float j;
system("cls"); //alternative to clrscr()
/*Accept the values for data types from user*/
printf("Enter Character: ");
scanf("%c",&x);
printf("Enter Integer Value: ");
scanf("%d",&num);
printf("Enter Float Value: ");
scanf("%f",&j);
/*Display the accepted values*/
printf("Integer=%d\tFloat Value=%f\tCharacter=%c",num,j,x);
}
4.
/*Program to accept characters and display using unformatted input-
output functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
char x,y,z;
clrscr();
printf("Enter 1st character: ");
x = getchar();//displays entered value and waits for \n
printf("Enter 2nd character: ");
y = getche();//displays entered value but does not wait for \n
printf("\nEnter 3rd character: ");
z = getch();//doesn’t display entered value and doesn’t wait for \n
printf("\nFirst character is ");
putchar(x);
printf("\nSecond character is ");
putch(y);
printf("\nThird character is ");
putchar(z);
getch();
}
5. WAP to read two numbers, add them, and display their sum
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d",&a);//input function
scanf("%d",&b);
printf("Sum is: %d",a+b);
getch();
}
Alternatively:
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Sum is: %d",a+b);
getch();
}
6. WAP to evaluate the arithmetic expression (a -b/c *d+ e) and display
solution. Get the values of the variables from the user through
console. (Understanding the nature of float and int variables)
#include<stdio.h>
int main()
{
int a, b, c, d, e;
float x;
system("cls");
printf("Enter values of a, b, c, d, & e: ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
x=a-b/c*d+e;
printf("(a-b/c*d+e) = %f",x);
getch();
return 0;
}
Note: To get the correct output, try declaring a,b,c,d,e as float
instead of int. Then change %d to %f for float in scanf.
7. WAP to read the radius of a circle, calculate its area and display it
#include <stdio.h>
#include <conio.h>
void main()
{
float r, pi=3.1416;
clrscr();
printf("Enter radius: ");
scanf("%f",&r);
printf("Area of circle is: %f",pi*r*r);
getch();
}
Alternatively:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float r,A;
clrscr();
printf("Enter radius: ");
scanf("%f",&r);
A=M_PI*pow(r,2); //using <math.h> constant and function
printf("Area of circle is: %f",A);
getch();
}
8. WAP to calculate simple and compound interest.
Finally, CI will be = A – P
Simple interest:
#include <stdio.h>
#include <conio.h>
void main()
{
int p;
float r,t,SI;
clrscr();
printf("Enter principal, rate of interest & time (years): ");
scanf("%d %f %f",&p,&r,&t);
SI=p*r*t/100;
printf("Simple interest is: %f",SI);
getch();
}
Compound interest:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int p,n;
float r,t,A,CI;
clrscr();
printf("Enter principal, rate of interest, frequency of
compounding & time (years): ");
scanf("%d %f %d %f",&p,&r,&n,&t);
A=p*pow(1+r/(100*n),n*t);
CI=A-p;
printf("Compound interest is: %f\n",CI);
printf("Simple interest is: %f",p*r*t/100);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2;
system("cls");
printf("Enter the coefficients a,b,c of the quadratic
equation: ");
scanf("%f %f %f",&a,&b,&c);
r1=(-b+sqrt(pow(b,2)-4*a*c))/(2*a);
r2=(-b-sqrt(pow(b,2)-4*a*c))/(2*a);
printf("The roots are: %0.2f and %.3f",r1,r2);
getch();
}
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b,c;
clrscr();
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("After swap, a = %d, b = %d",a,b);
getch();
return 0;
}
11. WAP to swap values of two variables without using a third variable
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b;
clrscr();
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
a=a+b;//alternatively a=a*b
b=a-b;//alternatively b=a/b
a=a-b;//alternatively a=a/b
printf("After swap, a = %d, b = %d",a,b);
getch();
return 0;
}
----------------------------------X----------------------------------