C Programming (Project)
C Programming (Project)
C Programming
1. WAP to print the sum and product of digits of an integer.
Sol:
// C program to Print Sum and Product of Digits of an integer
#include <stdio.h>
#include <conio.h>
int main()
{
int n,n1; //type declaration // n=integer input by user
int dig, sum,pro;
n1=n;
while(n>0) //condition
{
dig=n%10; //get digit
sum+=dig; //sum of digits
pro*=dig; //product of digits
n=n/10;
}
getch();
return 0;
}
2|Page
int main()
{
int n, reversedNumber = 0, remainder; //type declaration //n=integer
while(n != 0) //condition
{
remainder = n%10; //digit separation
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
getch();
return 0;
}
3|Page
3. WAP to compute the sum of the first n terms of the following series,
S=1+1/2+1/3+1/4+……
Sol:
/* C Program to find the Sum of Series 1 + 1/2 + 1/3 + 1/4 + ... + 1/N*/
#include<stdio.h>
#include<conio.h>
int main()
{
double number, sum = 0, i; //type declaration
4. WAP to compute the sum of the first n terms of the following series,
S =1-2+3-4+5…………….
Sol:
#include<stdio.h>
#include<conio.h>
int series_sum(int n)
{
if (n%2==0)
return (-(n/2));
else
return ((n+1)/2);
}
int main ()
{
int num;
printf("Enter the number of terms : ");
scanf("%d", &num);
series_sum(num);
getch();
return 0;
}
1-2+3-4+5
5|Page
5. Write a function that checks whether a given string is Palindrome or not. Use
this function to find whether the string entered by user is Palindrome or not.
Sol:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main ()
{
char string1 [20];
int i, length; //type declaration
int h=0;
printf("Enter a string : "); //user input
scanf("%s", string1);
length = strlen (string1);
for(i=0; i<length; i++) //conditional loop
{
if ( string1[i] != string1[length-i-1])
{
h=1;
break;
}
}
if(h)
printf("%s is not a palindrome", string1);
else
printf("%s is a palindrome", string1); //output
getch ();
return 0;
}
6|Page
6. Write a function to find whether a given no. is prime or not. Use the same to
generate the prime numbers less than 100.
Sol:
//find whether a given no. is prime or not
#include <stdio.h>
#include<conio.h>
int main()
{
int n, i, flag = 0; //flag = Boolean variable
printf("Enter a positive integer: "); //user input
scanf("%d", &n);
for(i = 2; i <= n/2; ++i) //conditional loop
{
// condition for nonprime number
if(n%i == 0)
{
flag = 1; //if the number is even then flag is set to 1 (true)
break;
}
}
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
7|Page
if(num % i == 0)
{
printf("%d, ",i);
}
}
getch();
return 0;
}
9|Page
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;
getch ();
return 0;
}
9. WAP to print a triangle of stars as follows (take number of lines from user):
*
***
*****
*******
Sol:
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j,spc,rows,k;
printf("Input number of rows : "); //user input
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
spc--;
}
getch ();
return 0;
}
*
***
*****
*******
11 | P a g e
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{
char ch;
do
{
int n,sum=0,count=0;
float avg=0.0;
int b[100]; //integer array
printf("Enter the size of the array:");
scanf("\n%d",&n); //taking input for size
int arr[100];
int i,j;
for(i=0;i<n;i++)
{
scanf("\n%d",&arr[i]); //taking input for array elements
}
int max=arr[0],min=arr[0];
printf("\nEnter 1. to print even valued element");
printf("\nEnter 2. to print odd valued element");
12 | P a g e
for(i=0;i<n;i++)
{
13 | P a g e
for(i=1;i<n;i++)
{
if (arr[i]>max) //checking for maximum elements
{
max=arr[i]; //storing maximum elements
}
if (arr[i]<min) //checking minimum elements
{
min=arr[i]; //stroring minimum elements
}
}
printf("\n The maximum element is; %d",max);
printf("\n The minimum element is; %d",min);
break;
for(i=0;i<n;i++)
{
for(j=0;j<count;j++)
{
14 | P a g e
if(arr[i]==b[j])
break;
}
if(j==count)
{
b[count]=arr[i];
count++;
}
}
printf("\nArray after removing duplicate elemnts is:");
for(i=0;i<count;i++)
{
printf("\n%d",b[i]);
}
break;
for(i=(n-1);i>=0;i--)
{
printf("\n%d",arr[i]);
}
break;
default:
printf("\nEnter a valid no.");
break;
}
printf("\n Do you want to continue:");
printf("\n Enter Y to continue and N to stop:");
scanf("\n%c",&ch);
}
while(ch=='Y'|| ch=='y');
return 0;
}
15 | P a g e
Minimum of array is : 0
Enter your choice : Enter 4
Maximum of array is : 5
11. WAP that prints a table indicating the number of occurrences of each
alphabet in the text entered as command line arguments.
Sol:
#include <stdio.h>
#include <conio.h>
#include <conio.h>
int main()
int a=10,b=20;
return 0;
getch();
int t;
*x=*y;
*y=t;
a = 10, b = 20 a = 20, b = 10
19 | P a g e
14. Write a program which takes the radius of a circle as input from the user,
passes it to another function that computes the area and the circumference of
the circle and displays the value of area and circumference from the main()
function.
Sol:
#include<stdio.h>
#include<conio.h>
float area(float); //declaring the function
float cf(float);
int main()
{ //beginning of main
float r;
printf("Enter the radius of the circle : ");
scanf("\n %f",&r); //taking input for radius
float ar=area(r); //passing the radius to area function and
storing in a variable
float cr=cf(r);
printf("\nThe area is %f" ,ar);
printf("\n");
printf("\nThe perimeter is %f ",cr) ;
return 0;
getch();
}
float area(float c) //function to calculate area
{
return(3.14*c*c);
}
float cf(float d) //function to calculate circumference
{
return(2*3.14*d);
}
15. Write a program to find sum of n elements entered by the user. To write
this program, allocate memory dynamically using malloc() / calloc() functions
or new operator.
Sol:
#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements:");
scanf("\n%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //Allocating memory dynamically
//If memory not allocated
if(ptr==NULL)
{
printf("\nMemory not allocated");
exit(0);
}
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("\n%d",ptr+i); //taking input
sum+=*(ptr+i); // calculating and storing sum
}
printf("\nThe sum is %d",sum);
return 0;
}
case 2:
{
break;
}
case 3:
{
strcat(a,b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
23 | P a g e
char s[100];
printf("\nEnter a string : ");
scanf("%s", s);
break;
}
case 8:
{
int c = 0, count = 0;
char s[100];
printf("Input a string\n");
scanf("%s", s);
char s[100];
printf("Enter a string to reverse\n");
scanf("%s", s);
strrev(s);
printf("Reverse of the string: %s\n", s);
break;
}
default:
printf("Wrong choice !!!");
break;
}
getch ();
return 0;
}
25 | P a g e
17. Given two ordered arrays of integers, write a program to merge the two-
arrays to get an ordered array.
Sol:
#include<stdio.h>
#include <conio.h>
int main()
{
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;
printf("\nEnter no of elements in 1st array :");
scanf("%d", &n1);
for (i = 0; i < n1; i++)
{
scanf("%d", &arr1[i]);
}
printf("\nEnter no of elements in 2nd array :");
scanf("%d", &n2);
for (i = 0; i < n2; i++)
{
scanf("%d", &arr2[i]);
}
i = 0;
j = 0;
k = 0;
// Merging starts
while (i < n1 && j < n2)
{
if (arr1[i] <= arr2[j])
{
res[k] = arr1[i];
i++;
k++;
}
else
{
res[k] = arr2[j];
k++;
j++;
}
}
18. WAP to display Fibonacci series (i)using recursion, (ii) using iteration.
Sol:
// Using Recursion
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i=0, c; //n is the number of terms in fibonacci series
return 0;
}
// Using Iteration
#include<stdio.h>
#include<conio.h>
int main()
{
int n, first = 0, second = 1, next, c;
29 | P a g e
19. WAP to calculate Factorial of a number (i) using recursion, (ii) using
iteration.
Sol:
//Using Recursion
#include<stdio.h>
#include<conio.h>
long int factorial (int n); //declaration of the function
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n); //taking value of n from user
printf("Factorial of %d = %ld", n, factorial(n)); //function call
return 0;
}
long int factorial(int n) //function to calculate the factorial
{
if (n>=1)
return n*factorial(n-1);
else
return 1;
}
// Using iteration
#include <stdio.h>
#include<conio.h>
int main()
{
int c, n, f = 1;
20. WAP to calculate GCD of two numbers (i) with recursion (ii) without
recursion.
Sol:
// Using Recursion
#include <stdio.h>
#include <conio.h>
int hcf (int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int hcf(int n1, int n2) // recursive function to calculate GCD
{
if (n2 != 0)
return hcf(n2, n1 % n2); /*calculating the highest
common factor*/
else
return n1;
}
int main ()
{
int n;
printf("Enter the options Given Below");
printf("\n1. Sum of Matrices\n");
printf("\n2. Difference of Matrices\n");
printf("\n3. Product of Materices\n");
printf("\n4. Transpose of Matrices\n");
scanf("%d", &n);
switch (n)
{
case 1:
{
int r, c, a[100][100];
int b[100][100];
int sum[100][100];
int i, j;
printf("Enter the number of rows (between 1 and 100):");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100):");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
33 | P a g e
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nSecond Matrix :\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
s[i][j]=a[i][j]-b[i][j];
}
}
printf("\nDifference Between Matrices :\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%3d ",s[i][j]);
}
printf("\n");
}
break;
}
case 3:
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
35 | P a g e
case 4:
{
36 | P a g e
default:
printf("Please enter the correct option");
}
getch ();
return 0;
}
OUTPUT:
Enter the options Given Below
1. Sum of Matrices
37 | P a g e
2. Difference of Matrices
3. Product of Materices
4. Transpose of Matrices
Enter-1
Enter the number of rows (between 1 and 100): 3
Enter the number of columns (between 1 and 100): 3
81 83 85
87 89 91
38 | P a g e
22. Copy the contents of one text file to another file, after removing all whitespaces.
Sol:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
fclose(fptr1);
fclose(fptr2);
return 0;
}
39 | P a g e
23. Write a function that reverses the elements of an array in place. The
function must accept only one pointer value and return void.
Sol:
#include<stdio.h>
#include <conio.h>
// Function to reverse arr[] from start to end
void reverseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
printf("\n");
}
24. Write a program that will read 10 integers from user and store them in an
array. Implement array using pointers. The program will print the array
elements in ascending and descending order.
Sol:
#include <stdio.h>
#include<conio.h>
int main()
{
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
for (i = 0; i < n; i++) //Loop for ascending ordering
{
for (j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] > a[i]) //Comparing other array elements
{
int tmp = a[i]; /*Using temporary variable for storing
last value*/
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nAscending : "); //Printing message
for (i = 0; i < n; i++)//Loop for printing array data after sorting
{
printf(" %d ", a[i]);
}
for (i = 0; i < n; i++) //Loop for descending ordering
{
for (j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] < a[i]) //Comparing other array elements
{
int tmp = a[i]; /*Using temporary variable for storing
last value*/
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
41 | P a g e
}
printf("\n\nDescending : "); //Printing message
for (i = 0; i < n; i++) /*Loop for printing array data after
sorting*/
{
printf(" %d ", a[i]); //Printing data
}
getch();
return 0; //returning 0 status to system
}