## Search Your Topic ## Programming (216)

Friday, 07 December 2018 07:50

Written by

## Spy Number

A number is a Spy number, if sum and product of all digits are equal.

Example:

Number 123 is a Spy number, sum of its digits is 6 (1+2+3 =6) and product of its digits is 6 (1x2x3 = 6), sum and product are same, thus, 123 is a spy number.

```import java.util.Scanner;

public class SpyNumber
{
public static void main(String[] args)
{
int n,product=1,sum=0;
int ld;

// create object of scanner.
Scanner sc = new Scanner(System.in);

// you have to enter number here.
System.out.print("Enter the number :" );

// read entered number and store it in "n".
n=sc.nextInt();

// calculate sum and product of the number here.
while(n>0)
{
ld=n%10;
sum=sum+ld;
product=product*ld;
n=n/10;
}

// compare the sum and product.
if(sum==product)
System.out.println("Given number is spy number");
else
System.out.println("Given number is not spy number");
}
}Output```
```First run:
Enter the number :1124
Given number is spy number

Second run:
Enter the number :1123
Given number is not spy number```

### Program to check Spy number in java

Friday, 07 December 2018 07:44

### Program to check if number is Neon Number in java

Written by

This program checks if a number entered is a Neon Number or not, in JAVA.

A number is said to be a Neon Number if the sum of digits of the square of the number is equal to the number itself.

Example- 9 is a Neon Number. 9*9=81 and 8+1=9.Hence it is a Neon Number.

Program -

import java.util.*;
public class NeonNumber
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the number to be checked.");
int num=ob.nextInt();
int square=num*num;
int sum=0;
while(square!=0)//Loop to find the sum of digits.
{
int a=square%10;
sum=sum+a;
square=square/10;
}
if(sum==num)
{
System.out.println(num+" is a Neon Number.");
}
else
{
System.out.println(num+" is not a Neon Number.");
}
}
}

Description -

The number is entered through Scanner class and then its square is taken by multiplying the number 2 times.After that we find the sum of digits of the square and check if the number is equal to the sum of digits of square of the number.

Friday, 07 December 2018 07:30

### Classify Abundant, deficient and perfect number (integers) between 1 to 10,000

Written by

Write a Java program to classify Abundant, deficient and perfect number (integers) between 1 to 10,000.

In number theory, an abundant number is a number for which the sum of its proper divisors is greater than the number itself.
Example :
The first few abundant numbers are:
12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102,…
The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6 for a total of 16.
Deficient number: In number theory, a deficient number is a number n for which the sum of divisors σ(n)<2n, or, equivalently, the sum of proper divisors (or aliquot sum) s(n)<n. The value 2n − σ(n) (or n − s(n)) is called the number's deficiency.
As an example, divisors of 21 are 1, 3 and 7, and their sum is 11. Because 11 is less than 21, the number 21 is deficient. Its deficiency is 2 × 21 − 32 = 10.
The first few deficient numbers are:
1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 29, 31, 32, 33, …….
Perfect number: In number system, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself) i.e. σ1(n) = 2n.
The first perfect number is 6. Its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128.

Sample Solution:

Java Code:

import static java.util.stream.LongStream.rangeClosed;
public class Exercise2 {

public static void main(String[] args) {
int count_Deficient_no = 0;
int count_Perfect_no = 0;
int count_Abundant_no = 0;

for (long i = 1; i <= 10_000L; i++) {
long sum = proper_Divs_Sum(i);
if (sum < i)
count_Deficient_no++;
else if (sum == i)
count_Perfect_no++;
else
count_Abundant_no++;
}
System.out.println("Number Counting [(integers) between 1 to 10,000]: ");
System.out.println("Deficient number: " + count_Deficient_no);
System.out.println("Perfect number: " + count_Perfect_no);
System.out.println("Abundant number: " + count_Abundant_no);
}
public static Long proper_Divs_Sum(long num) {
return rangeClosed(1, (num + 1) / 2).filter(i -> num % i == 0 && num != i).sum();
}
}

