Project work
of Basic C programming
By: Surakshya panthi To: Rupesh Sir
Section: A Subject: C Programming
{Workshop}
# Questions
1. WAP to display “Hello World”.
2. WAP to add two numbers.
3. WAP to take three numbers and find average.
4. WAP to find simple interest.
5. WAP to find area of triangle taking length of
sides from user.
6. WAP to find compound interest, taking
principle, rate and time from user.
7. WAP which takes temperature in Fahrenheit
and converts into Celsius.
8. WAP to demonstrate different operators in C.
9. WAP to illustrate increment or decrement
operators.
10. WAP to demonstrate different format
specifiers.
11. WAP to convert upper case letter into lower
case letter.
1.
#include<stdio.h>
void main()
{
puts("Hello World.");
}
➔ Discussion:
Here, stdio.h is a type of header file which various
standard input output functions which can be only
accessed after declaring it. The main() function is
used to declare an entry point of a program. The
puts or printf function is used to show a output to
the user on the terminal. In this program, Hello
World is printed to user by puts function.
2.
#include<stdio.h>
void main()
{
int a,b;
printf("Enter any 2 numbers.");
puts("\n");
scanf("%d %d",&a,&b);
printf("The sum of two numbers is %d.",a+b);
}
➔ Discussion:
Here, int is a datatype that declares the used
variable to be an integer. Also, scanf is used to
read and assign a value entered by the use.
Similarly, %d is the place holder of the used
integer.
3.
#include<stdio.h>
void main()
{
float a,b,c,avg;
printf("Enter any 3 numbers.");
puts("\n");
scanf("%f %f %f",&a,&b,&c);
avg=(a+b+c)/3;
printf("The average of 3 numbers is:%f",avg);
}
➔ Discussion:
Here, we use float datatype to store fractional
values which are in decimal place. %f is a place
holder for the float values. Also, we use
parenthesis or small brackets while adding the
integers as, if it is missing, according to BODMAS
rule division takes place prior to addition,
causing error in calculations.
4.
#include<stdio.h>
void main()
{
float p,t,r,si;
printf("Enter the principle rate and time.");
puts("\n");
scanf("%f %f %f",&p,&t,&r);
si=(p*t*r)/100;
printf("The simple interest is:%f",si);
}
➔ Discussion:
Here, we use (&) during taking value from user,
as when we pass variable to function without
ampersand (&) it only passes their values but
their address information of actual variable isn’t
passed. Also, here parenthesis or small bracket
isn’t compulsory because during multiplication,
division doesn’t really affect the multiplication.
5.
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,s,z,area;
printf("Enter the 3 sides of triangle.");
puts("\n");
scanf("%f %f %f",&a,&b,&c);
s=(a+b+c)/2;
z=s*(s-a)*(s-b)*(s-c);
area=sqrt(z);
printf("The area of triangle is:%f",area);
}
➔ Discussion:
Here, math.h is a type of header file that
contains various mathematical functions such as
sqrt, pow and so on. The use of the function
sqrt() is to square root the variable and assign it.
6.
#include<stdio.h>
#include<math.h>
void main()
{
float p,t,r,x,y,ci;
printf("Enter the principle, time and rate.");
printf("\n");
scanf("%f %f %f",&p,&t,&r);
x=1+(r/100);
y=pow(x,2);
ci=(p*y)-p;
printf("The compound interest is:%f",ci);
}
➔ Discusssion:
Here, pow function is a math.h exclusive
function that is used to make a variable the
desired power needed by user. Also, the \n is
used to go to the next line on the terminal.
7.
#include<stdio.h>
void main()
{
float f,c;
printf("Enter the temperature in fahrenheit.");
printf("\n");
scanf("%f",&f);
c=(f-32)*5/9;
printf("The temperature in celsius is:%f",c);
}
➔ Discussion:
Here, we use division after other athematic
operations as, if division was used first and the
divided value was less than 1(i.e., 0.~~) then
during multiplication it would have been
counted as 0 and our output would always be 0
no matter how high the other values are input
by user.
8.
#include<stdio.h>
void main()
{
printf("There are various operators in C. \n\n");
printf(" 1. Arithmetic Operators \n");
printf("-Addition[+]\n-Subtraction[-]\n-
Multiplication[*]\n-Division[/]\n-
Modulus[%]\n\n");
printf(" 2. Relational Operators \n");
printf("-Less Than[<]\n-Greater Than[>]\n-Less
Than or Equal To[<=]\n-Greater Than or Equal
To[>=]\n-Equal To[=]\n-Not Equal To[!=]\n\n");
printf(" 3. Logical Operators \n");
printf("-Logical And[&&]\n-Logical Or[||]\n-Logical
Not[!]\n\n");
printf(" 4. Other Useful Operators \n");
printf("-Increment[++]\n-Decrement[--]\n-
Assignment[=]\n");
}
➔ Discussion:
Here, there are various
operators used in C which
are used for various things
such as arithmetic operator
are used for mathematical
calculations, relational
operator are used for
comparisons, logical
operator are used to filter
multiple conditions to
find the true or false value,
other operators such as
increment and decrement
are used in loop for the
increase or decrease in
value and assignment
operator is used to give a
certain variable a value to store.
9.
#include<stdio.h>
void main()
{
int a=2,b,c,d,e;
b=++a;
c=b++;
d=c--;
e=--d;
printf("a=%d \n b=%d \n c=%d \n d=%d \n
e=%d",a,b,c,d,e);
}
➔ Discussion:
Here, (++a) is pre-increment, (b++) is post-
increment, (c--) is post-decrement and (--d) is pre-
decrement. In pre-cases, the value is changed then
assigned whereas in post-cases, the value is first
assigned then changed.
10.
#include<stdio.h>
void main()
{
int a=5;
float b=3.6;
char c='C';
double d=1;
printf("%d is an integer.\n",a);
printf("%f is a float.\n",b);
printf("%c is a character.\n",c);
printf("%lf is a double.\n",d);
}
➔ Discussion:
Here, %d denotes an integer and stores real
numbers. %f denotes a float and stores decimal
numbers. %c denotes a character and stores string.
%lf denotes a double and can store integer, float
and string. There are other such format specifiers
in C too such as: %Ld, %ld, %x, %p and so on.
11.
#include<stdio.h>
#include<ctype.h>
void main()
{
char a,b;
printf("Enter a character.\n");
scanf("%c",&a);
b=tolower(a);
printf("The lowercase of %c is %c.",a,b);
}
➔ Discussion:
Here, ctype.h is a type of header file that stores
various functions revolving around strings. Among
them, tolower() is a type of such function which is
used to lower the case of string from uppercase to
lowercase. For the use of this on multiple string, a
loop containing strlen() should be used which
measures the length of a string.