Write C program to print all Armstrong numbers from 1 to N using while and do while loop.

11. Write C program to print all Armstrong numbers from 1 to N using while and do while loop..

Armstrong number is a number that is equal to the sum of cubes of its individual digits. For example 1, 153, 370, 371 and 407 are the Armstrong numbers.
e.g 153 is Armstrong Number the sum of cube of its individual digits is 153.
cube of 1 is =1
cube of 5 is =125
cube of 3 is=27
sum of cube of individual digits =1+125+27 =153

#include <stdio.h>
/* Write C Program to find all Armstrong numbers from 1 to N using while loop provided by the user.*/

#include <stdio.h>
int main()
{
int r=0, limit, sum=0 ,n=1,n1;
/* Input upper limit from user */
printf("Enter upper limit to find Armstrong number: ");
scanf("%d", &limit);
printf("Armstrong number between 1 to %d are: \n", limit);
while(n<=limit)
  {
   sum = 0;
   /* Calculate sum of power of digits */
   n1=n;
   while(n1!=0)
   {
    r=n1%10;
   sum+=r*r*r;
   n1=n1/10;
   }
 /* Check for Armstrong number */
 if(n == sum)
  {
    printf("%d, ", n);
  }
 n++;
  }
return 0;
}
Output:
Enter upper limit to find Armstrong number:
407
Armstrong number between 1 to 407 are:
1 153  370  371  407
Program Explanation:
1. In the program given above the variables r=0, limit, sum=0 ,n=1,n1; is declared as integer variable and assigned values.
variable limit is declared as the integer, the value in limit is used as the range to find Armstrong number 1 to limit .
2. printf("Enter upper limit to find Armstrong number: ");
scanf("%d", &limit);
printf() function prompt the user "Enter upper limit to find Armstrong number:" then control jumps to the input function
scanf("%d",&limit); and wait for the user input.
User inputs/enters number 407 which is get stored(scaned)in variable limit.
printf("Armstrong number between 1 to %d are: \n", limit);
--> displays message Armstrong number between 1 to 407 are:
1 153  370  371  407
Note:Here in this example we just explain working for number 153.
Consider the 153rd iteration of outer while loop.
value of n is 153 i.e. n=153.
let test the condition while (n<=limit) which is true ( while 153<=407)
variable sum=0 and n1=n is initialized , here value of n1 and n is 153.
3.1 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(153!=0) which results in true, statements inside body of loop
r=n1%10
sum+=r*r*r;
n1=n1/10; is get processed.

r=n1%10 ---> calculates the remainder r=153%10 ,here calculated reaminder 3 is stored in r i.e r=3.
sum+=r*r*r ---> calculate sum of cube of r , here value of sum=27.
n1=n1/10 ---> divide n1 by 10 after calculation here n1 is 15 i.e n1=15.
(153/10 result is 15 not 15.3 because of integer division.)
3.2 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(15!=0) which results in true, statements inside body of loop
r=n1%10;
sum+=r*r*r;
n1=n1/10; is get processed.

r=n1%10 ---> calculates the remainder r=15%10 ,here calculated reaminder 5 is stored in r i.e r=5.
sum+=r*r*r ---> calculate sum of cube of r add it to sum ,
previous value of sum is 27 ,here value of sum=sum+r*r*r is 152 i.e sum=152
n1=n1/10 ---> divide n1 by 10 after calculation here n1 becomes 1 i.e n1=1.
(15/10 result is 1 not 1.5 because of integer division.)
3.3 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(1!=0) which results in true, statements inside body of loop
r=n1%10
sum+=r*r*r;
n1=n1/10; is get processed.

r=n1%10 ---> calculates the remainder r=1%10 ,here calculated reaminder 1 is stored in r i.e r=1.
sum+=r*r*r ---> calculate sum of cube of r add it to sum , previous value of sum is 152 ,here value of sum=sum+r*r*r becomes 153 i.e sum=153
n1=n1/10 ---> divide n1 by 10 after calculation here n1 becomes 0 i.e n1=0.
(1/10 result is 0 not 0.1 because of integer division.)
3.4 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(0!=0) which results in false, control jumps out of inner while loop and start executing stataments inside body of outer while loop.
4. Executes the Statement.
if(n == sum)
  {
    printf("%d, ", n);
  }