Sample Output:
`Number Counting [(integers) between 1 to 10,000]:`
`Deficient number: 7508 `
`Perfect number: 4   `
`Abundant number: 2488 `

Thursday, 21 September 2017 05:56

### C -for-Program

Written by

Print the first five numbers starting from I together with their squares.

`#include<stdio.h>#include<conio.h>Void main ( ){       Int  I;       Clrscr ( );       For (i=l; <=5; i++)       Printf (“\n  Number:  %5d  it’ s  Square:  %5d”, I, i*i);      getch();}`

OUTPUT:

Number:  1  it’ s  Square:  1

Number:  2  it’ s  Square:  4

Number:  3  it’ s  Square:  9

Number:  4  it’ s  Square:  16

Number:  5  it’ s  Square:  25

`#include<stdio.h>#include<conio.h>Valid main ( ){        Int  I;        Clrscr ( ) ;        For ( i=l; i<=15; i= i+l)        Printf (“%5d”,i);        getche ( );}`

OUTPUT:

1 2 3 4 5 6 7 8 9 10 11 12 13 14  15

Write a program to display even numbers from 0 to 14.  Declare the initial counter value before the for loop statement.

`#include<stdio.h>#include<conio.h>Void main ( ){        Int  i=o ;        Clrscr ( );        For (i<=15;)        {                  Printf (“% 5 d “ , i) ;                  i+=2;        }}`

OUTPUT:

O 2 4 6 7 8 10 12 14

Write a program to count numbers between 1 to 100 not divisible by 2, 3, and 5.

`#include<stdio.h>#include<conio.h>Void main ( ){     Int  x, c=0;     Clrscr ( );     Printf (“\n  Numbers  from  1  to  100  not  divisible  by  2, 3  &  5 \n \n”);     For (x=0  ;x<=100 ;x ++)     {            If (x%2 !=0   &&  x%3!=0  && x%5!=0)            {                Printf (%”%d\t” ,x),                C++;            }     }     Printf (“\nTotal numbers  :  %d”, c );}`

OUTPUT:

Numbers  from  1  to  100  not  divisible  by 2, 3  &  5

1    7    11    13    17    19    23    29    31    37     37

41   43    47    49    53     59    61    67    71     73

77    79    83    89   91    97

Total  Numbers  :  26

Write a program to display the numbers in increasing and decreasing order using the infinite for loop.

`#include<stdio.h>#include<conio.h>           Void main ( ){       Int  n, a, b;       Clrscr ( );       Printf (“Enter  a  number  :”);       Scanf (“, &);       A=b=n;       Printf (“ (++)  ( - - ) \n”);       Printf (“============”);       For (;  ;  (a++, b - - ) )       {             Printf (“\n%d\t%d”, a, b);             If (b==0)             Break;       }}`

OUTPUT:

Enter  a  number  :  5

(++)         ( - - )

===========

5    5

6    4

7    3

8    2

9    1

10  0

create an infinite for loop.  Check each value of the for loop.  If the value is even display it otherwise continue with iterations.  Print Even numbers from 1 to 21.  Use break statement to terminate the program.

`#include<stdio.h>#include<conio.h>Void main ( ){       Int  i=l;       Clrscr ( );       Printf (“\n \t \t  Table  of  Even  numbers  from  1  to  20”);       Printf (“\n \t \t  ===== == ==== ======= ==== = == == \n”);       For (;  ;)       {           If (i==21)           Break;           else  if (i%2==0)           {                 Printf (“%d\t”, i);                 I++;                 Continue;           }           else           {                I++;                Continue;           }    }    getche ( );}`

OUTPUT:

Table  of  Even  numbers  from  1  to  20

====  ==  ====  =======  ===  =  ==  ==

2  4  6  8  10  12  14  16  18  20

Calculate the sum of the first five numbers and their squares. Display their results.

`Void main ( ){        Int  I,  sum=0, sqsum=0;        Clrscr ( );        For (i=l; i<-5; ++)        {               Sum+-I;               Sqsum+=i*I;               Printf (“\n  number:  %5d  it’ s  Square  :  %8d”, I, i*i);       }       Printf (“\n===================================”);       Printf (“\n  The  sum  of  the  five  numbers (1  t0  5)  : -%6d”, sum);       Printf (“\n  The  sum  of  their  Squares: -%9d”,  sqsum);       getche ( );}`

