sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to...

36
CO-2 Session 12- ANALYSIS OF ALGORITHMS Time Complexity: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…) Asymptotic Notations: It is the meaningful way of representation of time complexity. They are i) Big Oh [ O (g(n)) ] { Upper bound } ii) Big Omega [ Ω (g(n)) ] { Lower bound } iii) Big Theta [ Ɵ (g(n)) ] { Average } The Order increases in this format: O (1), O (log n), O (n/2), O (n), O (n 2 ), O (n 3 ) . . . Example: Sum of n natural numbers Algorithm: Time taken 1) Start ------ 0 2) Input: Read ‘n’ ------ 1 3) initialize: sum=0, i=1 ------ 2 4) Process: sum = sum + i ------ n 5) i = i +1 ------ n 6) if ( i < = n ) go to step 4 ------ n +1 7) Output: 8) print ‘sum’ ------ 1 9) Stop ------ 0 Total time in function f(n) = 3n + 4 Big Oh: O() { Upper bound }:The function f(n) = O(g(n)), if and only if there exists constants “c and n 0 “ such that f(n) ≤ c . g(n) for all value of n, where as n>n 0 Analysis : Sum of n natural numbers f(n)= 3n + 4 ; f(n) ≤ C . g(n) 3n + 4 C . g(n) 3n + 4 3n + 4n (Upper bound)

Transcript of sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to...

Page 1: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

CO-2

Session 12- ANALYSIS OF ALGORITHMSTime Complexity: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)Asymptotic Notations: It is the meaningful way of representation of time complexity.They are

i) Big Oh [ O (g(n)) ] { Upper bound }ii) Big Omega [ Ω (g(n)) ] { Lower bound }iii) Big Theta [ Ɵ (g(n)) ] { Average }

The Order increases in this format: O (1), O (log n), O (n/2), O (n), O (n2), O (n3) . . . Example: Sum of n natural numbersAlgorithm: Time taken

1) Start ------ 02) Input: Read ‘n’ ------ 13) initialize: sum=0, i=1 ------ 24) Process: sum = sum + i ------ n5) i = i +1 ------ n6) if ( i < = n ) go to step 4 ------ n +17) Output: 8) print ‘sum’ ------ 19) Stop ------ 0

Total time in function f(n) = 3n + 4Big Oh: O() { Upper bound }:The function f(n) = O(g(n)), if and only if there exists constants “c and n0 “ such that f(n) ≤ c . g(n) for all value of n, where as n>n0

Analysis : Sum of n natural numbersf(n) = 3n + 4 ; f(n) ≤ C . g(n)3n + 4 ≤ C . g(n)3n + 4 ≤ 3n + 4n (Upper bound)If 3n + 4 ≤ 7n then C = 7 , g(n) = nIf n=1 then 7 ≤ 7If n=2 then 10 ≤ 14If n=3 then 13 ≤ 21

Therefore f(n) = O(g(n)) = O(n) thus f(n) = O(n) [Worst case ]

Big Omega [ Ω () ] { Lower bound } : The function f(n) = Ω(g(n)), if and only if there exists constants “c and n0 “ such that f(n) ≥ c . g(n) for all value of n, where as n>n0 .

Page 2: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Analysis : Sum of n natural numbers:

f(n) = 3n + 4 ; f(n) ≥ C . g(n)3n + 4 ≥ C . g(n)3n + 4 ≥ 3n (Lower bound)If 3n + 4 ≥ 3n then C = 3 , g(n) = nIf n=1 then 7 ≥ 3If n=2 then 10 ≥ 6If n=3 then 13 ≥ 9

Therefore f(n) = Ω(g(n)) = Ω(n) Thus f(n) = Ω(n) [Best Case]

Big Theta [ Ɵ () ] { Average } : The function f(n) = Ɵ(g(n)), if and only if there exists constants “c1, c2 and n0“ such that c1 . g(n) ≤ f(n) ≤ c . g(n) for all value of n, where as n>n0

.

Analysis : Sum of n natural numbers:

