[go: up one dir, main page]

0% found this document useful (0 votes)
7 views34 pages

C Examples

The document contains a series of C programming exercises focused on various problem-solving techniques, including finding the GCD of two numbers, calculating maximum and minimum values, generating Fibonacci sequences, and working with matrices. Each exercise includes test cases, code implementations, and expected outputs. The exercises cover a range of topics such as array manipulation, mathematical computations, and data structure handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views34 pages

C Examples

The document contains a series of C programming exercises focused on various problem-solving techniques, including finding the GCD of two numbers, calculating maximum and minimum values, generating Fibonacci sequences, and working with matrices. Each exercise includes test cases, code implementations, and expected outputs. The exercises cover a range of topics such as array manipulation, mathematical computations, and data structure handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

SCSA2105 - Problem Solving Techniques Lab

Cycle1 - Ex1 - a. GCD of two numbers (using for loop)

TestCase:
Input: 81 153
Output: 9

Code:
#include <stdio.h>
int main()
{
int Num1, Num2, i, GCD;
scanf("%d %d", &Num1, &Num2);
for(i = 1; i <= Num1 && i <= Num2; i++)
{
if(Num1 % i == 0 && Num2 % i == 0)
GCD = i;
}
printf("%d",GCD);
return 0;
}

Cycle1 - Ex1 - b. GCD of two numbers (using while loop)

TestCase:
Input: 81 153
Output: 9

Code:
#include <stdio.h>
int main()
{
int Num1, Num2, Temp, GCD;
scanf("%d %d", &Num1, &Num2);
while (Num2 != 0) {
Temp = Num2;
Num2 = Num1 % Num2;
Num1 = Temp;
}
GCD = Num1;
printf("%d", GCD);
return 0;
}

Cycle 1 - Ex-2: Max and Min out of given three numbers using functions:

TestCase:
Input: 85 174 96
Output: 174 85

#include<stdio.h>
int main()
{
int max(int a, int b, int c);
int min(int a, int b, int c);
int a,b,c,d,e;
scanf("%d %d %d",&a,&b,&c);
d=max(a,b,c);
e=min(a,b,c);
printf("%d %d",d,e);
return 0;
}

int max(int a, int b, int c)


{
int max;
if(a > b)
{
if(a > c)
{
max = a;
}
}
else
{
if(b > c)
{
max = b;
}
else
{
max = c;
}
}
return max;
}

int min(int a, int b, int c)


{
int min;
if(a < b)
{
if(a < c)
{
min = a;
}
}
else
{
if(b < c)
{
min = b;
}
else
{
min = c;
}
}
return min;
}

Cycle 1 - Ex-3: Exponentiation of given number and its base using while loop

TestCase:
Input: 55 7
Output: 1522435234375

#include <stdio.h>
int main()
{
int base, exp;
long int result = 1.0;
scanf("%d %d",&base,&exp);
while (exp != 0)
{
result *= base;
exp--;
}
printf("%ld", result);
return 0;
}
Cycle 1 - Ex-4: Sum of array elements

TestCase:
Input: 7
33 65 72 31 98 67 43
Output: Sum of array : 409

Code:
#include <stdio.h>
int main()
{
int a[100],i,size,sum=0;
scanf("%d",&size);
for(i=0; i<size; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<size; i++)
{

sum=sum+a[i];
}
printf("Sum of array : %d",sum);

return 0;
}

Cycle 1 - Ex-5: Generate Fibonacci Sequence