OUTPUT:

Number:             1                 it’ s  Square:             1

Number:             2                 it’ s  Square:             4

Number:             3                 it’ s  Square:             9

Number:             4                 it’ s  Square:            16

Number:             5                 it’ s  Square:            25

=========================================

The  sum  of  the  five  numbers (1  t0  5) : -  15

The  sum  of  their  Squares: -  55

Write a program to find the number in between 7 and 100 which is exactly divisible by 4 and

If divided by 5 and 6 remainders obtained should be 4.

`#include<stdio.h>#include<conio.h>#  include  <process . h>Void main ( ){       Int  x;       Clrscr ( );       For (x=7;x<100;x++)       {              If (x%4==0  &&  x%5==4  &&  x%6==4)              {                     Printf (“\n  Number  :  %d”, x);              }      }     getche ( );}`

OUTPUT:

Number  :  64

Write a program to find the sum of the following series.

`/*  1.  1+2+3+ ..n  *//*  2.  12+22+32+..n2  */#include<stdio.h>#include<conio.h>Void main ( ){       Int  sum=0, ssum-0, I, j;       Clrscr ( );       Printf (“Enter  Number  :”);       Scanf (“%d”,  &j);       Clrscr ( );       Printf (“  Numbers:”);       For (i=1; i<=j; i++)       Printf (“%5d”, i);       Printf (“\n \nSquares:”);       For (i=1; i<=j; i++)       {            Printf (“%5d”, i*i);            Sum=sum+I;            Sum=ssum+i*i;       }       Printf (“\n \nSum  of  Numbers  from  1  to  %d  :%d”, j, sum);       Printf (“\nSum  of  Squares  of  1  to  %d  Numbers  :%d”, j, ssum);}`

OUTPUT:

Enter  Number:  5

Numbers:  1  2  3  4  5

Squares:  1  4  9  16  25

Sum  of  Numbers  from  1  to  5:  15

Sum  of  Squares  of  1  to  5  Numbers:  55

Write a program to find the perfect squares from 1 to 500.

`#include<stdio.h>#include<conio.h># include  <math . h>Void maid ( ){      Int  I, count, x;      Float  c;      Clrscr ( );      Printf (“\n \n”);      Printf (“  Perfect  squares  from  1  to  500 \n”);      For (i=1; <=500; i++)  {      C=sqrt (i);       X=floor (c);  /*  For  rounding  up  floor ( )  is  used.  */       {              Printf (“\t%5d”, i);              Coun ++;      }  }  Printf (“\n \n  Total  Perfect  Squares  =5d\n”, conut);  getch ( );}`

OUTPUT:

1       4      9      16      25   36   49   64   81   121   144

169   196   225    256    289   324    361    400   441

484

Total  Perfect  Squares  =  22

Write a program to detect the largest number out of five numbers and display it.

`#include<stdio.h>#include<conio.h># include<process . h>Void main ( )Exit (0);{       Int  a, b, c, e, sum=0, I;       Clrscr ( );       Printf (“\nEnter  Five  numbers  :”);       Scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e);       For (i=sum;  i<=sum; I - -)       {             If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)             {                   Printf (“The  Largest  Number  :  %d”, i);                   Exit (0);             }      }}`

OUTPUT:

Enter  Five  number  :  5  2  3  7  3

The  Largest  Number  :  7

Write a program to detect the smallest number our of five numbers and display it.

`#include<stdio.h>#include<conio.h>Void main ( ){      Int  a, b, c, d, e, sum=0, I;      Clrscr ( );      Printf (“\nEnter  Five  numbers  :”);      Scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e);      Sum=a+b+c+d+e;      For (i=l;  i<=sum; i++)      {            If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)            {            Printf (“The  Smallest  Number  :  %d”, i)            Exit (0);            }      }}`

OUTPUT:

Enter  Five  numbers  :  5  2  3  7  3

