Sunteți pe pagina 1din 10

1//Write a program to find gcd/hcf of given two numbers using recursion

//Eg: GCD(18,24)=6 , GCD(23,9)=1

#include <stdio.h>

int gcd(int ,int );

int main()

int n1,n2;

printf("Enter first number:");

scanf("%d",&n1);

printf("Enter second number:");

scanf("%d",&n2);

if(n1<=0 || n2<=0)

printf("Invalid input...!\n");

else

printf("GCD of given numbers=%d\n",gcd(n1,n2));

return 0;

}
int gcd(int n1,int n2)
{
if(n1%n2==0)
return n2; else
return gcd(n2,n1%n2);
}

2// Write a program for multiplication of two matrices

#include

int main(){

int row_size1,col_size1,row_size2,col_size2,i=0,j=0,k=0;

printf("Enter Matrix1 row size:");

scanf("%d",&row_size1); // matrix row size input from user

printf("Enter Matrix1 column size:");

scanf("%d",&col_size1); // matrix column size input from user

int matrix1[row_size1][col_size1];

// user input for matrix1 elements

printf("Enter Matrix1 Elements");

for(i=0;i<row_size1;i++){

for(j=0;j<col_size1;j++){

scanf("%d",&matrix1[i][j]);

printf("Enter Matrix2 row size:");

scanf("%d",&row_size2); // matrix row size input from user

printf("Enter Matrix2 column size:");

scanf("%d",&col_size2);

int matrix2[row_size2][col_size2]; // defining matrix arrays


// user input for matrix2 elements

printf("Enter Matrix2 Elements");

for(i=0;i<row_size2;i++){

for(j=0;j<col_size2;j++){

scanf("%d",&matrix2[i][j]);

//check if multiplication of matrices is possible

if(col_size1!=row_size2){

printf("Multiplication of matrices is not possible if Column size of first matrix doesn't equals row size of second
matrix");

exit(0);

// if multiplication is possible, calculate it and print it

int output[row_size1][col_size2];

for(i=0;i<row_size1;i++){

for(j=0;j<col_size2;j++){

output[i][j]=0;

// multiplication of two matrices

for(i=0;i<row_size1;i++){

for(j=0;j<col_size1;j++){

for(k=0;k<col_size2;k++){

output[i][k] = output[i][k]+matrix1[i][j]*matrix2[j][k];

}
// printing output matrix

printf("Multiplication Matrix:\n");

for(i=0;i<row_size1;i++){

for(j=0;j<col_size2;j++){

printf("%d ",output[i][j]);

printf("\n");

getchar();

getchar();

3 Reverse array : CoCubes coding question


Ques. You are given a function,int* ReverseArray(int* arr, int length);
The function takes an integer array and its length as input. Implement the function to
return the array such that the array is reversed i.e. the first element of the array occupies
the last position, second element occupies the second last position and so on.

Note:
The re-arrangement is to be done in-place i.e you cannot use another array.

Assumption:
You may assume that the array is of even length.

Example:

Input:
2 4 6 8 20 15 10 5

Output:
5 10 15 20 8 6 4 2

*********************************************************************************

Program
*********************************************************************************

#include<stdio.h>
/* Function to reverse arr[] from start to end*/
void rvereseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

/* Utility that prints out an array on a line */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);

printf("\n");
}

/* Driver function to test above functions */


int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
printArray(arr, 6);
rvereseArray(arr, 0, 5);
printf("Reversed array is \n");
printArray(arr, 6);
return 0;
}

4.SECOND LARGEST NO
CODING:
#include<conio.h>
int secLargest(int a , int b,int c);
int main()

{
int a,b,c,sec;

scanf("%d%d%d",&a,&b,&c);
sec=secLargest(a,b,c);
printf("second largest number :%d",sec);
getch();

}
int secLargest(int a , int b,int c)
{
int large=0,seclarge=0;

if(a>b)
{
if(a>c)
large=a;
else
seclarge=a;
}
else
{
if(a>c)
seclarge=a;
}

if(b>c)
{
if(b>a)
large=b;
else
seclarge=b;
}
else
{
if(b>a)
seclarge=b;
}

if(c>a)
{
if(c>b)
large=c;
else
seclarge=c;

}
else
{
if(c>b)
seclarge=c;
}

return seclarge;

6.// Write a program for concatenation two strings

// Ex: AimForJob, Success -> AimForJobSuccess


#include<string.h>

#include<stdio.h>

int main(){

char str1[100],str2[100],output[200];

printf("Enter first string:");

gets(str1);

printf("Enter second string:");

gets(str2);

int i=0,j=0;

for(i=0;i<strlen(str1);i++){

output[j++] = str1[i];

for(i=0;i<strlen(str2);i++){

output[j++] = str2[i];

for(i=0;i<j;i++){

printf("%c",output[i]);
}

getch();

7./ Write a program to find out the sum of cubes of natural numbers

// Ex: 4 -> 100; 2 -> 9

// Author: AimForJob.com

#include<stdio.h>

int main(){

int num;

printf("Enter a number to find sum of cubes upto that number:");

scanf("%d",&num);

while(num<=0){

printf("Please enter only a positive integer: ");

scanf("%d",&num);

// calculating sum of cubes

long long int sum;


sum = (num*num/4.0)*(num+1)*(num+1);

int i=1;

printf("\n");

if(num<=4){

while(i<num){

printf("%d^3+",i);

i++;

printf("%d^3\n",i);

else{

printf("1^3+2^3+...+%d^3+%d^3\n",num-1,num);

printf("\nSum of cubes of natural numbers upto %d is: %lld",num,sum);

getch();

S-ar putea să vă placă și