TestCase:
Input:
9
Output:
Fibonacci Series: 0 1 1 2 3 5 8 13 21
Code:
#include <stdio.h>
int main() {

int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
scanf("%d", &n);
printf("Fibonacci Series: %d %d ", t1, t2);
for (i = 3; i <= n; ++i)
{
printf("%d ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

Cycle 1 - Ex-6: Generate Sine series

Write a C program to compute Sine series implementation.

Test case:
Input :
45 4
Output :
The value of Sin(0.785398) = 0.7071

Code:

#include<stdio.h>
int main()
{
int i, n;
float x, sum, t;
scanf("%f",&x);
scanf("%d",&n);

x=x*3.14159/180;
t=x;
sum=x;

for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}

printf("The value of Sin(%f) = %.4f",x,sum);


return 0;
}

Cycle 1 - Ex-7: Quadratic Equation Roots

Write a C program to compute roots of a given quadratic equation using its coefficients.
Test case:
Input :
5.8 6 9.3
Output :
root1 = -0.52+1.16i and root2 = -0.52-1.16i
Code:

#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}

return 0;
}

Cycle 1 - Ex-8: Reversing digits

Write a C program to reverse the digits of any given integer. .


Test case:
Input :
5.8 6 9.3
Output :
root1 = -0.52+1.16i and root2 = -0.52-1.16i

#include <stdio.h>
int main()
{
int n, rev = 0, remainder;
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

Cycle 1 - Ex-9: Reversing digits

Write a C program to compute the smallest divisor of the given number.


Test case:
Input :
105
Output :
The smallest number is 3

#include <stdio.h>
#include <math.h>

int main()
{
int n, i, sq;
scanf("%d", &n);
i = 2;
sq = sqrt(n);
while(i <= sq){
if(n % i == 0){
printf("The smallest number is %d\n", i);
break;
}
i++;
}
if(i > sq){
printf("The smallest number is %d\n", n);
}
}

Cycle 1 - Ex-11: Exponent

Write a C program to raise power to large number without power function.


Test case:
Input :
9 24
Output :
Answer = 79766443076872509865984

#include <stdio.h>
int main()
{
int base, exp;
long double result = 1.0;
scanf("%d", &base);
scanf("%d", &exp);
while (exp != 0)
{
result *= base;
--exp;
}
printf("Answer = %.0Lf", result);
return 0;
}

Cycle 1 - Ex-11: Exponent

Write a C program to raise power to large number with power function.


Test case:
Input :
9 24
Output :
9.0^24.0 = 79766443076872514306048.00

#include <math.h>
#include <stdio.h>

int main() {
double base, exp, result;
scanf("%lf", &base);
scanf("%lf", &exp);

result = pow(base, exp);

printf("%.1lf^%.1lf = %.2lf", base, exp, result);


return 0;
}

Cycle 1 - Ex-12: Remove Duplicates

Write a C program to raise power to large number with power function.


Test case:
Input :
9 25 12 85 96 48 77 63 85 34
9 - size of array, rest input - elements in array
Output :
25 12 85 96 48 77 63 34

#include <stdio.h>
int main ()
{
int arr[20], i, j, k, size;
scanf (" %d", &size);
for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);
}
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
if ( arr[i] == arr[j])
{
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
size--;
j--;
}
}
}
for ( i = 0; i < size; i++)
{
printf (" %d ", arr[i]);
}
return 0;
}

Cycle 1 - Ex-13: Finding kth smallest element

Write a C program to raise power to find kth smallest element.

Test case:
Input :
10
23 76 98 11 54 72 89 45 63 32
4
10 - size of array, rest input - elements in array, 4 - k value
Output :
11 23 32 45 54 63 72 76 89 4th smallest element is 45

#include<stdio.h>
void find(int a[20], int n, int k)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=1;i<n;i++)
{
printf("%d ",a[i]);
}
for(i=1;i<k;i++);
printf("%dth smallest element is %d",k,a[i]);
}

int main()
{
int i,n,a[20],k;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&k);
find(a,n,k);
}

Cycle 1 - Ex-15: Matrix Addition

Write a C program to add two matrices.


Test case:
Input :
3
3
22 33 44 11 23 21 66 87 45
66 44 23 78 12 45 98 65 37
3*3 - row*column value, rest input - matrix elements
Output :
Sum of two matrices:
88 77 67

89 35 66

164 152 82

#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
scanf("%d", &r);
scanf("%d", &c);
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
scanf("%d", &a[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1)
{
printf("\n\n");
}
}

return 0;
}

Cycle 1 - Ex-15: Matrix Multiplication

Write a C program to multiply two matrices.


Test case:
Input :
3
3
22 41 31 54 32 65 78 69 54
33 11 65 34 62 54 56 89 65
3*3 - row*column value, rest input - matrix elements
Output :
Multiplication result of two matrices =
3856 5543 5659
6510 8363 9463
7944 9942 12306

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
scanf("%d",&r);
scanf("%d",&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("Multiplication result of two matrices = \n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",mul[i][j]);
}
printf("\n");
}
return 0;
}
Write a C program to raise power to generate histogram.
Test case:
Input :
9
4255128423
Output :
11
23
42
52
81

#include<iostream>
using namespace std;
class histogram{
int a[15],n,*count,max;
public:
void init();
void calculate();
void display();
};

void histogram::init(){
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
}

void histogram::calculate(){
max=a[0];
for(int i=1;i<n;i++)
if(max<a[i])
max=a[i];
count=new int[max+1];
for(int i=0;i<=max;i++)
count[i]=0;
for(int i=0;i<n;i++)
count[a[i]]++;
}

void histogram::display(){
for(int i=0;i<=max;i++)
if(count[i] != 0)
cout<<i<<" "<<count[i]<<endl;
}

int main(){
histogram s;
s.init();
s.calculate();
s.display();
return 1;
}

Write a C program to convert decimal number to binary number.


Test case:
Input :
761
Output :
Binary of the given number= 1011111001