The Smallest  Number:  2

Write a program to print the five entered numbers in the ascending order.

`#include<stdio.h>#include<conio.h>Void main ( ){      Int  a, b, c, d, e, sum-0, I;      Clrscr ( );      Printf (“\nEnter  Five  numbers  :”);      Scanf (“%d  %d  %d  %d  %d” &a, &b, &c, &d, &e);      Printf (“\n  Numbers  in  ascending  order  :”);      Sum=a+b+c+d+e;      For (i=l;  i<=sum; i++)  {     If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)     {             Printf (“%3d”, i);     }  }}`

OUTPUT:

Enter  Five  numbers  :  5  8  7  4  1

Numbers  in  ascending  order  :  1  4  5  7  8

Perform multiplication of two integers by using the negative sign.

`#include<stdio.h>#include<conio.h>Void main ( ){      Int  a, b, c, d=0;      Clrscr ( );      Printf (“\n  Enter  two  numbers  :”);      Scanf (“%d  %d”,  &a, &b);      For (c=l; c<-b; c++)      D= (d) – ( - a);      Printf (“Multiplication  of  %d  *  %d  :%d”, a, b, d);      getche ( );}`

OUTPUT:

Enter  two  numbers  :  5  5

Multiplication  of  5  *  5  :  25

Calculate the sum and average of five subjects.

`#include<stdio.h>#include<conio.h>Void main ( ){      Int  a, b, c, d, e, sum=0, I;      Float  avg;      Clrscr ( );      Printf (“\nEnter  The  Marks  of  Five  Subjects”);      For (i=l; i<=5; i++)      {           Printf (“\n [%d]  Student:”, i);           If (scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e) ==5)           {                 Sum=a+b+c+d+e;                 Avg=sum/5;                 Printf (“\n  Total  Marks  of  Student [%d]  %d”, I, sum);                 Printf (“\n  Average  Marks  of  Student [%d]  %f\n”, I, avg);           }           else           {                Clrscr ( );                Printf (“\n  Type  Mismatcch”);           }     }}`

OUTPUT:

Enter  The  Marks  of  Five  Subjects

 Student:  58  52  52  56  78

Total  Marks  of  Student   296

Average  Marks  of  Student   59.000000

 Student:

Write a program to find perfect cubes up to a given number.

`/* 1, 8, 27, 64 are perfect cubes of 1, 2, 3 and 4 */.#include<stdio.h>#include<conio.h>#  include<math . h>Void main ( ){      Int  I,  j,  k;      Clrscr ( );      Printf (“Enter  a  Number  :”);      Scanf (“%d”, &k);      For (i=l; i<k; i++)      {            For (j=l; j<=I; j++)            {                 If (i==pow (j,3) )                 Printf (“\nNumber  :  %d  &   it’ s  Cube  :%d:, j, i);            }     }}`

OUTPUT:

Enter  a  Number  :  100

Number  :  1  &  it’ s  Cube  :  1

Number  :  2  &  it’ s  Cube  :  8

Number  :  3  &  it’ s  Cube  :  27

Number  :  4  &  it’ s  Cube  :  64

Write a program to display the stars as shown below.

*

*  *

*  *  *

*  *  *  *

*  *  *  *  *

`#include<stdio.h>#include<conio.h>Void main ( ){       Int  x, I, j;       Clrscr ( );       Printf (“How  many  lines  stars  (*)  should  be  printed  ?  :”);       Scanf (“%d”, &x);       For (i=l; i<=x; i++)       {              For (j=l; j<=I; j++)              {                     Printf (“*”);              }              Printf (“\n”);       }}`

OUTPUT:

How  many  lines  stars  (*)  should  be  printed  ?  :  5

*

*  *

*  *  *

*  *  *  *

*  *  *  *  *

Write a program to generate the pattern of numbers as given under.

6  5  4  3  2  1  0

5  4  3  2  1  0

4  3  2  1  0

3  2  1  0

2  1  0

1  0

0

