Wa0000
Wa0000
Wa0000
}
printf("Total elements divisible by %d is %d\n",b, count);
return 0;
}
2. Write a c program to cyclically permute the element of an array. (In right to left
direction).
#include <stdio.h>
#include <stdlib.h>
void print(int* a,int n){
printf("printing ........\n");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
int* cyclicallyPermute(int* a,int n)
{
int temp=a[0];
for(int i=0;i<n;i++)
{
if(i==n-1)
a[i]=temp;
else
a[i]=a[i+1];
}
return a;
}
int main()
{
int n;
printf("enter array length: ");
scanf("%d",&n);
int* a=(int*)(malloc(sizeof(int)*n));
printf("enter elements: \n");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array before permutation\n");
print(a,n);
a=cyclicallyPermute(a,n);
printf("array after permutation\n");
print(a,n);
return 0;
}
3.Split the given array into K sub-arrays such that maximum sum of all sub arrays is
minimum.
#include <conio.h>
int count = 0;
int sum = 0;
return False;
sum += array[i];
count++;
sum = array[i];
count++;
if (count <= K)
return true;
return false;
int start = 1;
int end = 0;
end += array[i];
int answer = 0;
answer = mid;
end = mid - 1;
else {
start = mid + 1;
return answer;
int main()
int array[] = { 1, 2, 3, 4 };
int K = 3;
cout<<solve(array, n, K);
#include <conio.h>
{
int left = ranges[i][0];
if (index == left)
index = right;
else
index--;
return arr[index];
int main()
int arr[] = { 1, 2, 3, 4, 5 };
int rotations = 2;
int ranges[2][2] = { { 0, 2 }, { 0, 3 } };
int index = 1;
return 0;
#include <conio.h>
int sum = 0;
sum -= (2 * arr[i]);
return sum;
int main()
int arr[] = { 4, 2, 1, 8 };
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
#include <stdio.h>
#define R 4
#define C 4
int i, k = 0, l = 0;
k++;
n--;
if (k < m)
m--;
if (l < n)
l++;
int main()
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
spiralPrint(R, C, a);
return 0;
2.You are given a two-dimensional 3*3 array starting from A [0][0]. Write a program to add
the alternate elements of the array and print its sum.
(It should print two different numbers the first being sum of A[ 0][ 0], A [0][ 2], A[ 1] [1],
A [2][ 0], A [2][ 2] and A [0][ 1], A [1] [0], A [1][ 2], A [2][ 1])
Input :
1 2 3
4 5 6
7 8 9
Output:
25
20
#include <bits/stdc++.h>
#include<conio.h>
using namespace std;
void sumAlternate(int* arr, int n)
{
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n * n; i++)
{
if (i % 2 == 0)
sum1 += *(arr + i);
else
sum2 += *(arr + i);
}
cout << "Sum of alternate elements : " << sum1
<< ", " << sum2 << endl;
}
int main()
{
int mat[3][3] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
sumAlternate(&mat[0][0], n);
return 0;
}
3.Write a C program to find the minimum sum path from (0,0) to (N-1,N-1) in
given N x Nmatrix. Only right , down moves are allowed.
Input
12 71 62 10 4
26 48 35 25 11
66 10 49 100 17
78 60 53 2 85
87 29 12 28 90
Output:
12+26+48+10+49+53+12+28+90=32
#include<stdio.h>
#include<limits.h>
#define R 5
#define C 5
if (n < 0 || m < 0)
return INT_MAX;
else if (m == 0 && n == 0)
return cost[m][n];
else
minCost(cost, m, n-1) );
{
if (x < y)
else
int main()
{26,48,35,25,11},
{66,10,49,100,17},
{78,60,53,2,85},
{87,29,12,28,90} };
return 0;
4.Write a program to check whether the given matrix is upper triangular matrix or not.
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
int main()
int array[MAX_ROWS][MAX_COLS];
scanf("%d", &array[row][col]);
isUpper = 1;
isUpper = 0;
if(isUpper == 1)
printf("\n");
else
return 0;
5.Write a program to find the sum of elements in the zig-zag sequence in a given matrix.
Input:
1 2 3
4 5 6
7 8 9
Output:
1+2+3+5+7+8+9=35
#include<stdio.h>
#include<conio.h>
int main()
scanf("%d%d",&m,&n);
int i, j;
int mat[m][n];
scanf("%d",&mat[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
if ((i + j) == (m -1))
diag += mat[i][j];