f(n)= 3n + 4 ; c1 . g(n) ≤ f(n) ≤ c . g(n) C1 . g(n) ≤ 3n + 4 ≤ C2 . g(n) 3n ≤ 3n + 4 ≤ 7n (Lower and Upper bound)If 3n ≤ 3n + 4 ≤ 7n then C1 = 3 , C2 = 7 and g(n) = nIf n=1 then 3 ≤ 7 ≤ 7If n=2 then 6 ≤ 10 ≤ 14If n=3 then 9 ≤ 13 ≤ 21

Therefore f(n) = Ɵ(g(n)) = Ɵ(n) Thus f(n) = Ɵ(n) [Average Case]

Other Examples:If f(n) = n (n+1) / 2 then

Page 3: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

f(n) = O (n2) f(n) = Ɵ (n2) f(n) = Ω (n2)Ex: Sum of all even numbers less than n natural numbers

O (n/2) [worst case]

Example: Sum of n natural numbers:

Algorithm: Time taken1) Start ------ 02) Input: Read n ------ 13) Process: sum = n * (n+1) / 2 ------ 14) print ‘sum’ ------ 15) Stop ------ 0

Total time in function f(n) = 3 f(n) = 3 . 1 = C . g(n) ; C = 3 , g(n) = 1 therefore f(n) = O(g(n) ) = O (1) = Ω(1) = Ɵ(1)

Session-13 Simple if and if else

Content:

CONDITIONAL STATEMENTSUsually a processor (machine) will execute its instructions in a sequential order

only. But in real time problems we need to execute a set of instructions whenever a particular condition satisfies otherwise we want to skip that set of instructions which indicates a small sub task. i.e. we want to change the execution flow of a processor from its sequential flow. To solve this type of problems C language provides few programming constructs which are called as conditional statements or decision making and branching statements. They are

1. Simple if statement2. If-else statement3. else-if ladder statement4. Nested if statements.5. Switch-case statement.

1.simple if statement:

It is used to execute a statement or a group of statements depending upon the condition.

It is simple if-statement. It is also called as one-way branching.

Syntax:- if(condition) { statement block or body of if; } next statement;

Condition -> it is logical expression that results in true or false. Statement block ->A set of statements or compound block.

Page 4: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Explanation:-

First the condition is checked. If the condition is true then the statement block is executed or the

statements that immediately follows ‘if’ is executed. If the condition is false, it skips the statement block and the control transfer

to the executable statement in the program. The condition may be an expression containing constants, variables or

logical expressions. The condition must be enclosed with in parenthesis.

The execution flow of the simple-if statement is shown in the following flow chart.

T F

If the expression is the true then if-body will execute and then goes to the next statement otherwise controller will skip the if-body and directly it goes to execute the next statement in the program.

void main(){Statement;Statement;…………..…………..if(expression){ T FIf-body}Statement;Statement;……………………

}

Program: Write program to check a given number is positive

#include<stdio.h>

Page 5: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Void main(){

Int i;Printf(“enter integer value:”);Scanf(“%d”, &i);If(i>0)

Printf(“given number is positive”);Printf(“end of program”);

}

Program: write a program to print whether a given number is even

#include<stdio.h>void main(){

Int i;Printf(“enter integer value:”);Scanf(“%d”, &i);

If( i%2==0)Printf(“Given number is even”);

Printf(“end of program”);}

If-else statement:- If there are two statements to be executed attentively then this conditional statement is

used. It is a two-way branching.

Syntax:- if( condition) { statement 1; //true block

} else {

statement 2; //false block } next statement;

}

Condition-> it is a logical expression that results in true or false .statement1 & statement 2 can be a single statement or compound statement.

Procedure:- First the condition is tested. If the condition is true then the statement1 or true block is executed. It skips the

Page 6: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

false block and control is transferred to next statement. If the condition is false it skips the true block and false block is executed. The

control is transferred to the executable statement in the program. The condition may be an expression containing constants, variables or logical

comparisons. The condition must be enclosed with in parenthesis.