`#include<stdio.h>#include<conio.h>Void main ( ){      Int  I, c=0;      Clrscr ( );      Printf (“Enter  a  Number  :”);      Scanf (“%d, &i);      For (;i>=0i - -)      {            C=I;            Printf (“\n”);            For (;  ;)            {                   Printf (“%3d”, c);                   If (c==0)                   Break;                   C- -;            }      }}`

OUTPUT:

Enter  a  number:  6

6  5  4  3  2  1  0

5  4  3  2  1  0

4  3  2  1  0

3  2  1  0

2  1  0

1  0

Write a program to display the series of numbers as given below.

1

1  2

1  2  3

1  2  3  4

4  3  2  1

3  2  1

2  1

1

`#include<stdio.h>#include<conio.h>Void main ( ){      Int  I, j, x;       Printf (“\nEnter  Value  of  x  :”);      Scanf (“%d”,&x);      Clrscr ( );      For (j=l; j<=x; j++)      {             For (i=l; i<=j; i++)             Printf (“%3d”, i);             Printf (“\n”);     }     Printf (“\n”);     For (j=x; j>=l; I - -)     {            For (i=j; i>=l; I- -)            Printf (“%3d”, i);            Printf (“\n”);     }} `

OUTPUT:

1

1  2

1  2  3

1  2  3  4

4  3  2  1

3  2  1

2  1

1

Write a program to display the series of numbers as given below.

1

1  2

1  2  3

1  2  3  4

4  3  2  1

3  2  1

2  1

1

`#include<stdio.h>#include<conio.h>Void main ( ){     Int  I, j, x;     Printf (“\nEnter  Value  of  x  :”);     Scanf (“%d”, &x);     Clrscr ( );     For (j-l; j<=x; j++)     {           For (i=j; i>=l; I - -)           {                 Printf (“%3d”, i);           }           Printf (“\n”);     }}`

OUTPUT:

Enter  Value  of  x  :  4

1

1  2

1  2  3

1  2  3  4

4  3  2  1

3  2  1

2  1

1

Write a program to generate the pyramid structure using numerical.

`#include<stdio.h>#include<conio.h>Void main ( ){      Int  k, I, j, x, p=34;      Printf (“\n  Enter  A  number  :”);      Scanf (“%d”, &x);      Clrscr ( );      For (j=0; j <=x; j++)      {             Gotoxy(p, j+l);             /*  position  cursor  on  screen  (x  cordinate, y  cordinate)  */            For (i=0 – j; I <=j; i++)            Printf (“%3d”, abs ( I ) );            P=p-3;     }}`

OUTPUT:

Enter  a  number  :  3

0

1  0  1

2  1  0  1  2

3  2  1  0  1  2  3

Tuesday, 19 September 2017 05:11

### nested loops in C

Written by

C programming allows to use one loop inside another loop. The following section shows a 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);
}
```

The syntax for a nested while loop statement in C programming language is as follows −

```while(condition) {

while(condition) {
statement(s);
}

statement(s);
}
```

The syntax for a nested do...while loop statement in C programming language is as follows −

```do {

statement(s);

do {
statement(s);
}while( condition );

}while( condition );
```

A final note on loop nesting is that you can put any type of loop inside any other type of loop. For example, a 'for' loop can be inside a 'while' loop or vice versa.

## Example

The following program uses a nested for loop to find the prime numbers from 2 to 100 −

```#include <stdio.h>

int main () {

/* local variable definition */
int i, j;

for(i = 2; i<100; i++) {

for(j = 2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) printf("%d is prime", i);
}

return 0;
}```

When the above code is compiled and executed, it produces the following result −

```2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime```
Tuesday, 19 September 2017 05:09

### do...while loop in C

Written by

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.

do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

## Syntax

The syntax of a do...while loop in C programming language is −

```do {
statement(s);
} while( condition );
```

Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.

## Flow Diagram ## Example

```#include <stdio.h>

int main () {

/* local variable definition */
int a = 10;

/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );

