C Program to check whether inputed number is a Prime Number or not by using for loop without using mid value...
//C Program to check wether inputed number
//is a prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, c=0, i;
printf("Enter a number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
getch();
}
Loop is used to execute the block of
code several times according to the given condition in loop. It means it
executes the same code multiple times.
There are 3 types of loop
–
01.while loop
02.do – while loop
03.for loop
1. while Loop –
While loop is also
known as a pre-tested loop. In general, a while loop allows a part of the code
to be executed multiple times depending upon a given condition. The while loop
is mostly used in the case where the number of iterations is not known in
advance.
Syntax of while loop :
while(condition)
{
statements – 1
statements – 2
.
.
statements – n
//increment / decrement operator if required
(optional)
}
Flowchart of while loop :
e.g.
//Program for printing 1 to 10 number
//by using while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("%d
",i);
i++;
}
getch();
}
2. do – while loop
It also executes the code until
condition is false. In this at least once, code is executed whether condition
is true or false but while loop is executed only when the condition is true.
Syntax :
do
{
statements – 1
statements – 2
.
.
statements – n
}
while(condition);
Flowchart of do-while loop :
e.g.
//Program for printing 1 to 10 number
//by using do-while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
do
{
printf("%d
",i);
i++;
}
while(i<=10);
getch();
}
3. for Loop
It also executes the code until
condition is false. In this three parameters are given to it these are Initialization,
Condition & Increment/Decrement operators
Switch-Case statement in C allows us to
execute multiple operations for the different possible values of a single
variable called switch variable. Here, We can define various statements in the
multiple cases for the different values of a single variable. A switch statement
allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for
each switch case.
Syntax :
switch
(expression)
{
case
value1:
{
Statements
of case 1
break;
}
case
value2:
{
Statements
of case 2
break;
}
case
value3:
{
Statements
of case 3
break;
}
.....
default:
{
code to be executed if all cases are not
matched;
}
} //End of switch case statement
Rules for switch statement in C language
ØThe
case value must be an integer or character constant.
ØThe
case value can be used only inside the switch statement.
ØThe
switch expression must be of an integer or character type.
The goto
statement is a jump statement which is sometimes also referred to as
unconditional jump statement. The goto statement
can be used to jump from anywhere to anywhere within a function.
C
Language में goto स्टेटमेंट एक जम्प स्टेटमेंट है कभी-कभी इसे अनकंडीशनल स्टेटमेंट भी कहते
है। C प्रोग्राम में कही से कही भी जम्प करने के लिए goto स्टेटमेंट का प्रयोग करते है।
Youtube Video
Syntax
1 –
label:
.
.
.
goto label;
Syntax
2 -
goto label;
.
.
.
label:
//
C program to print numbers
//
from 1 to 10 using goto statement
#include <stdio.h>
#include <conio.h>
void main ( )
{
int n = 1;
label:
printf("%d\n",n);
n++;
if (n <= 10)
goto label;
}
//Program
to demonstrate
//goto
statement in C Program
#include <stdio.h>
#include <conio.h>
void main ( )
{
int num;
clrscr ( );
printf("Enter any
number");
scanf("%d",&num);
if (num % 2 == 0)
// jump to even
goto even;
else
// jump to odd
goto odd;
even:
printf("%d is even", num);
// return if even
getch();
return;
odd:
printf("%d is odd", num);
getch();
}
Disadvantages
of using goto statement:
It makes the program logic very
complex.
Use of goto
makes the task of analyzing and verifying the correctness of programs very
difficult.
Simply avoided using break and cnontinue
statements.
In else – if a programmer can
make decision among multiple conditions. In C "if
statements" are executed from the top
down approach. As soon as one of the if is true, the statement associated with if is executed, and the rest
of the C else – if
is bypassed. If none of the conditions are true, then the final else statement will be
executed.
C Program to understand the concept of "if-else" statement in C by Satyam Srivastava
If-else Statement
The if-else statement is used to
perform two operations for a single condition. The if-else statement is an extension to the if statement using which,
we can perform two different operations, i.e., one is for the true condition,
and the other is for the false. Here, we must notice that if and else block cannot be executed
in same time. The syntax of the if-else statement is given below.
An if statement consists of a Boolean expression followed by one or more statements.
Syntax :
The syntax of an if statement in C programming language is −
if (condition)
{
statement(s) will execute only if the boolean expression is true
}
If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the 'if' statement (after the closing curly brace) will be executed.
/*Program
to demonstrate if statement in C WAP
to input total mark and mark obtained by student
and determine his result according to given condition if
percentage less than 50% then fail otherwise pass.*/
#include<stdio.h>
#include<conio.h>
void
main()
{
clrscr();
int
mo, tm, per;
printf("Enter
total Marks\n");
scanf("%d",&tm);
printf("Enter
Marks obtained by student\n");
scanf("%d",&mo);
per=mo*100/tm;
printf("Percentage
obtained by student = %f",per);