In the above syntax if and else are keywords. The execution flow of the if-else statement is shown in the following flowchart.

T F

If the expression is a true expression then controller will execute the if-body ( true body) and then goes to execute next statement in the C program otherwise controller will execute the else-body and then goes, to execute the next statement in C program.

Program:-Program to find whether 2 given numbers are same or not.#include<stdio.h>#include<conio.h>main(){

int n1,n2;clrscr();printf(“enter numbers”);scanf(“%d%d”,&n1,&n2);if(n1==n2)printf(“the nos are equal:”);elseprintf(“the nos are not equal:”);getch();

}output:-

enter numbers 20 40the numbers are not equalProgram: Program to Print a given number is even or odd.Void main(){

Int I;Printf(“enter integer value”);Scanf(“%d”,&i);If(i%2==0)

Printf(“Given number is even”);

Page 7: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

ElsePrintf(“Given number is odd”);

}

Program: print whether the given year is leap year or not#include <stdio.h>void main(){ int year; printf("Enter a year to check if it is a leap year\n"); scanf("%d", &year); if ( year%400 == 0 && year%4 == 0) printf("%d is a leap year.\n", year); else printf("%d is not a leap year.\n", year);}Practice Program:

1. What would be the output of each of the following code segments? void main ( ) { int a,b=0; if(a=b=1) printf (“hello”); else printf (“world”);

}2. What would be the output of each of the following code segments?

void main() { int var1,var2=2,num=100,a; if(var1=var2%2) num=2; a=2;

printf(“%d %d”,num,var1); }3. What is the value of y in the following code?

x=7,y=0; if(x = 6) y=7; else

y=1;4. What is the value of z after the following statements executed?

int x=2,y=2,z=1; if(x=y%2) z+=10; else

z+=20;5. What is the output of the following code?

main()

Page 8: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

{ int a=300,b=100,c=50; if(a>400) b=300; c=200;

printf(“\n b=%d c=%d”,b,c); }

Session-14 NESTED IF-ELSE STATEMENTContent:Nested if-else:

Nested if else statment is same like if else statement, where new block of if else statement is defined in existing if or else block statment. Used when user want to check more than one conditions at a time.

Syntax:

If(Test_Expression1)

{

If(Test_Expression2)

{

Statements-1;

}

else

{

Statements-2;

}

}

else

{

If(Test_Expression3)

{

Statements-3;

}

Page 9: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

else

{

Statements-4;

}

}

Explanation:

If test expression1 is true then it enters into the outer if block and checks the test expression2.

If the test expression2 is also true then enter into inner if block and statement1 will be executed.If test expression2 is false then enters into else block and statement2 will be executed.

If test expression1 is false then it enters into the outer else block and checks the test expression3.

If the test expression3 is true then enters into if block and statement3 will be executed.If test expression3 is false then statement4 will be executed.

Page 10: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Program: Write a program to input three numbers and find the largest number among three.Solution:---------------------------------------------------------------------------#include<stdio.h>#include<conio.h>void main(){   int a,b,c;   clrscr();   printf("Enter three numbers");   scanf("%d%d%d", &a, &b, &c);   if(a>b) { If(a>c)       {         printf("a is largest");        }  else       {                  printf("C is largest");                   

Page 11: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

         }} else        {

              if( b>c)                 {                   printf("b is largest");                  }  else       {                  printf("C is largest");                            }

         }getch();}

Session-15 ELSE-IF LADDER STATEMENTContent:else-if ladder statement:-

If there are more than one statement and we have to select any one from the given alternatives then else-if is used.

Syntax:-

if(condition1)Statement1;

else if(condition2)Statement2;

else if(condition n)Statement n;

elseStatement x;

Explanation :-

The conditions are evaluated from top to bottom in the else-if statement. As seen as a true condition is found the statement associated with it is executed and

the control is transferred to the statement x.(i.e skipping the rest of statements). When all the n conditions are false, then the final else containing the default

statement will be executed.

Page 12: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