here n=153 and sum=153
The condition tested in above if statement is true i.e if(153==153)
then executes statements printf("%d, ", n); inside body of if statement and displays output 153 .
value of n is incremented by 1 i.e. n++, here n becomes 154 i.e n=154
control jumps back to outer loop and test the condition while(n<=limit) which is true then start executing statemets inside body of outer loop.
The process continues to find all Armstrong number between 1 to 407 while value of n not becomes 408 i.e. n=408.
4. finally control come out of outer while loop when the value of n becomes greater than limit. Finally program stop the execution.


11. Write C Program to find all prime numbers from 1 to N using do while loop provided by the user..

#include <stdio.h>
/* Write C Program to find all Armstrong numbers from 1 to N using do while loop provided by the user.*/
#include <stdio.h>
int main()
{
int r=0, limit, sum=0 ,n=1,n1;
/* Input upper limit from user */
printf("Enter upper limit to find Armstrong number: ");
scanf("%d", &limit);
printf("Armstrong number between 1 to %d are: \n", limit);

  do{
   sum = 0;
   /* Calculate sum of power of digits */
   n1=n;
  
   do{
    r=n1%10;
   sum+=r*r*r;
   n1=n1/10;
   }while(n1!=0);
 /* Check for Armstrong number */
 if(n == sum)
  {
    printf("%d, ", n);
  }
 n++;
  }while(n<=limit);
return 0;
}
Output:
Enter upper limit to find Armstrong number:
407
Armstrong number between 1 to 407 are:
1 153  370  371  407
Program Explanation:
The working of this program is same as the while loop given in the above program.
except that the body in the do while loop executes at least once even though the condition tested is false.


Assignments/practicles using while/do while loop:
Practice/Assignment set 1:

1. Write a program in C to print ODD numbers from 1 to N using while and do while loop.
This is an example of while loop and do while loop - In this C program, we are going to study or write a program to show/print all ODD numbers from given range (1 to N) using while loop and do while loop?
2. Write C program to show EVEN numbers from 1 to N using while loop and do while loop.
This is an example of while and do while loop - In this C program, we are going to study how can we print or display all EVEN numbers from given range 1.. N using while and do while loop?
3.Write a program in C to print all uppercase alphabets using do while and while loop.
This is an example of while loop and do while loop in C programming language - In this C program, we are going to print all uppercase alphabets from ‘A’ to ‘Z’ using while loop and do while loop.
4. Write a program in c to display all lowercase alphabets using while and do while loop.
This is an example of while and do while loop in C programming language - In this C program, we are going to print all lowercase alphabets from ‘a’ to ‘z’ using do while and while loop.
5. C program to print numbers from 1 to 10 using while and do while loop.
This is an example of while and do while loop in C programming language - In this C program, we are going to print numbers from 1 to 10 using while and do while loop.
6.Write C program to read an integer and print its multiplication table using while and do while loop.
In this program, we are reading an integer number and printing its multiplication table, the programs are implemented using while and do while loop.
.7 C Program to check entered number is ZERO, POSITIVE or NEGATIVE until user does not want to quit.
This program will read an integer number and check whether entered number is Positive, Negative or Zero until user does not want to exit.
8. Write the Program in C language to find factorial of a number using while and do while.
In this program, we will read and integer number and find the factorial using different methods - using simple method
9. Write C Program to find sum of first N natural number, N must be provided by the user.
This program will read the value of N from user(keyboard)and calculate sum from 1 to N, first N natural numbers means numbers from 1 to N and N will be read through user.
10. C program to print all prime numbers from 1 to N using while and do while loop.
This program will read the value of N and print all prime numbers from 1 to N. The logic behind implement this program - Run loop from 1 to N and check each value in another loop, if the value is divisible by any number between 2 to num-1 (or less than equal to num/2) - Here num is the value to check it is prime of not.
11. C program to print all Armstrong numbers from 1 to N.
This program will read value of N and print all Armstrong Numbers from 1 to N.
12. C program to print all leap years from 1 to N using while and do while loop.
This program will read value of N and print all Leap Years from 1 to N years. There are two conditions for leap year: 1- If year is divisible by 400 , 2- If year is divisible by 4 and must not be divisible by 100 (for Non Century years).


Practice/Assignment set 2:
1. C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2
2. C program to find sum of the all natural numbers from 1 to N.
Series: 1+2+3+4+..N
3.C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
4) C program to find sum of following series:
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
5) C program to find sum of following series:
1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms


Previous Topic:-->> Write a program in C to print all uppercase alphabets using do while and while loop || Next topic:-->>C program to print numbers from 1 to 10 using while and do while loop.

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Technogies. All rights reserved.