PRACTICAL PROGRAMS:
Program 1A: Write a program that defines a function to add first n numbers. (Without Recursion)
#include <stdio.h>
void addNumbers(int num);
int main( )
{
int n;
printf("Enter a number : ");
scanf("%d", &n);
addNumbers(n);
return 0;
}
void addNumbers (int num)
{
int i=1, sum=0;
while ( i <= num )
{
sum=sum + i;
i++;
}
printf("sum of 1 to %d = %d", num, sum);
}
Output:
Enter a number : 3
sum of 1 to 3 = 6
Program 1B: Write a program that defines a function to add first n numbers. (With Recursion)
#include <stdio.h>
int sum(int n);
void main( )
{
int number, result;
printf("Enter a number : ");
scanf("%d", &number);
result = sum(number);
printf("sum of natural number till entered value is = %d", result);
}
int sum (int n)
{
if(n!=0)
{
return n + sum(n-1);
}
else
{
return n;
}
}
Output:
Enter a number : 3
sum of natural number till entered value is = 6
Program 2: Write a function check-prime() in the program to return 1 if number is prime otherwise return 0.
#include <stdio.h>
int checkPrime(int num);
int main( )
{
int n, prime;
printf("Enter a number : ");
scanf("%d", &n);
prime = checkPrime(n);
if( prime == 1 )
{
printf("%d is prime number", n);
}
else
{
printf("%d is not prime number", n);
}
return 0;
}
int checkPrime (int num)
{
int i = 2;
while ( i < num )
{
if( num % i == 0 )
{
return 0;
}
i++;
}
return 1;
}
Output:
Enter a number : 13
13 is prime number
Program 3A: Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function. (Using call by value).
#include <stdio.h>
void exchange(int x, int y);
int main( )
{
int a, b;
printf("Enter value of a : ");
scanf("%d", &a);
printf("Enter value of b : ");
scanf("%d", &b);
printf("Before Exchange a = %d and b = %d", a, b);
exchange(a, b);
return 0;
}
void exchange (int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("\nAfter Exchange x = %d, y = %d", x, y);
}
Output:
Enter value of a : 10
Enter value of b : 5
Before Exchange a = 10 and b = 5
After Exchange x = 5, y = 10
Program 3B: Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function. (Using call by reference).
#include <stdio.h>
void exchange(int *x, int *y);
int main( )
{
int a, b;
printf("Enter value of a : ");
scanf("%d", &a);
printf("Enter value of b : ");
scanf("%d", &b);
printf("Before Exchange a = %d and b = %d", a, b);
exchange(&a, &b);
printf("\nAfter Exchange a = %d and b = %d", a, b);
return 0;
}
void exchange (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output:
Enter value of a : 10
Enter value of b : 20
Before Exchange a = 10 and b = 20
After Exchange a = 20 and b = 10
Program 4: Write a program to calculate nCr using user defined function. nCr = n! / (r! * (n-r)!)
#include <stdio.h>
int fact (int);
int main( )
{
int n, r, ncr;
printf("Enter number n : ");
scanf("%d", &n);
printf("Enter number r : ");
scanf("%d", &r);
ncr = fact(n) / ( fact(r) * fact(n-r) );
printf("value of %dC%d = %d", n, r, ncr);
return 0;
}
int fact (int n)
{
int i, f=1;
for ( i = 1; i<=n ; i++ )
{
f = f * i;
}
return f;
}
Output:
Enter number n : 10
Enter number r : 5
value of 10C5 = 252
Program 5: Write a program to find factorial of a number using recursion.
#include <stdio.h>
int fact(int);
void main()
{
int n, f;
printf("Enter a number : ");
scanf("%d", &n);
f = fact(n);
printf("factorial = %d", f);
}
int fact(int n)
{
if ( n = = 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
Output:
Enter a number : 5
Factorial = 120
Program 6: Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents.
#include <stdio.h>
void uppercase(char *);
int main( )
{
char str[50];
printf("Enter string : ");
gets(str);
uppercase(str);
printf("string in upper case %s", str);
return 0;
}
void uppercase(char *ch)
{
while (*ch != '\0')
{
if(*ch>='a' && *ch<='z')
{
*ch = *ch-32;
}
ch++;
}
}
Output:
Enter string : comPUter
string in upper case COMPUTER
Program 7: Develop a menu-based program to perform addition, multiplication, subtraction and division using user-defined function. (Using switch case).
#include <stdio.h>
void add(int a, int b);
void sub(int a, int b);
void mul(int a, int b);
void div(int a, int b);
void main( )
{
int x, y, choice;
printf("Enter first number : ");
scanf("%d", &x);
printf("Enter second number : ");
scanf("%d", &y);
printf("1 = add\n2 = sub\n3 = mul\n4 = div\n");
printf("Enter choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
add(x, y);
break;
case 2:
sub(x, y);
break;
case 3:
mul(x, y);
break;
case 4:
div(x, y);
break;
default:
printf("wrong choice");
break;
}
}
void add(int a, int b)
{
printf("\nadd = %d", a+b);
}
void sub(int a, int b)
{
printf("\nsub = %d", a-b);
}
void mul(int a, int b)
{
printf("\nmul = %d", a*b);
}
void div(int a, int b)
{
printf("\ndiv = %f", (float)a/b);
}
Output:
Enter first number : 10
Enter second number : 5
1 = add
2 = sub
3 = mul
4 = div
Enter choice : 4
div = 2.000000
PRACTICE PROGRAMS:
Practice Program 1: Write a C program to use recursive calls to evaluate F(x) = x – x3/3! + x5/5! – x7/7! + … xn/ n!
#include <stdio.h>
#include <math.h>
float series(float, int);
float power(float x, int y);
long factorial(int p);
void main( )
{
int n;
float x;
printf("Enter value of x : ");
scanf("%f", &x);
printf("Enter no of iteration n : ");
scanf("%d", &n);
printf("\nAns %f", series(x, n));
getch();
}
float series(float x, int n)
{
float sum=0;
int i, s=1;
for( i = 1 ; i <= n*2 ; i+=2)
{
sum = sum + ( power( x , i ) / factorial( i ) ) * s;
s = s*-1;
}
return sum;
}
long factorial(int p)
{
long f=1;
int i;
for(i = 1 ; i<=p ; i++)
{
f = f * i ;
}
return f;
}
float power(float x, int y)
{
float p=1;
int i;
for( i = 1 ; i <= y ; i++)
{
p = p * x ;
}
return p;
}
Output:
Enter X:2
Enter n:4
Ans 0.907936
Practice Program 2: Write a C program using global variable, static variable.
#include <stdio.h>
void stat ( );
int i; //Global Variable
int main( )
{
for(i=0; i<=3; i++)
{
stat( );
}
return 0;
}
void stat ( )
{
static int x=0;
x= x+1;
printf("value of x = %d for iteration %d\n", x, i);
}
Output:
value of x = 1 for iteration 0
value of x = 2 for iteration 1
value of x = 3 for iteration 2
value of x = 4 for iteration 3