This construct is known as the else if ladder. The conditions are evaluated from the top of the ladder to downwards. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the Next-statement-x (skipping the rest of the ladder). When all the n conditions become false, then the final else containing the default statement will be executed.

This execution flow is shown graphically in the following flow chart.

T F

T F

T F

T F

Page 13: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

T

Program:-To print day of a week by giving code of the day as a number.#include<stdio.h>#include<conio.h>main(){

int code;printf(“enter code:”);scanf(“%d”,&code);if(code==1)printf(“Monday”);else if(code==2)printf(“Tuesday”);else if(code==3)printf(“Wednesday”);else if(code==4)printf(“Thursday”);else if(code==5)printf(“Friday”);else if(code==6)printf(“satarday”);else if(code==7)printf(“Sunday”);elseprintf(“not a valid number”);getch();

}

output:-enter code 4ThursdayEnter code 9Not a valid number

Program: program to display grades of student#include<stdio.h>void main(){         int m1,m2,m3,total;         float per;         clrscr();

Page 14: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

         printf("Enter 3 Nos.");         scanf("%d%d%d",&m1,&m2,&m3);         total=m1+m2+m3;         per=total*100/300;         if(per>=60&&per<=100)                 printf("You are 1st :");        else if(per>=50&&per<=60)                 printf("You are 2nd");        else if(per>=40&&per<=50)                 printf("You are 3rd");        else                 printf("You are Fail");        getch();}

Program: a program to enter the temperature and print the following message according to the given temperature by using if else ladder statement.1. T<=0                        "Its very very cold".2. 0 < T < 0                  "Its cold".3. 10 < T < =20            "Its cool out".4.  20 < T < =30           "Its warm".5.  T>30                        "Its hot"

#inlcude<stdio.h>#include<conio.h>void main(){  float temp;  clrscr();  printf("Enter The Temperature");  scanf("%f",&T);  if(T<=0)     {      printf("Its very very cold");      }  else if(T>0 && T<=10)           {            printf("Its cold");           }   else if(T>10 && T < =20)                  {                   printf("Its cool out");                  }   else if(T<=30 && T>20)              {                  printf("Its warm");              }   else             { 

Page 15: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

                  printf("Its hot");             }getch();

Switch statement:-

if we have number of statements and from that any one statement has to be selected then switch is used.

It is an alternative from else-if ladder statement.

Syntax:- switch ( expression){case value1: statement-1;break;case value2: statement -2;break;..case value n: statement -n;break;default : default statement;break;}Flowchart:

Page 16: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Explanation:- The expression value is compared with all case values or label values in the

switch case. If the match is found that block is executed and control comes out of switch

statement (because every case contains one break statement) and continues the execution with statement x.

Break is a keyword. It indicates the external exit from the switch statement. If break is not there in switch case all the cases will be executed including default. Where ever the break is found in switch statement till there the cases are executed

and control returns from the switch statement and continues with statement x. The expression is a keyword. Case should always follow by an integer constant,

character constant or constant expression. All the cases should be distinct. The block of statements under default is executed when none of the cases match the

value of expression.

Points to Remember:i) The case values should not be float values or Boolean expression.ii) The case values value1,value2,… value n should be distinct.iv) default case is optional.Program:-To perform arithmetic operations

#include<stdio.h>#include<conio.h>main(){int a,b,choice;clrscr();printf(“enter a ,b:”);scanf(“%d%d”,&a,&b);printf(“\n 1.addition”);printf(“\n 2.multipliction”);printf(“\n 3.division. ”);printf(“\n 4.subtraction”);printf(“enter choice”);scanf(“%d”,&choice);switch(choice){

case 1:printf(“the sum is%d”,a+b);break;case 2:printf(“the multiplication is%d”,a*b);break;case 3:printf(“thedivision is%d”,a/b);break;case 4: printf(“the subtraction is%d”,a-b);break;

Page 17: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

}getch();}

Output :- enter a,b 20 101.addition2.multipliction3.division4.subtractionenter choice:3the division is 10

