Practice Program
Solution:through for loop
void main()
{
int i,n;
printf("Enter the number:");
scanf("%d",&n);
//As you know 1 is factor of every number ,
printf("Factors of %d are:",n);
//so we are printing it
printf("%d\t",1);
for(i=2;i<i++)
{
if(n%i == 0)//for every value of i we are checking that,
printf("%d\t",i); //Is n is divisible by i
} //so,if it is divisible by i then ,i
//is the one of the factor of
//n and we are printing it.
printf("%d",n);
//As you know every element is divisible by itself
//So we are printing the value of 'n' as one of the
//factor of 'n'
}
void main() { int i,n; printf("Enter the number:"); scanf("%d",&n); //As you know 1 is factor of every number , printf("Factors of %d are:",n); //so we are printing it printf("%d\t",1); for(i=2;i<i++) { if(n%i == 0)//for every value of i we are checking that, printf("%d\t",i); //Is n is divisible by i } //so,if it is divisible by i then ,i //is the one of the factor of //n and we are printing it. printf("%d",n); //As you know every element is divisible by itself //So we are printing the value of 'n' as one of the //factor of 'n' }
Output1
Enter the number:20
Factors of 20 are:: 1 2 4 5 10 20
Factors of 20 are:: 1 2 4 5 10 20
Output2
Enter the number:10
Factors of 10 are:
1 2 5 10
Factors of 10 are:
1 2 5 10
Output3
Enter the number:30
Factors of 30 are:
1 2 3 5 6 10 15 30
Factors of 30 are:
1 2 3 5 6 10 15 30
Solution:from while loops
void main()
{
int i,n;
printf("Enter the number:");
scanf("%d",&n);
//As you know 1 is factor of every number ,
//so we are printing it
printf("%d\t",1);
i=2;
while(i<n)
{
if(n%i == 0)//for every value of i we are checking that,
printf("%d\t",i); //Is n is divisible by i
i++; //so,if it is divisible by i then ,i
} //is the one of the factor of
//n and we are printing it.
printf("%d",n);
//As you know every element is divisible by itself
//So we are printing the value of 'n' as one of the
//factor of 'n'
}
void main() { int i,n; printf("Enter the number:"); scanf("%d",&n); //As you know 1 is factor of every number , //so we are printing it printf("%d\t",1); i=2; while(i<n) { if(n%i == 0)//for every value of i we are checking that, printf("%d\t",i); //Is n is divisible by i i++; //so,if it is divisible by i then ,i } //is the one of the factor of //n and we are printing it. printf("%d",n); //As you know every element is divisible by itself //So we are printing the value of 'n' as one of the //factor of 'n' }
Solution:from do-while loops
void main()
{
int i,n;
printf("Enter the number:");
scanf("%d",&n);
//As you know 1 is factor of every number ,
//so we are printing it
printf("%d\t",1);
i=2;
do
{
if(n%i == 0)//for every value of i we are checking that,
printf("%d\t",i); //Is n is divisible by i
i++; //so,if it is divisible by i then ,i
} while(i<n); //is the one of the factor of
//n and we are printing it.
printf("%d",n);
//As you know every element is divisible by itself
//So we are printing the value of 'n' as one of the
//factor of 'n'
}
void main() { int i,n; printf("Enter the number:"); scanf("%d",&n); //As you know 1 is factor of every number , //so we are printing it printf("%d\t",1); i=2; do { if(n%i == 0)//for every value of i we are checking that, printf("%d\t",i); //Is n is divisible by i i++; //so,if it is divisible by i then ,i } while(i<n); //is the one of the factor of //n and we are printing it. printf("%d",n); //As you know every element is divisible by itself //So we are printing the value of 'n' as one of the //factor of 'n' }
defining a function 'give_factor()' which will print factors of number.
void give_factor(n)
{ int i;
//As you know 1 is factor of every number ,
//so we are printing it
printf("%d\t",1);
i=2;
while(i<n)
{
if(n%i == 0)//for every value of i we are checking that,
printf("%d\t",i); //Is n is divisible by i
i++; //so,if it is divisible by i then ,i
} //is the one of the factor of
//n and we are printing it.
printf("%d",n);
//As you know every element is divisible by itself
//So we are printing the value of 'n' as one of the
//factor of 'n'
}
void give_factor(n) { int i; //As you know 1 is factor of every number , //so we are printing it printf("%d\t",1); i=2; while(i<n) { if(n%i == 0)//for every value of i we are checking that, printf("%d\t",i); //Is n is divisible by i i++; //so,if it is divisible by i then ,i } //is the one of the factor of //n and we are printing it. printf("%d",n); //As you know every element is divisible by itself //So we are printing the value of 'n' as one of the //factor of 'n' }
Using above define function 'give_factor()':
void main()
{
int i,n;
printf("Enter the number:");
scanf("%d",&n);
printf(" Factors of %d are:",n);
//calling function
give_factor(n); //actual parameters.
}
void main() { int i,n; printf("Enter the number:"); scanf("%d",&n); printf(" Factors of %d are:",n); //calling function give_factor(n); //actual parameters. }
Output1
Enter the number:20
Factors of 20 are: 1 2 4 5 10 20
Factors of 20 are: 1 2 4 5 10 20
Using above define function 'give_factor()':
void main()
{
int i,n;
printf("Enter the number:");
scanf("%d",&n);
printf(" Factors of %d are:",n);
//calling function
give_factor(n); //actual parameters.
}
void main() { int i,n; printf("Enter the number:"); scanf("%d",&n); printf(" Factors of %d are:",n); //calling function give_factor(n); //actual parameters. }
Output2
Enter the number:10
Factors of 10 are:
1 2 5 10
Factors of 10 are:
1 2 5 10
Using above define function 'give_factor()':
void main()
{
int i,n;
printf("Enter the number:");
scanf("%d",&n);
printf(" Factors of %d are:",n);
//calling function
give_factor(n); //actual parameters.
}
void main() { int i,n; printf("Enter the number:"); scanf("%d",&n); printf(" Factors of %d are:",n); //calling function give_factor(n); //actual parameters. }
Output2
Enter the number:30
Factors of 30 are:
1 2 3 5 6 10 15 30
Factors of 30 are:
1 2 3 5 6 10 15 30
0 Comments
Post a Comment