While Day 3
While Day 3
=======
----------------------------
Sample input : Enter base: 2
Enter exponent: 5
Sample output : 2^5 = 32
----------------------------
====================================================
#include<stdio.h>
while(i<exponent)
{
i++;
res=base*res;
printf("%d^%d=%d",base,exponent,res);
}
int main()
{ int base, exponent;
printf("Enter Base=>");scanf("%d",&base);
printf("Enter Exponent=>");scanf("%d",&exponent);
calpower(base,exponent);
Que 2 :
======
Write a C program to calculate the factorial of a given positive integer using a
while loop and user defined function.
The program should prompt the user to enter a number, compute its factorial, and
display the result.
-> If the user enters a negative number, the program should display an error
message: "Invalid input...Please enter a positive integer."
-> If the input is 0, the program should print Factorial of 0 = 1 because by
definition, 0!=1
-> FORMULA => n!=1×2×3×...×n.
int calfact()
{
long int n,fact=1;
printf("Enter any number=>");scanf("%ld",&n);
long int i=1;
if(n<0)
{
printf("Invalid input");
}
else{
while(n>=i)
{
fact=fact*n;
n--;
}
printf("%ld",fact);
}
}
int main()
{
calfact();
}
====================================================
Que 3 :
=======
====================================================
Que 4 :
=======
Write a C program to read one integer number from user and check it is Prime number
or not using a while loop and user defined function.
->it is a number that is not divisible by any other number except for 1 and itself.
#include<stdio.h>
int isprime()
{
int n,c;
printf("Enter number=>");scanf("%d",&n);
int i=1;
while(n>=i)
{
if(n%i==0)
{
c++;
}
i++;
}
if(c==2)
{
printf("it prime");
}
else{
printf("not prime");
}
}
int main()
{
isprime();
}
====================================================
Que 5 :
=======
Write a C program that converts a given integer into its corresponding word
representation for each digit while loop and user defined function.
The program should take an integer as input and output each digit of the number in
words, separated by spaces.