C Excersice Programs PDF
C Excersice Programs PDF
C Excersice Programs PDF
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num;
printf("\nHello world!\nWelcome to Studytonight: Best place to learn\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("\n\nEnter a Character\n");
scanf("%c",&character);
printf("\n\nThe character that you have entered is %c", character);
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char c;
printf("Enter a character : ");
scanf("%c" , &c);
printf("\n\nASCII value of %c = %d",c,c);
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
scanf() and gets() both are used to take input from the user.
scanf() can only take input until it encounters a space. The words after space are ignored by
it.
gets() is used to take a single input at a time but can be used to input a complete sentence
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
gets(str);
printf("\n\nWelcome to Studytonight %s\n\n\n", str);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int number;
printf("Please enter a number:\n");
scanf("%d",&number);
/*
For single statements we can skip the curly brackets
*/
if(number < 100)
printf("Number is less than 100!\n");
else if(number == 100)
printf("Number is 100!\n");
else
printf("Number is greater than 100!\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
switch(grade)
{
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Keep it up!\n\n");
break;
case 'C':
printf("Well done\nbreak keyword takes execution to exit the switch
case\n\n");
break;
case 'D':
printf("You passed\n");
break;
case 'F':
printf("Better luck next time\n");
break;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
default:
printf("Invalid grade\n");
}
printf("Your grade is %c\n",grade);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
scanf("%c", &grade);
switch(grade)
{
case 'A':
printf("Excellent\n");
case 'B':
printf("\n\n\nKeep it up!\n\nNo break statement\n\nHence all the case
following this(but not the ones above this) except the default case will get
executed !\n\n");
case 'C':
printf("\n\n\t\tCase C : Well done !\n\n");
case 'D':
printf("\t\tCase D : You passed!\n\n");
case 'F':
printf("\t\tCase E : Better luck next time\n\n\n");
default:
printf("\t\tDefault Case : Invalid grade\n\n\n");
}
printf("Your grade is %c\n",grade);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Input a Character : ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("\n\n%c is a vowel.\n\n", ch);
break;
default:
printf("%c is not a vowel.\n\n", ch);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Output:
Explanation:
If break statement is not used for a case then all the cases following the valid
case are executed and evaluated. This way you can make your code easier
default is executed only if none of the above cases are true. It is similar to
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char alphabet;
printf("Enter an alphabet : ");
putchar('\n'); // to move to next Line
alphabet=getchar();
if(islower(alphabet))
putchar(toupper(alphabet));
else
// must be an uppercase character
printf("%c",tolower(alphabet)) ;
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
void main()
{
int x = 10, y = 15, temp;
temp = x;
x = y;
y = temp;
printf("x = %d and y = %d", x, y);
getch();
}
x = 15 and y = 10
void main()
{
int x = 10, y = 15;
x = x + y - (y = x);
printf("x = %d and y = %d",x,y);
getch();
}
x = 15 and y = 10
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
void main()
{
int x = 6, y = 4;
x = x^y;
y = x^y;
x = x^y;
printf("x = %d and y = %d", x, y);
getch();
}
x = 4 and y = 6
void main()
{
int x = 6, y = 4;
x = x*y;
y = x/y;
x = x/y;
printf("x = %d and y = %d", x, y);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
getch();
}
x = 4 and y = 6
Unlike local variables that can be used within the scope of a particular function.
& is used to assign the input value to the variable and store it at that particular location.
int a,b;
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("\n\nEnter the two values to find the greatest and smallest number:
\n");
scanf("%d%d", &a, &b);
if(a == b)
printf("Both are equal\n");
}
else //Only possibility remaining
{
printf("The largest number is %03d\n", a);
printf("The smallest number is %03d\n", b);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
3. Updation: Incrementing the loop variable to eventually terminate the loop not
Remember that the loop condition checks the conditional statement before it
loops again.
Syntax:
for(initialization, condition, incrementation)
{
code statements;
}
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
/*
Always declare the variables before using them
*/
int i = 0; // declaration and initialization at the same time
/*
consequently, when i equals 10, the loop breaks.
i is updated before the condition is checked-
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
3. Updation: Incrementing the loop variable to eventually terminate the loop not
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
/*
always declare the variables before using them
*/
int i = 0; // declaration and initialization at the same time
/*
while i is less than 10
*/
while(i<10)
{
printf("%d\n",i);
/*
Update i so the condition can be met eventually
to terminate the loop
*/
i++; // same as i=i+1;
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
3. Updation: Incrementing the loop variable to eventually terminate the loop not
Do while loop is used when the actual code must be executed atleast once. For
example: Incase of menu driven functions.
Below is a simple program on do while loop.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
/*
always declare the variables before using them
*/
int i = 10; // declaration and initialization at the same time
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("\n\nNested loops are usually used to print a pattern in c. \n\n");
printf("\n\nThey are also used to print out the matrix using a 2 dimensional
array. \n\n");
int i,j,k;
printf("\n\nOutput of the nested loop is :\n\n");
for(i = 0; i < 5; i++)
{
printf("\t\t\t\t");
for(j = 0; j < 5; j++)
printf("* ");
printf("\n");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Factorial of 5 is 120
int a, b, c, i = 3;
a = 0;
b = 1;
if(num == 1)
printf("%d",a);
if(num >= 2)
printf("%d\t%d",a,b);
0 1 1 2 3 5
void main()
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
{
int a, b, c, s = 0;
clrscr();
printf("Enter a number:\t");
scanf("%d", &a);
c = a;
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
while(n != 0)
{
remainder = n%10;
sum += remainder;
n = n/10;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
}
printf("The reverse string is %s\n", rev);
getch();
}
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, i;
float sum = 0, x;
return 0;
}
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,sum,i,t,a;
printf("\n\n\nThe Armstrong numbers in between 1 to 500 are : \n\n\n");
if(sum == i)
printf("\n\t\t\t%d", i);
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, sum = 0, c, t, a;
a = n%10;
sum += a*a*a;
n = n/10;
}
if(sum == t)
printf("\n\n\t\t%d is an armstrong number\n", t);
else
printf("\n\n\t\t%d is not an armstrong number\n", t);
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int x;
for(x = 0; x <= 10; x++)
{
if(x&1) // if number is odd
printf("\t\t\t%d is odd\n",x);
else if(!(x&1)) // ! is used inside if to reverse the boolean value
printf("\t\t\t%d is even\n",x);
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
We have used a simple for loop to input numbers and show how to use the
Bitwise operator. You can take input from user using scanf() and use th same
logic to find if the input number is odd or even.
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n;
printf("Enter a number: ");
scanf("%d",&n);
if((n/2)*2 == n)
printf("\n\n\t\t %d is Even\n", n);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
else
printf("\n\n\t\t %d is Odd\n", n);
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, i;
printf("Enter the number to find the factors of : ");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
scanf("%d",&num);
printf("\n\n\nFactors of %d are \n\n", num);
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,sum=0,c,value;
printf("Enter %d integers\n\n",n);
for(c = 1; c <= n; c++)
{
scanf("%d", &value);
/*
need to initialise sum before using otherwise
garbage value will get printed
*/
sum += value;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i = 3, count, c;
if(n >= 1)
{
printf("\n\nFirst %d prime numbers are : ", n);
printf("2 ");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
if(c == i) // c is prime
{
printf("%d ", i);
count++; // increment the count of prime numbers
}
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i;
float c,big;
printf("\n\nEnter the number of elements you wish to find the greatest element
of: ");
scanf("%d", &n);
printf("\n\nEnter %d numbers :\n", n);
printf("\n\n\t\t\tElement 1: ");
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
// same as while((--exp)!=-1)
while(exp-- > 0)
{
value *= n; // multiply n to itself exp times
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
#include<conio.h>
#include<string.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char number[10];
int flag = 0;
int length, i = 0;
length = strlen(number);
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
/*
temporarily storing elements into array b
starting from end of array a
*/
for(c = n-1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
/*
copying reversed array into original.
Here we are modifying original array to reverse it.
*/
return 0;
}
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
printf("\n\nEnter the location where you want to insert new element: ");
scanf("%d", &position);
In the above program we take an array as user input and then ask the user for a
new number that they wish to add to the original array, and the position where
they want to add the new number.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
The we shift the existing numbers from the index position to the end of the array
one position to the right, therby vacating a space for the new element. And then
we add the new number at the user specified position index.
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("\n\nEnter the location where you want to delete element from: ");
scanf("%d", &position);
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a[50], size, i, big, small;
scanf("%d", &size);
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, sum = 0, c, array[100];
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, c, n;
clrscr();
printf("Enter number of elements you want to sort: ");
scanf("%d", &n);
sorting(a, n);
getch();
}
5 3 4 2 1 6
1 2 3 5 6
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, m, c, d, matrix[10][10];
int counter = 0;
printf("\nEnter the number of rows and columns of the matrix \n\n");
scanf("%d%d",&m,&n);
Output:
columns.
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
/*
if the program is not terminated yet,
it means the matrix is symmetric
*/
printf("\n\nMatrix is Symmetric\n\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int a[2][2], i, j;
long determinant;
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Normal: Square root of the sum of the squares of each element of the matrix.
Diagonal Element: An element having same indices for row and column.
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
{
for(j = 0; j < n; j++) // to iterate the columns
{
scanf("%d", &aj[i][j]);
a = aj[i][j]*aj[i][j]; // finding square of each element
sum1 += a; // same as sum1 = sum1 + a
}
}
normal = sqrt((double)sum1); // typecasting to double value
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
/*
printing the first matrix
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
/*
printing the second matrix
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
/*
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
/*
finding the DIFFERENCE of the two matrices
and storing in another matrix difference of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &p, &q);
if(n != p)
printf("Matrices with the given order cannot be multiplied with each
other.\n\n");
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
}
printf("\n"); // to take the control to the next row
}
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int var = 24; // actual variable declaration
int *p;
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, i, *ptr, sum = 0;
/*
freeing memory of ptr allocated by malloc
using the free() method
*/
free(ptr);
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Accessing array
elements(Traversing array) by
incrementing a Pointer
Name of the array refers to the base address of the array.
Here we have a tutorial to understand How Pointer arithmetic works?
Below is a program to access elements of an array using pointer increment.
#include <stdio.h>
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
/*
storing address of the first element
of the array in pointer variable
*/
ptr = var;
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
/*
storing address of the last element
of the array in pointer variable
*/
ptr = &var[MAX-1];
Output:
Pointer Comparison in C
In C language pointers can be compared if the two pointers are pointing to the
same array.
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
All relational operators can be used for pointer comparison, but a pointer cannot
Multiplied or Divided.
Below is a program on pointer comparison for same type of pointer:
#include <stdio.h>
int main()
{
int *ptrA,*ptrB;
return(0);
}
int main()
{
int *ptrA;
float *ptrB;
return(0);
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int var;
int *ptr;
int **pptr;
var = 50;
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char str[100];
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
char rev[100];
char *sptr = str; // sptr stores the base address of the str
char *rptr = rev; // rptr stores the base address of the reverse
int i = -1;
sptr++;
rptr++;
}
Output:
int main()
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b;
int *ptra, *ptrb;
int temp;
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
// function pointer
int(*fptr)(int , int);
fptr = func;
// function calling
func(2,3);
fptr(2,3); // calling a function referring to pointer to a function
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int *ptr = NULL; // ptr is a NULL pointer
Output:
#include<stdio.h>
int y;
/*
Function to add two numbers and
return the result
*/
int add(int m, int n)
{
if(n == 0)
return m;
/*
Recursion: adding 1, n times and
then at the end adding m to it
*/
y = add(m, n-1) + 1;
return y; // return the result
}
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, r;
printf("Enter the two numbers:\n");
scanf("%d%d", &a, &b);
r = add(a, b); // function call
printf("\n\nSum of two numbers is: %d\n\n", r);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, f;
printf("\n\nEnter a number: ");
scanf("%d", &num);
f= fact(num);
printf("\n\nFactorial of %d is %d\n\n", num, f);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
{
if(aj==1 || aj==0)
return 1;
else
return (aj*fact(aj-1));
}
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int k, n;
long int i = 0, j = 1;
printf("Enter the length of the Fibonacci series: ");
scanf("%d", &n);
printf("\n\nfirst %d terms of Fibonacci series are:\n\n\n",n);
printf("%d ", 1);
printFibo(n);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, sum;
printf("\n\nEnter the range of n: ");
scanf("%d", &n);
sum = getSum(n);
printf("\n\nThe sum of first %d numbers is %d\n", n, sum);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
// function definition
int getSum(int aj)
{
/*
static variables hold their values
till the end of the program
*/
static int sum = 0;
if(aj > 0)
{
sum = sum + aj;
getSum(aj-1);
}
return sum ;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
void main()
{
int num, sum;
clrscr();
printf("Enter a number:\t");
scanf("%d", &num);
sum = sumOfDigit(num);
printf("The sum of digits of %d is: %d", num, sum);
getch();
}
/*
global declaration to use the same value
in both the functions
*/
int n;
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int palindrome;
printf("\n\nEnter a number to check for Palindrome: ");
scanf("%d", &n);
palindrome = isPal(n);
if(palindrome == 1)
printf("\n\n\n%d is palindrome\n\n", n);
else
printf("\n\n\n%d is not palindrome\n\n", n);
{
static int sum = 0;
if(aj != 0)
{
sum = sum *10 + aj%10;
isPal(aj/10); // recursive call same as while(n!=0) using loop
}
else if(sum == n)
return 1;
else
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int base, exp;
printf("Enter base number: ");
scanf("%d", &base);
printf("\n\nEnter Power factor: ");
scanf("%d", &exp);
printf("\n\n\n\t\t\t%d^%d = %d", base, exp, power(base, exp));
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int arr[MAX], max, i;
printf("\n\nEnter the size of the array: ");
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
scanf("%d", &size);
printf("\n\nEnter %d elements\n\n", size);
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Below is a program to find whether the user input number is a prime number or a composite
number using recursion.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, prime;
printf("Enter a positive number to check if Prime: ");
scanf("%d", &num);
prime = isPrime(num, num/2);
if(prime == 1)
{
printf("\n\n%d is a prime number\n\n", num);
}
else
{
printf("\n\n%d is a Composite number\n\n", num);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
// function definition
int isPrime(int n, int i)
{
if(i == 1)
return 1; // return statement terminates the recursive funtion
else
{
if(n%i == 0)
return 0;
else
isPrime(n, i-1); // recursive call not using return statement
}
}
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, lcm;
printf("\n\nEnter 2 integers to find LCM of:\n");
scanf("%d%d", &a, &b);
lcm = find_lcm(a,b); // function call
printf("\n\n LCM of %d and %d is: %d\n\n", a, b, lcm);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
Output:
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int a, b, gcd;
printf("\n\nEnter two numbers to find GCD of \n");
scanf("%d%d", &a, &b);
gcd = find_gcd(a, b);
printf("\n\nGCD of %d and %d is: %d\n\n", a, b, gcd);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
void main()
{
int i, j, k;
char str[100];
char *rev;
printf("Enter the string:\t");
scanf("%s", str);
printf("The original string is: %s\n", str);
rev = reverse(str);
printf("The reversed string is: %s\n", rev);
WWW.FOURSTEPSOLUTIONS.COM FOURSTEPS TRAINING SOLUTIONS PVT LTD
getch();
}