c Lab experiments 5-8
c Lab experiments 5-8
}
return s;
}
Output:
Enter a number 12345
The sum of digit 12345 is 15
Program 19: Write a C program to find Factorial of a number using for loop
#include <stdio.h>
#include<conio.h>
void main()
{
int fact, i, n;
fact=1;
printf("Enter the number:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf("Factorial of %d is %d", n, fact);
getch();
}
Output: Enter the number:7
Factorial of 7 is 5040
Program 20: Write a C program to find Factorial of a Number using Recursion.
#include <stdio.h>
#include<conio.h>
int factorial(int n);
void main()
{
int fact, i, n;
printf("Enter the number: ");
scanf("%d", &n);
fact=factorial(n);
printf("Factorial of %d is %d", n, fact);
getch();
}
int factorial(int n)
{
int fact=1;
if(n==1)
{
return fact;
}
else
{
fact=n*factorial(n-1);
return fact;
} }
Output: Enter the number: 5
Factorial of 5 is 120
Experiment – 8
Exercise on formatted input and output
Program 21: Write a C Program which prints integer number with integer input.
#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an Integer:");
scanf("%d", &testInteger);
printf("Enteed Number = %d", testInteger);
return 0;
}
Output: Enter an Integer:4
Enteed Number = 4
Program 22: Write a C Program which prints float number with floating input
#include <stdio.h>
int main()
{
float f;
printf("Enter a floating input: ");
scanf("%f", &f);
printf("Floating Value = %f", f);
return 0;
}
Output: Enter a floating input: 14.56
Floating Value = 14.560000
Program 23: Write a C program to print character and ASCII value of given Character.
#include <stdio.h>
int main()
{
char ch;
printf("Enter a Character: ");
scanf("%c", &ch);
printf("Entered Character %c \n", ch);
printf("ASCII value of %c is %d.", ch, ch);
return 0;
}
Output: Enter a Character: f
Entered Character f
ASCII value of f is 102.