#include <iostream>
using namespace std;
int main()
{
int a[10], n, i;
cin>>n;
for(i=0; n>0; i++)
{
a[i]=n%2;
n= n/2;
}
cout<<"Binary of the given number= ";
for(i=i-1 ;i>=0 ;i--)
{
cout<<a[i];
}
}

Write a C program to convert binary number to decimal number.


Test case:
Input :
1011111001
Output :
Equivalent Decimal Value = 761

#include<iostream>

using namespace std;

int main()

int binnum, decnum=0, i=1, rem;

cin>>binnum;

while(binnum!=0)

{
rem = binnum%10;

decnum = decnum + (rem*i);

i = i*2;

binnum = binnum/10;

cout<<"Equivalent Decimal Value = "<<decnum;

cout<<endl;

return 0;

Write a C program to delete n characters from given position in a given string.


Test case:

Input :

codingisfun

35

Output :

codinfun

using namespace std;

#include<iostream>

class string1{

char a[50];
int n,i;

public:

void init();

void delet();

void display();

};

void string1::init(){

cin>>a;

cin>>n>>i;

void string1::delet(){

int l=0,j,o;

while(a[l]!='\0')

l++;

l--;

j=i;

o=i+n;
while(o<=l){

a[j]=a[o];

j++;

o++;

a[j]='\0';

void string1::display(){

cout<<a;

int main(){

string1 s;

s.init();

s.delet();

s.display();

return 0;

}
Write a C program to search elements through linear search technique.
Test case:

Input :

33 677 654 123 984 563 267 178 345 425

345

Output :

Found at Index No.8

#include<iostream>

using namespace std;

int main()

int arr[10], i, num, index;

for(i=0; i<10; i++)

cin>>arr[i];

cin>>num;

for(i=0; i<10; i++)

if(arr[i]==num)
{

index = i;

break;

cout<<"Found at Index No."<<index;

cout<<endl;

return 0;

Write a C program to sort the elements in ascending order.


Test case:

Input :

10

33 677 654 123 984 563 267 178 345 425

Output :

Unsorted Array elements:

33 677 654 123 984 563 267 178 345 425

Sorted (Ascending Order) Array elements:

33 123 178 267 345 425 563 654 677 984


#include <iostream>

using namespace std;

#define MAX 100

int main()

//array declaration

int arr[MAX];

int n,i,j;

int temp;

cin>>n;

//check bound

if(n<0 || n>MAX)

cout<<"Input valid range!!!"<<endl;

return -1;

}
//read n elements

for(i=0;i<n;i++)

cin>>arr[i];

//print input elements

cout<<"Unsorted Array elements:"<<endl;

for(i=0;i<n;i++)

cout<<arr[i]<<" ";

cout<<endl;

//sorting - ASCENDING ORDER

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(arr[i]>arr[j])

{
temp =arr[i];

arr[i]=arr[j];

arr[j]=temp;

//print sorted array elements

cout<<"Sorted (Ascending Order) Array elements:"<<endl;

for(i=0;i<n;i++)

cout<<arr[i]<<" ";

cout<<endl;

return 0;

Write a C program to sort the elements in descending order.


Test case:

Input :
10

56 78 34 51 23 79 44 36 55 98

Output :

Unsorted Array elements:

56 78 34 51 23 79 44 36 55 98

Sorted (Descending Order) Array elements:

98 79 78 56 55 51 44 36 34 23

#include <iostream>

using namespace std;

#define MAX 100

int main()

//array declaration

int arr[MAX];

int n,i,j;

int temp;

cin>>n;
//check bound

if(n<0 || n>MAX)

cout<<"Input valid range!!!"<<endl;

return -1;

//read n elements

for(i=0;i<n;i++)

cin>>arr[i];

//print input elements

cout<<"Unsorted Array elements:"<<endl;

for(i=0;i<n;i++)

cout<<arr[i]<<" ";

cout<<endl;
//sorting - Descending ORDER

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(arr[i]<arr[j])

temp =arr[i];

arr[i]=arr[j];

arr[j]=temp;

//print sorted array elements

cout<<"Sorted (Descending Order) Array elements:"<<endl;

for(i=0;i<n;i++)

cout<<arr[i]<<" ";

cout<<endl;
return 0;

Write a C program to find factorial of given number using recursion.


Test case:

Input :

Output :

Factorial of 17 = -288522240

#include<iostream>

using namespace std;

int factorial(int n);

int main()

int n;

cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);
return 0;

int factorial(int n)

if(n > 1)

return n * factorial(n - 1);

else

return 1;

Write a C program to find factorial of given number without using recursion.


Test case:

Input :

Output :

Factorial of 8 is: 40320

#include <iostream>

using namespace std;

int main()

{
int i,fact=1,number;

cin>>number;

for(i=1;i<=number;i++){

fact=fact*i;

cout<<"Factorial of " <<number<<" is: "<<fact<<endl;

return 0;

You might also like