Session-16&17 WHILE STATEMENT

Loops or iterative statements :Loops are used when a set of statements to be executed repeated for a fixed no of times or until some condition is true. Looping is also called repetitive or iterative. There are three types of loops present in ‘C’

1. for statement 2.while statement 3.do-while statement

while statementIt is simplest of all the looping structuresIt is also known as entry contolled loopIt is used when the no of statements is to be executed repeatedly until the specified conditions is trueSyntax:- while(expression) { body of while }Expression:- It can be any condition which results in true or false .condition must be specified within left and right parenthesis.While :- It is a keywordBody of while:- It can be single statement or compound statementsOperation:-

The body of while loop is executed whenever the condition is true. If the condition is false the body of while loop is skipped and control comes out of the loop and continuous with the next executable statement.In this minimum number of times the execution takes place is zero.

Flow Diagram:

Page 18: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Program: write a program to print numbers from 1 to 10.#include <stdio.h> int main (){ /* local variable definition */ int a = 1;

/* while loop execution */ while( a < =10 ) { printf("value of a: %d\n", a); a++; } return 0;}Program: Print first 10 even numbers

int main (){ /* local variable definition */ int a = 1, i=1;

/* while loop execution */ while( a < =10 ) {

If(i%2==0)

Page 19: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

{Printf(“%d\n”,i);a++;

} i++; } return 0;}Ex:- /* To find sum of even numbers */

#include<stdio.h>void main(){ int i,sum; clrscr(); i=1; sum=0; while(i<=10) { if(i%2==0) { printf("%d",i); sum=sum+i; } i++; } printf("=%d",sum); getch(); }

Nested while

The syntax for a nested while loop statement in C programming language is as follows:while(condition){ while(condition) { statement(s); } statement(s);}

Program: A program to print ************ ****** ****** Void main(){

Page 20: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Int i=0, j;While(I<4){

J=0;While(j<6){

Printf(“*”);J++;

}Printf(“\n”);I++;}

}

Program:To print****** ***** **** *** ** *

Void main(){ Int i=0, j;

While(I<4){J=i;

While(j<6){

Printf(“*”);J++;

}Printf(“\n”);I++;

}}Programs:

1. What is the output when the following program is executed? #include<stdio.h> main() { int i=1; while(i<=10) { printf(“%d”,--i); ++i; } }

Page 21: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

2. What is the output when the following program is executed? #include<stdio.h> main() { int x=1; while() { printf(“%d”,x); ++x; } }

3. What is the output when the following program is executed? #include<stdio.h> main() { int a=5; while(a-- >0) printf(“%d\t”,a); }

4. What is the output when the following program is executed? #include<stdio.h> main() { int p=5; while(p<8) { printf(“%d”,p); p=9; } }

5. What will be the output when the following code is executed? #include<stdio.h> main() { int a = 6; while (a) { printf(“%d\t”, a); a -= 2; }

Session-18&19 FOR LOOP STATEMENT

For statement:- This is used when the user knows how many times a set of statements are executed It is an efficient looping technique among all Syntax:-

Page 22: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

for(initial value; condition; increment or decrement) { Body of for loop; }

Here for is a keyword

Body of for Loop : it can be single or compound statements Initial value,condition and increment or decrementmust be separated by semicolon.

Operation:- First initial value is assigned for a variable. next condition is checked. If the condition is true body of the for loop is executed first and value is

incremented or decremented If the condition is false the control comes out of for loop and continues with next

statement in the program In this minimum no of times the execution takes place is zero It can include multiple expressions in any of the fields of the for loop It can omit one or more fields from the for statements by making its place with

semicolonFlow Chart:

Program: to print values from 1 to 10Void main(){ Int I;

Page 23: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

for(i=0;i<n;i++){

Printf(“%d “,i);}

}

Program: to print first 10 odd numbers Void main(){ Int I, count;

for(i=1,count=0; count< 10; i++){

If(i%2!=0){

Printf(“%d\n”,i);Count++;

}}

}Example program1 : Find the factorial of the given number ?-