return 0;
}```

When the above code is compiled and executed, it produces the following result −

```value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19```
Tuesday, 19 September 2017 05:07

### for loop in C

Written by

for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

## Syntax

The syntax of a for loop in C programming language is −

```for ( init; condition; increment ) {
statement(s);
}
```

Here is the flow of control in a 'for' loop −

• The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

• Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.

• After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

• The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.

## Flow Diagram ## Example

```#include <stdio.h>

int main () {

int a;

/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}

return 0;
}```

When the above code is compiled and executed, it produces the following result −

```value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19```
Tuesday, 19 September 2017 04:56

### while loop in C

Written by

while loop in C programming repeatedly executes a target statement as long as a given condition is true.

## Syntax

The syntax of a while loop in C programming language is −

```while(condition) {
statement(s);
}
```

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.

When the condition becomes false, the program control passes to the line immediately following the loop.

## Flow Diagram Here, the key point to note is that a while loop might not execute at all. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

## Example

```#include <stdio.h>

int main () {

/* local variable definition */
int a = 10;

/* while loop execution */
while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}

return 0;
}```

When the above code is compiled and executed, it produces the following result −

```value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19```
Tuesday, 19 September 2017 04:11

Written by

### What is a Loop?

A loop is defined as a block of statements, which are repeatedly executed for a certain number of times.

The loops are two types.

• Counter-controlled repetition: This is also called the definite repetition action, because the number of iterations to be performed is defined in advance in the program itself. The steps for performing counter-controlled repetitions are as follows.

Steps in Loop

Loop variable: IT is a variable used in the loop

Initialization: It is the first step in which starting and final values are assigned to the loop variable. Each time the upload value is checked by the loop itself.

Incrimination/discrimination: It is the numerical value added or subtracted to the variable in each round of the loop. The upload value is compared with the final value and if it is found less than the final value the steps in the loop are executed.

The above steps are implemented in numerous programs in this chapter.

• Sentinel- controlled repetition: This is also called the indefinite repetition. One cannot estimate how many    iterations are to be performed.  In this type, loop termination happens on the basis of certain conditions using the decision-making statement.

In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number. If it hasn't, the next instruction in the sequence is an instruction to return to the first instruction in the sequence and repeat the sequence. If the condition has been reached, the next instruction "falls through" to the next sequential instruction or branches outside the loop. A loop is a fundamental programming idea that is commonly used in writing programs.

# infinite loop (endless loop)

An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats indefinitely. In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked, such as whether a counter has reached a prescribed number. If the presence of the specified condition cannot be ascertained, the next instruction in the sequence tells the program to return to the first instruction and repeat the sequence, which typically goes on until the program terminates automatically after a certain duration of time, or the operating system terminates the program with an error.

Usually, an infinite loop results from a programming error - for example, where the conditions for exit are incorrectly written.

Loop Type & Description

while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

for loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

do...while loop

It is more like a while statement, except that it tests the condition at the end of the loop body.

nested loops

You can use one or more loops inside any other while, for, or do..while loop.

Monday, 18 September 2017 06:47

### C-if else Program

Written by

Write a program to check equivalence of two numbers. Use the if statement.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

Int  m, n;

Clrscr ( );

Printf (“\n Enter Two Numbers  :”);

Scanf (“%d  %d”, &m, &n);

If (m-n==0)

Printf (“\n Two numbers are equal. “);

getch( );

}

OUTPUT:

Enter Two Numbers  :  5  5

Two numbers are equal.

Write a program to check whether the candidate’s age is greater than 17 or not. If yes, display

Message ‘Eligible for Voting’.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

Int  age;

Clrscr ( );

Printf (“\n Enter age  :”);

Scanf (“%d, &age);

If (age>17)

Printf (“\n Eligible for Voting.”);

getch ( );

}

Write a program using curly braces in the if block. Enter only the three numbers and calculate their sum and multiplication.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

Int a, b, c, x;

Clrscr ( );

Printf (“\nEnter Three Numbers:”);

X=scanf (“%d %d %d”, &a, &b, &c);

If (x==3)

{

Printf (“\n Addition  :  %d”, a,+b+c);

Printf (“\n Multiplication  :  %d”, a*b*c);

}

}

OUTPUT:

Enter Three Numbers:  1  2  4

Multiplication  :  8

After second time execution

Enter Three Numbers:  5  v  8

Read the values of a, b and c through the keyboard. Add them and after addition check if it is In the range of 100 and 200 or not. Print the separate message for each.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

Int  a, b, c, d;

Clrscr (“Enter Three Numbers a b c  :”);

Printf (“%d %d %d”, &a, &b, &c);

Printf (“a=%d b=%d c=%d”, a, b, c);

D=a+b+c;

If (d<=200 && d>=100)

Printf (“\nSum is %d which is in between 100 & 200”, d);

else

Printf (“\nSum is %d which is out of range”, d);

getch ( );

}

OUTPUT:

Enter Three Numbers a b c  :  50  52  54

A=50  b=52  c=54

Sum is 156 which is in between 100 & 200.

Write a program to calculate the square of those numbers only whose least significant Digit is 5.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

Int  s, d;

Clrscr ( )

Printf (“\n Enter  a  Number  :”);

Scanf (“%d, &s);

D=s %10;

If (d==5)

{

S=s/10;

Printf (“\n Square  =  % d%d”, s*s++,d*d);

}

Else

Printf (“\n Invalid Number”);

}

OUTPUT:

Enter  a  Number  :  25

Square  =  625

Write a program to calculate the salary of medical representative based on the sales. Sales.

Bonus And incentive to be offered to him will be based on total sales.

If the sale exceeds Rs.1, 00,000,Follow the particulars of Table 1 otherwise follow Table 2.

 TABLE Basic=Rs.  3000. Hra=20% of basic. Da=110% of basic. Conveyance=Rs. 500. Incentive=10% of sales. Bonus=Rs.  500. TABLE Basic=Rs.  3000. Hra=20% of basic. Da=110% of basic. Conveyance=Rs. 500. Incentive=5% of sales. Bonus=Rs.  200.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

Float bs, hra, da, cv, incentive, bonus, sale, ts;

Clrscr ( );

Printf (“\n Enter Total Sales in Rs.:”);

Scanf (“%f”, &sale);

If (sale>100000)

{

Bs=3000;

Hra=20  *  bs/100;

Da110  *  bs/100;

Cv=500;

Incentive=sale*10/100;

Bonus=500;

}

Else

{

Bs=3000;

Hra=20  *  bs/100;

Da=110  *  bs/100;

Cv=500;

Incentive=sale*5/100;

Bonus=200;

}

Ts=bs+hra+da+cv+incentive+bonus;

Printf (“\nTotal Sales  :  % f”, sale);

Printf (“\nBasic salary :  % f”’, bs)

Printf (“\nHra               :  % f”, hra);

Printf (“\nDa                 :  % f, da);

Printf (“\nConveyance : % f”, cv);

Printf (“\nIncentive      : % f”, incentive)

Printf (“\nBonus            : % f”, bonus);

Printf (“\nGross Salary : % f”, ts);

getch ( );

}

OUTPUT:

Enter Total Sales in Rs.  100000

Total Sales:  100000.00

Basic Salary:  3000.00

Hra:  600.00

Da:  3300.00

Conveyance:  500.00

Incentive:  10000.00

Bonus:  500.00

Gross Salary:  17900.00

Write a program to calculate energy bill. Read the starting and ending meter readings.Charges are as follows.

No. of Units    Consumed Rates in  (Rs.)

200  -  500                           3.50

100  -  200                           2.50

Less  than  100                   1.50

#include<stdio.h>

#include<conio.h>

Void main ( )

{

Int initial, final, consumed;

Float total;

Clrscr ( );

Printf (“\n Initial & Final Readings  :”);

Scanf (“%d  %d”,  &initial,  &final);

Consumed  =  final – initial;

If (consumed>=200  &&  consumed<=500)

Total=consumed  *  3.500;

Else if (consumed>=100  &&  consumed<=199)

Total= consumed  *  2.500;

Else if (consumed*1.50;

Printf (“Total bill for %d unit is %f”, consumed, total);

getche ( );

}

OUTPUT:

Initial  &  Final Readings  :  800  850

Page 1 of 16