Write A C++ Program To Find Factors of A Number

Posted by w3codeworld on December 14th, 2020

There is a particular process to write a c++ program to find the factors of a number and print them. The program takes the given number as input from the user. Here we will use the while loop. The loop continues until the value is equal to the given number. There are some particular integer variables for this program. The variables are temp and number, where the temp is the loop variable, and the number is referred to as the given number. Thus, you can find out the factors of a number. The entire code to write a C++ program to find factors of a number is described here.

#include

using namespace std;

int main()

{

int number, temp = 1;

cout <<"Enter the number to determine its factors: "<< endl;

cin >> number;

cout <<"The factors of "<< number <<" are: "<< endl;

while (temp <= number)

{

if (not(number % temp))

cout << temp <<"";

temp++;

}

cout << endl;

}

C++ Language By Using The While Loop

All the loops are used to execute the blocks of the program, and while loop is not except for it. The loops continued till the upper limit. The c++ language by using while loop generates by three steps, which are initialization, condition, and completion. At first, the condition is checked. Here are the syntax and examples of c++ language by using the while loop.

 

Syntax

While(condition)

{

Statement(s);

}

 

Example

int i = 0;

while (i < 5)

{

cout << i <<"\n";

i++;

}

Real Numbers Without Using The Third Variable In The Java Language

There is a process to swap two real numbers without using the third variable in the Java language. The entire process needs some advanced knowledge of java. Here is the program to swap two real numbers without using the third variable in the java language.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

double p, q;

System.out.println("----Enter the two integer or real numbers----");

p = in .nextFloat();

q = in .nextFloat();

p = p - q;

q = p + q;

p = q - p;

System.out.printf("\nAfter swapping, p = %.2f", p);

System.out.printf("\nAfter swapping, q = %.2f\n", q);

}

}

Write A Java Program To Check The Input Integer Number

We can find out whether a number is an integer or not using the program. For that, we need to write a Java program to check the input integer number. Every element of an integer number must be a valid digit from 0 to 9. By writing a program in java language, you can check an integer. The program includes the concept of for loop and if-else statement. The entire process to write a java program to check the input integer number is mentioned here.

import java.io.*;

public class GFG {

static boolean isNumber(String s) {

for (int i = 0; i<s.length(); i++)

if (Character.isDigit(s.charAt(i)) == false)

return false;

return true;

}

static public void main(String[] args) {

String str = "6790";

if (isNumber(str))

System.out.println("Integer");

else

System.out.println("String");

}

}  

Like it? Share it!


w3codeworld

About the Author

w3codeworld
Joined: December 14th, 2020
Articles Posted: 2

More by this author