#include<stdio.h>void main(){ int n,fact=1,i; printf(“enter number “); scanf(“%d”,&n); for(i=1;i<=n;i++) { fact =fact*i; } printf(“the factorial of the given number is %d”,fact);}

Example program2: program to find whether a number is prime number or not.#include<stdio.h>

int main(){

    int num,i,count=0;    printf("Enter a number: ");    scanf("%d",&num);    for(i=2;i<=num/2;i++){        if(num%i==0){         count++;            break;        }    }   if(count==0 && num!= 1)        printf("%d is a prime number",num);   else

Page 24: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

      printf("%d is not a prime number",num);   return 0;}

Sample output:Enter a number: 55 is a prime number

Nested-for:

C programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.Syntax:The syntax for a nested for loop statement in C is as follows:for ( init; condition; increment ){ for ( init; condition; increment ) { statement(s); } statement(s);} program to print 1 11 21 32 1 2 22 33 13 23 3

Void main(){ Int I,j;

For(i=1;i<=3;i++){

For(j=1;j<=3;j++)Printf(“%d %d”,I,j);

}}

Program: Task to learners to print **********

Page 25: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

*****

Void main(){ Int I,j;

For(i=1;i<=5;i++){

For(j=1;j<=I;j++)Printf(“*”);

Printf(“\n”);}

}

Multiplication table:

void main(){int n ;printf(“enter any number\n”);scanf(“%d”,&n);for(i=1;i≤10;i++)printf(“%d * %d = %d\n”,i,n,i*n);}

Strong No:

Void main(){

Int n,temp,fact=1,rem,sum=0;Printf(“enter number\n”);Scanf(“%d”,&n);Temp=n;While(temp!=0){Rem=temp%10;Fact=1;For(i=1;i≤rem;i++)Fact=fact*i;Sum=sum+fact;Temp=temp/10;}If(sum==n)Printf(“%d is a strong number\n”);}How to find prime numbers from 1 to 100 in c#include<stdio.h>

int main(){    int num,i,count;

Page 26: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

      for(num = 1;num<=100;num++){         count = 0;

         for(i=2;i<=num/2;i++){             if(num%i==0){                 count++;                 break;             }        }                 if(count==0 && num!= 1)             printf("%d ",num);    }     return 0;}

Output:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Session-20 do-while Content:

do-while LoopThe while and for loops test the termination condition at the top. By contrast, the third loop in C, thedo-while, tests at the end after making each pass through the loop body. Hence the body is alwaysexecuted at least once.Thus while & for loops are entry-controlled loops, whereas do-while is exit controlled loop.

It is exit control loop. it is also used to execute the statements until the specified condition is true.

Syntax:-

do

{

body of do-while loop

Page 27: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

} while (expression);

Expression -> it can be any condition which results in true or false.

Do-while -> both are key words

Body of do-while-> it can be single statement or compound statement.

Condition must be specified within left & right parenthesis.

Do-while loop should end with semicolon.

Explanation:-

In this the condition is checked at the end i.e. The control first enter into the loop and executes the body of the loop

After execution it checks for the condition If the condition is true then control again enters into the body of the loop and execution

repeats until the condition is true If the condition is false the control comes out of the loop and continues with the next

statement The min no of times the body of the loop executes is one.

Page 28: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Example:

int main(){ int j=0 do { printf("Value of variable j is: %d", j); j++; }while (j<=8); return 0;}Program to add numbers until user enters zero

#include <stdio.h>

int main()

{

Int number, sum = 0;

// loop body is executed at least once

do

{

printf("Enter a number: ");

scanf("%d", &number);

sum += number;

}

while(number != 0);

printf("Sum = %d",sum);

return 0;

}

Output

Page 29: sampathgarimella.files.wordpress.com€¦  · Web view: It is the time taken by a computer to completely execute a program. Program may be in any language (C, C++, Java etc…)

Enter a number: 1

Enter a number: 2

Enter a number: -3

Enter a number: 4

Enter a number: 0

Sum = 4