75. C program to DELETE and MODIFY the records of the student, using structure.
Amit
#include <stdio.h>
#include <string.h>
typedef struct student{
char name[20];
char phNo[10];
int id;
}student;
int main()
{
student s;
int choose;
int choice;
printf("enter the name of the student:\n ");
scanf("%s", s.name);
printf("enter the phone number of student: \n");
scanf("%s", s.phNo);
printf("enter the id of the student: \n");
scanf("%d", &s.id);
printf("press 1 for display the student records\n");
printf("press 2 for delete the student records\n");
printf("press 3 for modify the student records\n");
scanf("%d", &choose);
switch(choose){
case 1:{
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
case 2:{
printf("enter the record to be deleted: \n");
printf("press 1 for delete the name of the student: \n");
printf("press 2 for delete the id of the student: \n");
printf("press 3 for delete the phone number of the student: \n");
printf("enter your choice: ");
scanf("%d", &choice);
if(choice==1){
for(int i=0; i<strlen(s.name); i++){
s.name[i] = NULL;
}
}
else if(choice ==2)
s.id = NULL;
else if(choice ==3){
for(int i=0; i<strlen(s.phNo); i++){
s.phNo[i] = NULL;
}
}
else
printf("invalid choice\n");
}
printf("STRUDENT RECORDS\nAfter deletion\n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
case 3:{
printf("enter the new name: \n");
scanf("%s", s.name);
printf("enter the new id: \n");
scanf("%d", &s.id);
printf("enter the new phone number: \n");
scanf("%s", s.phNo);
printf("NEW RECORDS OF STUDENT ARE: \n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
default:{
printf("please enter a valid choice!\n");
}
}
return 0;
}
Programmed by: @miss_rupali
Amit
#include <stdio.h>
#include <string.h>
typedef struct student{
char name[20];
char phNo[10];
int id;
}student;
int main()
{
student s;
int choose;
int choice;
printf("enter the name of the student:\n ");
scanf("%s", s.name);
printf("enter the phone number of student: \n");
scanf("%s", s.phNo);
printf("enter the id of the student: \n");
scanf("%d", &s.id);
printf("press 1 for display the student records\n");
printf("press 2 for delete the student records\n");
printf("press 3 for modify the student records\n");
scanf("%d", &choose);
switch(choose){
case 1:{
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
case 2:{
printf("enter the record to be deleted: \n");
printf("press 1 for delete the name of the student: \n");
printf("press 2 for delete the id of the student: \n");
printf("press 3 for delete the phone number of the student: \n");
printf("enter your choice: ");
scanf("%d", &choice);
if(choice==1){
for(int i=0; i<strlen(s.name); i++){
s.name[i] = NULL;
}
}
else if(choice ==2)
s.id = NULL;
else if(choice ==3){
for(int i=0; i<strlen(s.phNo); i++){
s.phNo[i] = NULL;
}
}
else
printf("invalid choice\n");
}
printf("STRUDENT RECORDS\nAfter deletion\n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
case 3:{
printf("enter the new name: \n");
scanf("%s", s.name);
printf("enter the new id: \n");
scanf("%d", &s.id);
printf("enter the new phone number: \n");
scanf("%s", s.phNo);
printf("NEW RECORDS OF STUDENT ARE: \n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
default:{
printf("please enter a valid choice!\n");
}
}
return 0;
}
Programmed by: @miss_rupali
76. C program to delete duplicate elements from a sorted array.
#include <stdio.h>
int rem_duplicate_ele(int arr[],int size){
int j =0, i, k;
if(size == 0 || size == 1)
return size;
int temp[size];
for(i = 0; i < size -1; i++)
if(arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[size-1];
for(k =0; k < j; k++)
arr[k]=temp[k];
return j;
}
int main()
{
int arr[] = {1,1,3,3,5,6,6,6,6,};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
n = rem_duplicate_ele(arr, n);
for (int i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int rem_duplicate_ele(int arr[],int size){
int j =0, i, k;
if(size == 0 || size == 1)
return size;
int temp[size];
for(i = 0; i < size -1; i++)
if(arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[size-1];
for(k =0; k < j; k++)
arr[k]=temp[k];
return j;
}
int main()
{
int arr[] = {1,1,3,3,5,6,6,6,6,};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
n = rem_duplicate_ele(arr, n);
for (int i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
Programmed by: @miss_rupali
77. C program to check whether number is positive or negative without using Comparison operators.
#include <stdio.h>
int main()
{
int x;
printf("enter a number: ");
scanf("%d",&x);
if(x>>31)
printf("negative");
else
printf("positive");
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main()
{
int x;
printf("enter a number: ");
scanf("%d",&x);
if(x>>31)
printf("negative");
else
printf("positive");
return 0;
}
Programmed by: @miss_rupali
#patter_series
78(a). C program to print the following pattern.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
#include<stdio.h>
int main()
{
int row=5;
int col=4;
int pr=1;
for(int a=1;a<=row;a++){
for(int b=1;b<=col;b++){
printf("%d ",pr);
pr++;
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
78(a). C program to print the following pattern.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
#include<stdio.h>
int main()
{
int row=5;
int col=4;
int pr=1;
for(int a=1;a<=row;a++){
for(int b=1;b<=col;b++){
printf("%d ",pr);
pr++;
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
#patter_series
78(b). C program to print the following pattern
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
#include<stdio.h>
int main()
{
for(int a=5;a>=1;a--){
for(int b=5;b>=1;b--){
if(b>=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
78(b). C program to print the following pattern
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
#include<stdio.h>
int main()
{
for(int a=5;a>=1;a--){
for(int b=5;b>=1;b--){
if(b>=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
#patter_series
78(c). C program to print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
int main()
{
for(int a=1;a<=5;a++){
for(int b=1;b<=5;b++){
if(b<=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
78(c). C program to print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
int main()
{
for(int a=1;a<=5;a++){
for(int b=1;b<=5;b++){
if(b<=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
79. C program to find sum of even and odd numbers between 0 to n.
#include <stdio.h>
int main() {
int n=100, num2, sum1 = 0,sum2=0;
for(int i = 0; i < n; i++){
if(i % 2 ==0){
sum1= sum1+i;
}
else{
sum2=sum2+i;
}
}
printf("sum even numbers: %d\n sum of odd numbers: %d",sum1,sum2);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main() {
int n=100, num2, sum1 = 0,sum2=0;
for(int i = 0; i < n; i++){
if(i % 2 ==0){
sum1= sum1+i;
}
else{
sum2=sum2+i;
}
}
printf("sum even numbers: %d\n sum of odd numbers: %d",sum1,sum2);
return 0;
}
Programmed by: @miss_rupali
80. C program to find maximum and minimum of a integer array.
#include <stdio.h>
int main()
{
int ar[5]={3,2,6,12,8};
int min,max;
min=max=ar[0];
int i,j;
int *ptr = &ar[0];
for(i=0; i<5; i++){
if(min>ptr[i])
min=ptr[i];
if(max <ptr[i])
max=ptr[i];
}
printf("\nmaximum element of the array is %d\nminimum element of the array is %d\n",max,min);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main()
{
int ar[5]={3,2,6,12,8};
int min,max;
min=max=ar[0];
int i,j;
int *ptr = &ar[0];
for(i=0; i<5; i++){
if(min>ptr[i])
min=ptr[i];
if(max <ptr[i])
max=ptr[i];
}
printf("\nmaximum element of the array is %d\nminimum element of the array is %d\n",max,min);
return 0;
}
Programmed by: @miss_rupali
81. C program to generate n random numbers between 0 to 100 and print lowest and highest number between these random numbers.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void genRandNum(int n)
{
int arr[n];
int randomNum,low,high;
srand(time(NULL));
for(int a=1; a<=n; a++){
randomNum=rand()%100;
printf("%d\n", randomNum);
arr[a]= randomNum;
}
high=low=arr[1];
for(int a=1;a<=n;a++){
if(low>arr[a])
low=arr[a];
if(high<arr[a])
high=arr[a];
}
printf("highest number between these random numbers is :%d\nlowest number between these random numbers is %d",high,low);
}
int main(){
genRandNum(50);
return 0;
}
Programmed by: @miss_rupali
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void genRandNum(int n)
{
int arr[n];
int randomNum,low,high;
srand(time(NULL));
for(int a=1; a<=n; a++){
randomNum=rand()%100;
printf("%d\n", randomNum);
arr[a]= randomNum;
}
high=low=arr[1];
for(int a=1;a<=n;a++){
if(low>arr[a])
low=arr[a];
if(high<arr[a])
high=arr[a];
}
printf("highest number between these random numbers is :%d\nlowest number between these random numbers is %d",high,low);
}
int main(){
genRandNum(50);
return 0;
}
Programmed by: @miss_rupali
👍1
82. C program to convert string into machine language.
#include <stdio.h>
int main() {
printf("***|CONVERTING STRING INTO MACHINE LANGUAGE|***\n");
int i=0;
int arr[10];
int num[100];
char str[100];
int len;
printf("enter a string: ");
scanf("%[^\n]%*c",&str);
len=strlen(str);
printf("Binary\n");
for(i=0;i<len;i++)
num[i]=str[i];
for(int a=0;a<len;a++){
i=0;
while(num[a]>0){
arr[i] = num[a] % 2;
num[a] = num[a] / 2;
i++;
}
for(int j= i-1;j>=0; j--){
printf ("%d",arr[j]);
}
printf(" ");
}
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main() {
printf("***|CONVERTING STRING INTO MACHINE LANGUAGE|***\n");
int i=0;
int arr[10];
int num[100];
char str[100];
int len;
printf("enter a string: ");
scanf("%[^\n]%*c",&str);
len=strlen(str);
printf("Binary\n");
for(i=0;i<len;i++)
num[i]=str[i];
for(int a=0;a<len;a++){
i=0;
while(num[a]>0){
arr[i] = num[a] % 2;
num[a] = num[a] / 2;
i++;
}
for(int j= i-1;j>=0; j--){
printf ("%d",arr[j]);
}
printf(" ");
}
return 0;
}
Programmed by: @miss_rupali
#DSA_Series
83. C program to search an element in array by using Binary Search.
#include <stdio.h>
int binary_search(int arr[], int size, int element);
int main()
{
int arr[] = {1,2,7,12,23,27,28,39,45,67};
int element = 45;
int size = sizeof(arr) / sizeof(int);
int search_ind = binary_search(arr,size ,element);
printf("The element %d was found at index %d\n",element, search_ind);
return 0;
}
int binary_search(int arr[], int size, int element){
int low, mid, high;
low = 0;
high = size-1;
while(low <= high){
mid = (low + high) / 2;
if(arr[mid] == element){
return mid;
}
else if(arr[mid] < element){
low = mid+1;
}
else{
high = mid-1;
}
}
return -1;
}
Programmed by: @miss_rupali
83. C program to search an element in array by using Binary Search.
#include <stdio.h>
int binary_search(int arr[], int size, int element);
int main()
{
int arr[] = {1,2,7,12,23,27,28,39,45,67};
int element = 45;
int size = sizeof(arr) / sizeof(int);
int search_ind = binary_search(arr,size ,element);
printf("The element %d was found at index %d\n",element, search_ind);
return 0;
}
int binary_search(int arr[], int size, int element){
int low, mid, high;
low = 0;
high = size-1;
while(low <= high){
mid = (low + high) / 2;
if(arr[mid] == element){
return mid;
}
else if(arr[mid] < element){
low = mid+1;
}
else{
high = mid-1;
}
}
return -1;
}
Programmed by: @miss_rupali
84. C program to check whether a binary is correct for a single character or not (including lower and upper case)
#include <stdio.h>
#include <math.h>
int check_char(int val){
if((val>= 65 && val <= 90) || (val>= 97 && val<= 122))
return 1;
else
return 0;
}
int main() {
int bin, rem, in=1, dec=0,temp,ret;
printf("enter a number in binary");
scanf("%d",&bin);
temp=bin;
while(bin!=0){
rem = bin % 10;
bin /= 10;
dec += rem*in;
in *= 2;
}
ret = check_char(dec);
if(ret){
printf("you have entered the binary\nits equivalent character is: %c", dec);
}
else
printf("incorrect binary for character");
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
#include <math.h>
int check_char(int val){
if((val>= 65 && val <= 90) || (val>= 97 && val<= 122))
return 1;
else
return 0;
}
int main() {
int bin, rem, in=1, dec=0,temp,ret;
printf("enter a number in binary");
scanf("%d",&bin);
temp=bin;
while(bin!=0){
rem = bin % 10;
bin /= 10;
dec += rem*in;
in *= 2;
}
ret = check_char(dec);
if(ret){
printf("you have entered the binary\nits equivalent character is: %c", dec);
}
else
printf("incorrect binary for character");
return 0;
}
Programmed by: @miss_rupali
85. C program to find Second largest element in an integer array.
#include<stdio.h>
int main()
{
int a[5]={12,43,5,75,1};
int lVal,x,pos=0,Val, pos2;
lVal=a[0];
for(x=0; x<5; x++){
printf(" %d ",a[x]);
}
for(x=0; x<5; x++){
if(lVal<a[x]){
lVal=a[x];
pos=x;
}
}
for(x=pos; x<5; x++){
a[x]=a[x+1];
}
Val=a[0];
x=0;
printf("\n");
for(x=0; x<5-1; x++){
if(Val<a[x]){
Val=a[x];
}
}
printf("\nThe second largest element is: %d\n",Val);
return 0;
}
Programmed by: @miss_rupali
#include<stdio.h>
int main()
{
int a[5]={12,43,5,75,1};
int lVal,x,pos=0,Val, pos2;
lVal=a[0];
for(x=0; x<5; x++){
printf(" %d ",a[x]);
}
for(x=0; x<5; x++){
if(lVal<a[x]){
lVal=a[x];
pos=x;
}
}
for(x=pos; x<5; x++){
a[x]=a[x+1];
}
Val=a[0];
x=0;
printf("\n");
for(x=0; x<5-1; x++){
if(Val<a[x]){
Val=a[x];
}
}
printf("\nThe second largest element is: %d\n",Val);
return 0;
}
Programmed by: @miss_rupali
86. C program to convert decimal digits into Excess 3 code
#include <stdio.h>
int main() {
printf("***EXCESS 3 CODE***\n");
int num ,i=0,n;
int bin[10],ex[10];
scanf("%d",&n);
num = n+3;
printf("number: %d\n",n);
while(n>0){
bin[i] = n % 2;
n = n / 2;
i++;
}
printf("binary: ");
for(int j= i-1;j>=0; j--){
printf ("%d ",bin[j]);
}
printf("\n");
i=0;
while(num>0){
ex[i] = num % 2;
num = num / 2;
i++;
}
printf("excess 3 code: ");
for(int j= i-1;j>=0; j--){
printf ("%d ",ex[j]);
}
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main() {
printf("***EXCESS 3 CODE***\n");
int num ,i=0,n;
int bin[10],ex[10];
scanf("%d",&n);
num = n+3;
printf("number: %d\n",n);
while(n>0){
bin[i] = n % 2;
n = n / 2;
i++;
}
printf("binary: ");
for(int j= i-1;j>=0; j--){
printf ("%d ",bin[j]);
}
printf("\n");
i=0;
while(num>0){
ex[i] = num % 2;
num = num / 2;
i++;
}
printf("excess 3 code: ");
for(int j= i-1;j>=0; j--){
printf ("%d ",ex[j]);
}
return 0;
}
Programmed by: @miss_rupali
#DSA_Series
87. C program to Traverse a BST using recursion.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
struct node *left;
struct node *right;
int data;
}node;
void show(node *root){
//checking if root is empty then print NULL
if(root->left==NULL || root->right==NULL)
printf("NULL\n");
//else print data of its rigth node and left node
else{
printf("%d\n%d %d\n",root->data, root->left->data, root->right->data);
}
}
//creating main function to creat the nodes of a tree
node * creat(int data){
//allocating memory in heap
node *p = (node*)malloc(sizeof(node));
p->data = data;
p->left=NULL;
p->right=NULL;
return p;
}
void preOrder(node *root){
//preorder follows ROOT->LEFT-RIGTH
if(root!=NULL){
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(node *root){
//inorder follows LEFT->ROOT->RIGHT
if(root!=NULL){
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}
void postOrder(node *root){
//postorder folllows LEFT->RIGTH->ROOT
if(root!=NULL){
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}
int main(){
//taking data in node *type pointer
node *n=creat(34);
node *n1=creat(97);
node *n2=creat(7);
node *n3=creat(37);
node *n4=creat(17);
node *n5=creat(67);
node *n6=creat(127);
n->left=n1;
n->right=n2;
n1->left=n3;
n1->right=n4;
n2->left=n5;
n2->right=n6;
printf("Preorder: ");
preOrder(n);
printf("\nInorder: ");
inOrder(n);
printf("\nPsotorder: ");
postOrder(n);
return 0;
}
87. C program to Traverse a BST using recursion.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
struct node *left;
struct node *right;
int data;
}node;
void show(node *root){
//checking if root is empty then print NULL
if(root->left==NULL || root->right==NULL)
printf("NULL\n");
//else print data of its rigth node and left node
else{
printf("%d\n%d %d\n",root->data, root->left->data, root->right->data);
}
}
//creating main function to creat the nodes of a tree
node * creat(int data){
//allocating memory in heap
node *p = (node*)malloc(sizeof(node));
p->data = data;
p->left=NULL;
p->right=NULL;
return p;
}
void preOrder(node *root){
//preorder follows ROOT->LEFT-RIGTH
if(root!=NULL){
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(node *root){
//inorder follows LEFT->ROOT->RIGHT
if(root!=NULL){
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}
void postOrder(node *root){
//postorder folllows LEFT->RIGTH->ROOT
if(root!=NULL){
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}
int main(){
//taking data in node *type pointer
node *n=creat(34);
node *n1=creat(97);
node *n2=creat(7);
node *n3=creat(37);
node *n4=creat(17);
node *n5=creat(67);
node *n6=creat(127);
n->left=n1;
n->right=n2;
n1->left=n3;
n1->right=n4;
n2->left=n5;
n2->right=n6;
printf("Preorder: ");
preOrder(n);
printf("\nInorder: ");
inOrder(n);
printf("\nPsotorder: ");
postOrder(n);
return 0;
}
88. C program to count how many number of Hours, minutes and seconds in given year.
#include<stdio.h>
int main()
{
int h_24, year,ts,yr;
printf("enter for how many year\n");
scanf("%d",&yr);
int h,m;
int h_1 = 3600;//in 1hr = 3,600 seconds
//in 1day 24 hours
h_24 = 24*3600;// in 1hr 3,600 sec
// so in 24hrs 86,400 seconds
year = h_24*365;//in 1yr 365 days
//after this line in 1yr 31536000 sec
ts = yr*year;
//this line will find sec ac to input yr.
h = ts/3600;//this line findes hours
m = ts/60; //this line findes minutes
printf("total hour = %d\n",h);
printf("total minutes = %d\n",m);
printf("total sec = %d\n",ts);
return 0;
}
Programmed by:@Sks_programmer
#include<stdio.h>
int main()
{
int h_24, year,ts,yr;
printf("enter for how many year\n");
scanf("%d",&yr);
int h,m;
int h_1 = 3600;//in 1hr = 3,600 seconds
//in 1day 24 hours
h_24 = 24*3600;// in 1hr 3,600 sec
// so in 24hrs 86,400 seconds
year = h_24*365;//in 1yr 365 days
//after this line in 1yr 31536000 sec
ts = yr*year;
//this line will find sec ac to input yr.
h = ts/3600;//this line findes hours
m = ts/60; //this line findes minutes
printf("total hour = %d\n",h);
printf("total minutes = %d\n",m);
printf("total sec = %d\n",ts);
return 0;
}
Programmed by:@Sks_programmer
89. C program to convert string into substring.
#include <stdio.h>
int main(){
char str[100];
char temp[80];
int start, end,x=0;
printf("Enter the string: ");
scanf("%[^\n]%*c", str);
printf("Enter the number of index for start and end of sub-string: ");
scanf("%d %d", &start, &end);
for(int i=start; i <= end; i++){
temp[x] = str[i];
x++;
}
for(int i=0; i<x; i++)
printf("%c", temp[i]);
return 0;
}
#include <stdio.h>
int main(){
char str[100];
char temp[80];
int start, end,x=0;
printf("Enter the string: ");
scanf("%[^\n]%*c", str);
printf("Enter the number of index for start and end of sub-string: ");
scanf("%d %d", &start, &end);
for(int i=start; i <= end; i++){
temp[x] = str[i];
x++;
}
for(int i=0; i<x; i++)
printf("%c", temp[i]);
return 0;
}
90. C program to find the difference between smallest and largest element of a given array.
#include <stdio.h>
int findDiff(int n, int arr[]){
int smallest = arr[0];
int largest = arr[0];
int diff=0;
for(int i=0; i<n; i++){
if(smallest > arr[i+1]){
smallest = arr[i+1];
}
}
for(int i=1; i<n; i++){
if(largest < arr[i]){
largest = arr[i];
}
}
diff = largest - smallest;
return diff;
}
int main()
{
int n;
printf("enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("enter values: \n");
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
int diff = findDiff(n, arr);
printf("Difference: %d", diff);
return 0;
}
#include <stdio.h>
int findDiff(int n, int arr[]){
int smallest = arr[0];
int largest = arr[0];
int diff=0;
for(int i=0; i<n; i++){
if(smallest > arr[i+1]){
smallest = arr[i+1];
}
}
for(int i=1; i<n; i++){
if(largest < arr[i]){
largest = arr[i];
}
}
diff = largest - smallest;
return diff;
}
int main()
{
int n;
printf("enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("enter values: \n");
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
int diff = findDiff(n, arr);
printf("Difference: %d", diff);
return 0;
}
91. C program to find Median of a given integer array.
#include<stdio.h>
int main()
{
int n;
int fst, lst, med, isSorted, temp;
printf("enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("enter the values: ");
for(int a=0; a<n; a++){
scanf("%d",&arr[a]);
}
for(int i=0; i<n-1; i++){
isSorted = 1;
for(int j=0; j<= n-i-1; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
isSorted = 0;
}
}
}
if(isSorted){
printf("After sorting the array: ");
for(int i=0; i<n; i++){
printf("%d ", arr[i]);
}
}
printf("\n");
fst=0;
lst= n-1;
med=(fst+lst)/2;
if(n%2)
printf("median of the array is : %d",arr[med]);
else
printf("median of the array is : %lf ",(arr[med]+arr[med+1])/2.0);
return 0;
}
#include<stdio.h>
int main()
{
int n;
int fst, lst, med, isSorted, temp;
printf("enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("enter the values: ");
for(int a=0; a<n; a++){
scanf("%d",&arr[a]);
}
for(int i=0; i<n-1; i++){
isSorted = 1;
for(int j=0; j<= n-i-1; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
isSorted = 0;
}
}
}
if(isSorted){
printf("After sorting the array: ");
for(int i=0; i<n; i++){
printf("%d ", arr[i]);
}
}
printf("\n");
fst=0;
lst= n-1;
med=(fst+lst)/2;
if(n%2)
printf("median of the array is : %d",arr[med]);
else
printf("median of the array is : %lf ",(arr[med]+arr[med+1])/2.0);
return 0;
}
#c_&_os
92. C program to implement FCFS algorithm in OS
#include <stdio.h>
int main()
{
int n,i;
printf("enter number of process..?\n");
scanf("%d", &n);
int process[n],arrival_time[n],burst_time[n],completion_time[n],tat[n],wt[n],rt[n];
printf("enter process...\n");
for(i=0; i<n; i++){
scanf("%d", &process[i]);
}
printf("enter arrival time..\n");
for(i=0; i<n; i++){
printf("enter arrival time for prcess: p%d\n",i+1);
scanf("%d",&arrival_time[i]);
}
printf("enter burst time..\n");
for(i=0; i<n; i++){
printf("enter burst time for prcess: p%d\n",i+1);
scanf("%d",&burst_time[i]);
}
//process starts here...
printf("\ncompletion time...\n");
for(i=0; i<n; i++){
completion_time[0] = burst_time[0];
completion_time[i+1] = completion_time[i] + burst_time[i+1];
printf("completion time of process p%d is %d\n",i+1,completion_time[i]);
}
//turn around time(tat)...
printf("\nTAT...\n");
for(i=0; i<n; i++){
tat[i] = completion_time[i] - arrival_time[i];
printf("turn around time time of process p%d is %d\n",i+1,tat[i]);
}
//waiting time(wt)...
printf("\nwaiting time...\n");
for(i=0; i<n; i++){
wt[i] = tat[i] - burst_time[i];
printf("wating time of process p%d is %d\n",i+1,wt[i]);
}
return 0;
}
92. C program to implement FCFS algorithm in OS
#include <stdio.h>
int main()
{
int n,i;
printf("enter number of process..?\n");
scanf("%d", &n);
int process[n],arrival_time[n],burst_time[n],completion_time[n],tat[n],wt[n],rt[n];
printf("enter process...\n");
for(i=0; i<n; i++){
scanf("%d", &process[i]);
}
printf("enter arrival time..\n");
for(i=0; i<n; i++){
printf("enter arrival time for prcess: p%d\n",i+1);
scanf("%d",&arrival_time[i]);
}
printf("enter burst time..\n");
for(i=0; i<n; i++){
printf("enter burst time for prcess: p%d\n",i+1);
scanf("%d",&burst_time[i]);
}
//process starts here...
printf("\ncompletion time...\n");
for(i=0; i<n; i++){
completion_time[0] = burst_time[0];
completion_time[i+1] = completion_time[i] + burst_time[i+1];
printf("completion time of process p%d is %d\n",i+1,completion_time[i]);
}
//turn around time(tat)...
printf("\nTAT...\n");
for(i=0; i<n; i++){
tat[i] = completion_time[i] - arrival_time[i];
printf("turn around time time of process p%d is %d\n",i+1,tat[i]);
}
//waiting time(wt)...
printf("\nwaiting time...\n");
for(i=0; i<n; i++){
wt[i] = tat[i] - burst_time[i];
printf("wating time of process p%d is %d\n",i+1,wt[i]);
}
return 0;
}