Friday, 28 March 2014
0
Friday, 28 March 2014
IndianBooktuber
read more
C Program 7: To find the area and perimeter of rectangle and circumference of circle
Aim: The length & breadth of a rectangle and radius of circle are input through the keyboard. Write a program to calculate the area and perimeter of the rectangle, and the circumference of the circle.
Theory: you need to know about three mathematical formulas and here you go.. This is the code..
//to find area and perimeter of rectangle and circumference of circle
#include<stdio.h>
#include<conio.h>
int main()
{
int l,b,i;
float r, area, peri, circum;
printf("Calculating the area and perimeter of rectangle\n");
printf("Enter the Length and breadth of rectangle: ");
scanf("%d%d", &l, &b);
peri= 2*(l+b);
area= l*b;
printf("The area of the rectangle is: %f\n", area);
printf("The perimeter of the rectangle is: %f\n", peri);
for(i=0; i<80; i++)
{
printf("*");
}
printf("\n");
printf("Calculating the radius of circle\n");
printf("Enter the radius of the circle\n");
scanf("%f", &r);
circum=2*3.14*r;
printf("The cirumference of the circle is: %f", circum);
getch();
return 0;
}
First we calculated the area and perimeter of rectangle using formulae:
scanf("%d%d", &l, &b);
peri= 2*(l+b);
Then we calculate the circumference of circle using formula: circum=2*3.14*r;
Ignore the for loop used inside the program. That isn't actually required. I just used it to make the output look more organized.
Here's the output:
1
IndianBooktuber
read more
C Program 6: To convert the temperature from Fahrenheit to Celsius.
Aim: Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.
Theory: This is one of the basic programs that one should know to make in the C but the only tough part is remembering the formula for conversion of temperature from one scale to another. There is no logic in that. It's just a mathematical formula which you need to keep in mind. The Formula is C= 5(F+32)/9
Code:
//to convert temperature from Fahrenheit to Celsius
#include<stdio.h>
#include<conio.h>
int main()
{
float fahf, celf;
printf("Enter the temperature in Fahrenheit: ", fahf);
scanf("%f", &fahf);
celf=5*(fahf-32)/9;
printf("The temperature in Celsius is: %f", celf);
getch();
return 0;
}
We just took two variables and then used the formula which I mentioned above in the theory portion. Here's the output:
1
IndianBooktuber
read more
C Program 5: To find the average and percentage of marks obtained
Aim: If marks obtained by a student in five different subjects are input through the keyboard, write a program to find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.
Theory: Again, as in the previous program, this program also needs to basic mathematical formulae.
1. To find the average. The formula is to calculate sum of marks obtained in five subjects/ total number of subject
2. To find the percentage. The formula is to calculate ( the sum of marks obtained*100)/total marks obtained.
The code for this program is:
//to find average and percentage of marks obtained in five subject (out of 100)
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,d,e;
float sum,avg, per;
printf("Enter the marks obtained in five subjects: ");
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
sum=a+b+c+d+e;
avg=sum/5; //calculating average marks
per=sum*100/500; //calulating percentage
printf("The average marks are:%f\n", avg);
printf("The percentage is: %f\n", per);
getch();
return 0;
}
We asked the user to enter the marks obtained in five subjects. Then we calculate their sum and apply formula for finding average marks obtained and the percentage of the marks obtained.
The output would be like this:
7
IndianBooktuber
read more
C Program 4: To Convert distance in Km to cm, feet, inch and meter.
Aim: the distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.
Theory: This program needs no complex coding. It's a simple program where you need to know four basic mathematical formulae. Once you know them, you can easily code this program in C.
Here's the code:
//to convert distance from km to m, cm, feet and inches
#include<stdio.h>
#include<conio.h>
int main()
{
float km, m, cm, feet, inch;
printf("Enter the distance in kilometers: ");
scanf("%f", &km);
m=km*1000;
cm=m*100;
feet=km*3280.8;
inch=feet*12;
printf("The distance in meters is %f\n", m);
printf("The distance in centimeters is %f\n", cm);
printf("The distance in feet is %f\n", feet);
printf("The distance in inches is %f\n", inch);
getch();
return 0;
}
We used four formulae in the code and calculated the values and then printed the result. That's all. It's too easy. Isn't it?
Here's the snapshot of output:
7
IndianBooktuber
read more
C Program 3: To calculate the gross salary when the basic salary is entered through keyboard
Aim: Ramesh's basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
Theory: We need to take input from user and then apply the simple formula to calculate the gross salary i.e. gross salary= basic salary+ dearness allowance+ house rent allowance.
Following would be the program code:
//to calculate the gross salary
#include<stdio.h>
#include<conio.h>
int main()
{
int bs, gs, da, hr;
printf("Enter the basic salary:");
scanf("%d", &bs);
da=40*bs/100; //given that dearance allowance is 40% of basic salaray
hr=20*bs/100; //given that house rent is 20% of basic salaray
gs= bs + da + hr; //formula to calculate gross salary
printf("The gross salary is: %d", gs);
getch();
return 0;
}
First we asked the user to enter the basic salary and then take the input using the scanf("%d", &bs); statement. Now, we calculate the dearness allowance using this statement da=40*bs/100; Similary we will calculate the house rent allowance according to the values mentioned in the question and then we would use our main formula to calculate the gross salary i.e. gs= bs + da + hr;
Outpur:
Monday, 24 March 2014
0
Monday, 24 March 2014
IndianBooktuber
read more
C Program 2: To Reverse A Number Using An Array
Aim: To reverse a number(digits entered) using an array
Theory: At first, we would need to ask the user about how many digits he is going to enter or what is the length of the number which he wants to reverse. Then we would ask him to enter the digits of the number one by one and then we reverse the digit and print the reverse number.
Following would be the code:
We would use two for loops in the program to accomplish our objective. Before that, we would ask the user to enter the digits he is going to enter. Then, using a for loop, we would get the input one by one. The loop
The next loop with 'j' is used to reverse the number. We use decrement operator in the loop so as to get the number reversed.
What actually happens is, we now print the array from backwards, for eg. when you entered a number 12, the numbers were stored like this in array:
a[0]=1;
a[1]=2;
When you used the decrement counter, you also changed the initialization and condition in the second for loop. The for loop begins with its execution from n, i.e. it first prints a[n] or a[1] in this case.
It works like:
a[1]=2;
a[0]=1;
Thus, the result becomes 21.
Theory: At first, we would need to ask the user about how many digits he is going to enter or what is the length of the number which he wants to reverse. Then we would ask him to enter the digits of the number one by one and then we reverse the digit and print the reverse number.
Following would be the code:
//to reverse the digits in an array
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,k, a[10];
a[10]=0;
printf("enter the number of digits of number you want to reverse: ");
scanf("%d", &n);
n=n-1;
printf("enter the digits one by one: \n");
for (i=0; i<=n; i++)
{
scanf("%d",&a[i]);
}
for (j=n; j>=0; j--)
{
printf("%d", a[j]);
}
getch();
return 0;
}
We would use two for loops in the program to accomplish our objective. Before that, we would ask the user to enter the digits he is going to enter. Then, using a for loop, we would get the input one by one. The loop
for(i=0; i<=n; i++)
serves this purpose.The next loop with 'j' is used to reverse the number. We use decrement operator in the loop so as to get the number reversed.
What actually happens is, we now print the array from backwards, for eg. when you entered a number 12, the numbers were stored like this in array:
a[0]=1;
a[1]=2;
When you used the decrement counter, you also changed the initialization and condition in the second for loop. The for loop begins with its execution from n, i.e. it first prints a[n] or a[1] in this case.
It works like:
a[1]=2;
a[0]=1;
Thus, the result becomes 21.
0
IndianBooktuber
read more
C Program 1: To Print The Sum of First Five Natural Numbers Using Arrays
Aim: To print the sum of first five natural numbers using arrays
Theory: For achieving the desired result, we need to put first five natural numbers in form of array. So, we use a for loop so as to enter the first give natural numbers in an array and then calculate their sum.
The code for the following would be:
Theory: For achieving the desired result, we need to put first five natural numbers in form of array. So, we use a for loop so as to enter the first give natural numbers in an array and then calculate their sum.
The code for the following would be:
//to print the sum of first 5 natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a[10], sum=0;
for (i=0; i<=4; i++)
{
a[i]=i;
printf("%d\n", a[i]);
sum=sum+a[i];
}
printf("The sum is %d", sum);
getch();
return 0;
}
Note that we would initialize the variable for storing the sum to 0. If we don't do that, the sum would initially have a garbage value which would give wrong output.
The first statement
Thus, at the end we get the desired output.
The first statement
a[i]=i;
would allow us to store first five natural numbers in the array form. Then using the next statement, we printed all the first five natural numbers and then we calculate their sum using the statement sum=sum+a[i];
Thus, at the end we get the desired output.
Friday, 21 March 2014
1
Friday, 21 March 2014
IndianBooktuber
read more
What To Expect Here?
I knew I could simply start with posting the C Programs which I need to code myself just to increase my efficiency in C programming. In fact, I have already posted a few on PerCepTion which is my personal blog but I felt it would not be a good idea to clutter my personal blog with C Programs. Many of my readers are already disappointed with that move so I decided to make things right and started this one.
So, by now, from the title you can guess that this blog is going to have 100 C Programs which I will be going to code to prepare myself for the campus placement interviews which I am going to have in the month of August.
Till then I intend to post programs here so as to share my work with each one of you who would read this blog.
Well, you might be thinking what's the difference? There are hundreds of blogs which already have this kind of structure and posts; then why should you come here often?
You would see the difference once I will start posting. Not many bloggers explain their code that perfectly which is quite a bad thing. And that's why I felt the need for this..
So stay tuned.. I hope you enjoy visiting this blog :)
So, by now, from the title you can guess that this blog is going to have 100 C Programs which I will be going to code to prepare myself for the campus placement interviews which I am going to have in the month of August.
Till then I intend to post programs here so as to share my work with each one of you who would read this blog.
Well, you might be thinking what's the difference? There are hundreds of blogs which already have this kind of structure and posts; then why should you come here often?
You would see the difference once I will start posting. Not many bloggers explain their code that perfectly which is quite a bad thing. And that's why I felt the need for this..
So stay tuned.. I hope you enjoy visiting this blog :)
Subscribe to:
Posts (Atom)