Monday, September 12, 2022

C Program for Prime Number

C Program For Prime Number

 //C Program for Prime Number

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int n, m, i, c=0;

printf("Enter a Number\n");

scanf("%d",&n);

m=n/2;

for(i=2;i<=m;i++)

{

if(n%i==0)

{

printf("%d is not a prime number",n);

c++;

break;

}

}

if(c==0)

{

printf("%d is a Primne number",n);

}

getch();

}

YouTube Video

(Please Subscribe our Channel)


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(); }

YouTube Video

(Please Subscribe our Channel)



Saturday, September 10, 2022

Looping statement in C Language

 Looping Statement in C

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

Syntax

for(initialization; condition; increment/decrement)

{

statements – 1

statements – 2

.

.

statements – n

}

Flowchart of for loop :



e.g.

//Program for printing 1 to 10 number

//by using for loop

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

for(i=1; i<=10; i++)

{

            printf("%d ",i);

}

getch();

}

Youtube video for program



Switch-Case Statement in C

 Switch-Case Statement in C

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.

Flowchart of switch statement in C

Example Program :

//Example program of swith case statement

//satyamsrig.blogspot.in

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int ch;

printf("Enter the number of month\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

{

printf("January");

break;

}

case 2:

{

printf("February");

break;

}

case 3:

{

printf("March");

break;

}

case 4:

{

printf("April");

break;

}

case 5:

{

printf("May");

break;

}

case 6:

{

printf("June");

break;

}

case 7:

{

printf("July");

break;

}

case 8:

{

printf("August");

break;

}

case 9:

{

printf("September");

break;

}

case 10:

{

printf("October");

break;

}

case 11:

{

printf("November");

break;

}

case 12:

{

printf("December");

break;

}

default :

{

printf("You have entered wrong keyword");

}

}

getch();

}

Example Program Youtube Video :



Sunday, September 4, 2022

"goto" statement in C Language

 goto statement in C

 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.

Tuesday, August 30, 2022

C Program to Demonstrate else-if statement

if-else-if in C

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.

Syntax: 


if (condition)

    statement;
else if (condition)
    statement;
else if (condition)
    statement;
else if (condition)
    statement;
.
.
else

    statement;
Youtube Video


Flow Chart



Sorce Code

 //WAP to enter any three number

//and find the largest

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int n1, n2, n3;

printf("Enter any three numbers \n");

scanf("%d%d%d",&n1,&n2,&n3);

if(n1>n2 && n1>n3)

printf("%d is larger than %d and %d",n1,n2,n3);

else if(n2>n1&&n2>n3)

printf("%d is larger than %d and %d",n2,n1,n3);

else

printf("%d is larger than %d and %d",n3,n1,n2);

printf("\nEnd of program");

getch();

}


Tuesday, August 9, 2022

C Program to understand the concept of "if-else" statement in C by Satyam Srivastava

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.

if(condition)

{  

//code to be executed if condition is true  

}

else

{  

//code to be executed if condition is false  

}  

Youtube Video

 Flow Chart

Sorce Code

//WAP to understand the concept of
//if-else statement
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf("Enter any number\n");
scanf("%d",&n);
if(n % 2 == 0)
{
printf("Even number");
}
else
{
printf("Odd number");
}
getch();
}

C Program to to understandthe concept of “if” statement

C - if statement

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);

if(per<50)

            {

            printf("\nFail");

            }

if(per>=50)

            {

            printf("\nPass");

            }

getch();

}

 

C Program to show the use of Mathematical Expressions


 

Program to find the sum of two number


 

Installation of Turbo C on windows PC