Tuesday, December 31, 2013

C Program to Define the Prime Number from the Provided Number

Objectives
-to find out the Prime/Largest Number from the provided number
-to learn the use of else if condition
-to learn to declare float-type variables
-to learn how to input several data from keyboard
-to know how to compare several data after providing by keyboard
-to learn how to show only two digit after the point in a float-type number
When you will run the C Program given below, then the text Enter the First Number: will appear on the screen. That means, you have to input a number from the keyboard. When you will press the Enter Key after providing the first data, again a line Enter the Second Number: will appear. Similarly you will be asked for the 3rd number. Finally, you will get a result like- .... ....  is the Largest Number.

#include<stdio.h>
void main()
{
float n1,n2,n3;
printf("Enter the First Number: ");
scanf("%f",&n1);
printf("Enter the Second Number: ");
scanf("%f",&n2);
printf("Enter the Third Number: ");
scanf("%f",&n3);
if(n1>n2&&n1>n3)
printf("%.2f is the Largest Number",n1);
else if("n2>n1&&n2>n3")
printf("%.2f is the Largest Number",n2);
else
printf("%.2f is the Largest Number",n3);
}
prime number,largest number,c program,if else condition
Description of the above codes
float n1,n2,n3;
The above line is the fourth line of the program.  In this line I have declared three float-type variables n1, n2 and n3. We will keep three numbers into these three variables and later we will compare them with one another to find out the largest one. int & char are two more data type. We may use int for integer type data and char for character type data. Semicolon (;) is must at the end of the line where we will declare variables. The variables will be separated by comma (,).

printf("Enter the First Number: ");
This is the fifth line of the above program. By this line, the program will print the writings Enter the Number: on the screen. You have to provide a number after showing this line. You may print anything you like to show by replacing the line by printf("text you want to show"); The rest of the line is must. The seventh and the ninth line are playing the same role in the program.

scanf("%f",&n1);
This is the sixth line of the program. The program will get the data by this line that we will provide using keyboard. The data will be stored into a variable named n1, we declared at the fourth line. Here, we have  to use %f as we will input float-type data. Similarly we need to use %d for integer type data and %c for character type data. The eighth and the tenth line are same to this sixth line. In eighth line the program will store the another data into the variable n2 and in tenth line the data will be stored into the variable n3. Later we will use this tree data to compare each other.

if(n1>n2&&n1>n3)
The program will perform a logical operation during executing this line. In this line, the program will compare n1 with n2 and n3. If the program found that n1 is greater than the rest two then it will execute the next line printf("%.2f is the Largest Number",n1); In this line I have told the program to show the data we kept into the variable n1. %f is enough to define a float type data during printing on the screen. But I have used %.2f. Because, I want to show two digit after the point. That means the result will be 8.13 or something like this. If we use %.3f, then the result will be 8.132

else if("n2>n1&&n2>n3")
If the first condition become false then the program will execute this line. In this line n2 will be compared with n1 and n3. If n2 is greater than the rest two then the program will execute the fourteenth line printf("%.2f is the Largest Number",n2); and will show the data kept into the variable n2. Otherwise the program will execute the line else and will show the data kept into the variable n3.


No comments:

Post a Comment