Assignment 7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Assignment 7: Function

Vishw Vekariya
F-39
U21CS104

Q-1) CODE:
#include<stdio.h>
int power(int x,int y){
if(y==0)
return 1;
else
return x*power(x,y-1);
}

int main(){
int a,b;
printf("Enter a number:");
scanf("%d",&a);
printf("Enter the power:");
scanf("%d",&b);

printf("%d^%d= %d",a,b,power(a,b));
return 0;
}

OUTPUT:
Q-2) CODE:
#include<stdio.h>

int mdsum(int y,int x[y][y]){


int i,sum=0;
for(i=0;i<y;i++){
sum+=x[i][i];
}
return sum;
}

void main(){
int n,k,j;
printf("Enter the value of n:");
scanf("%d",&n);
int a[n][n];
printf(" Enter the %d*%d Matrix :\n",n,n);
for(j=0;j<n;j++){
for(k=0;k<n;k++){
printf(" a[%d][%d]= ",j,k);
scanf("%d",&a[j][k]);
}
}
printf("The Sum of Diagonal elements of Matrix are:%d",mdsum(n,a));
}

OUTPUT:

Q-3) CODE:
#include<stdio.h>
#include<string.h>
void func(char b[],char a[],int p){
int i=0;
char c[300];
int n1=strlen(b);
int n2=strlen(a);

for(i=0;i<p;i++){
c[i]=b[i];
}

for(i=p;i<p+n2;i++){
c[i]=a[i-p];
}

for(i=p+n2;i<n1+n2;i++){
c[i]=b[i-n2];
}
puts(c);
}

void main(){
int p;
char a[100],b[200];
printf("Enter the String:\n");
gets(b);
printf("Enter the Word you want to insert:\n");
gets(a);
printf("Enter the position where you want to insert the word:\n");
scanf("%d",&p);

func(b,a,p);
}

OUTPUT:
Q-4) CODE:
#include<stdio.h>
#include<math.h>
int flip(int number, int n)
{
int rev = 0;
int i;
for ( i = 0; i != n; ++i) {
rev *= 10;
rev += number % 10;
number /= 10;
}
for ( i = 0; i != n; ++i) {
number *= 10;
}
return number + rev;
}

int main(){
int n,p;
printf("Enter the number:\n");
scanf("%d", &n);
printf("Enter the number of digits you have to flip from last:\n");
scanf("%d", &p);
printf("Resultant Number:");
printf("%d",flip(n,p));
return 0;
}

OUTPUT:

Q-5) CODE:
#include<stdio.h>

int fact(int n){


if(n==0){
return 1;
}
else{
return n*fact(n-1);
}
}
int sol(int n){
int sum=0;
int i;
for(i=1;i<=n;i++){
sum+=((fact(i))/i);
}
return sum;
}
void main(){
int n;
printf("Enter the value of n:");
scanf("%d",&n);
printf("The Required solution is %d",sol(n));
}

OUTPUT:

Q-6) CODE:
#include<stdio.h>
#include<conio.h>

int *func(int arr[],int p){


int result[2],i;
result[0]=arr[0];
result[1]=arr[0];
for(i=0;i<p;i++){
if(result[0]>arr[i]){
result[0]=arr[i];
}
if(result[1]<arr[i]){
result[1]=arr[i];
}
}
return result;
}

int main(){
int n,i;
printf("Enter the size of array you want to give:");
scanf("%d",&n);
int arr[n];
printf("Enter the array:\n");
for(i=0;i<n;i++){
printf("arr[%d]=",i);
scanf("%d",&arr[i]);
}
int *k;
k=func(arr,n);
printf("\nMinimum Number in Array is %d.\n",*k);
printf("Maximum Number in Array is %d.",*(k+2));
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy