0% found this document useful (0 votes)
24 views4 pages

While Day 3

Uploaded by

ukeysaurav2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

While Day 3

Uploaded by

ukeysaurav2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Que 1 :

=======

Write a C program to calculate the power of a base number raised to an exponent


using a while loop and user defined function .
The program should take two inputs: the base and the exponent. If either the base
or the exponent is zero or negative,
the program should display the message "Invalid input...Please enter positive
values" and terminate. Otherwise,
the program should calculate and display the result as base ^ exponent = result.

Create user defined function having two parameter/argument.


function name : power()
parameter/argument : int base, int exponent

Sample input : Enter base: 2


Enter exponent: -5
Sample output : Invalid input...Please enter positive values

----------------------------
Sample input : Enter base: 2
Enter exponent: 5
Sample output : 2^5 = 32

----------------------------

Sample input : Enter base: 3


Enter exponent: 4
Sample output : 3^4 = 81

====================================================
#include<stdio.h>

int calpower(int base,int exponent)


{
int res=1;
int i=0;

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.

->Create user defined function having one parameter/argument.


function name : factorial()
parameter/argument : int num

Sample input : Enter a positive integer: -5


Sample output : Factorial of 5 = Invalid input...Please enter a positive integer.

Sample input : Enter a positive integer: 5


Sample output : Factorial of 5 = 120
#include<stdio.h>

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 :
=======

Write a C program to check whether a given integer is a palindrome using a while


loop and user defined function.
- A number is considered a palindrome if the reverse of the number is equal to the
original number
-The program should prompt the user to enter an integer, reverse the number, and
then compare it to the original number to determine if it is a palindrome.

-Create user defined function having one parameter/argument.


function name : palindrome()
parameter/argument : int num

Sample input : Enter an integer: 121


Sample output : 121 is palindrome number.

Sample input : Enter an integer: 1121


Sample output : 1121 is not palindrome number.

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

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.

-Create user defined function having one parameter/argument.


function name : isPrime()
parameter/argument : int num

Sample input : Enter an integer: 5


Sample output : 5 is prime number.

Sample input : Enter an integer: 6


Sample output : 6 is not prime number.

#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.

-Create user defined function having one parameter/argument.


function name : convertToLetters()
parameter/argument : int num

Sample input : 1256


Sample output : one two five six

You might also like