0% found this document useful (0 votes)
103 views

C Prog

This document contains code snippets for several programs: 1) A program to calculate the day of the week for a given date. It takes the date, month, and year as input and outputs the weekday. 2) Programs to calculate the area of different types of triangles - scalene, right angled, and equilateral. They take the required parameters as input and output the area. 3) A program to calculate the electricity bill amount based on the customer number and power consumed. 4) Programs to convert Celsius to Fahrenheit and solve a quadratic equation for roots in the second quadrant.

Uploaded by

aishwarya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

C Prog

This document contains code snippets for several programs: 1) A program to calculate the day of the week for a given date. It takes the date, month, and year as input and outputs the weekday. 2) Programs to calculate the area of different types of triangles - scalene, right angled, and equilateral. They take the required parameters as input and output the area. 3) A program to calculate the electricity bill amount based on the customer number and power consumed. 4) Programs to convert Celsius to Fahrenheit and solve a quadratic equation for roots in the second quadrant.

Uploaded by

aishwarya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Date & weekday : default:

#include<stdio.h> printf("Incorrect data");


#include<conio.h> }
#include<math.h> return 0;
}
int fm(int date, int month, int year) { //------------------------------------------
int fmonth , leap; int main() {
int date, month, year;
//leap function 1 for leap & 0 for non-leap
if ((year % 100 == 0) && (year % 400 != 0)) printf("\nEnter the year ");
leap = 0; scanf("%d", &year);
else if (year % 4 == 0)
leap = 1; printf("\nEnter the month ");
else scanf("%d", &month);
leap = 0;
printf("\nEnter the date ");
fmonth = 3 + (2 - leap) * ((month + 2) / (2 * month)) scanf("%d", &date);
+ (5 * month + month / 9) / 2;
day_of_week(date, month, year);
//bring it in range of 0 to 6
fmonth = fmonth % 7; return 0;
}
return fmonth;
}
Output :
//----------------------------------------------
int day_of_week(int date, int month, int year) { Enter the year 2012
Enter the month 02
int dayOfWeek; Enter the date 29
int YY = year % 100; Date: 29/2/2012
int century = year / 100; w eekday = Wednesday

printf("\nDate: %d/%d/%d \n", date, month, year);

dayOfWeek = 1.25 * YY + fm(date, month, year) + date - 2 *


(century % 4);

//remainder on division by 7
dayOfWeek = dayOfWeek % 7;

switch (dayOfWeek) {
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
Area of scalen triangle : Area of right angle triangle :
#include<stdio.h> #include<stdio.h>
#include<math.h>
int main() {
int main() { int base, height;
int s1, s2, angle; float area;
float area;
printf("\nEnter the base of Right Angle Triangle : ");
printf("\nEnter Side1 : "); scanf("%d", &base);
scanf("%d", &s1);
printf("\nEnter the height of Right Angle Triangle : ");
printf("\nEnter Side2 : "); scanf("%d", &height);
scanf("%d", &s2);
area = 0.5 * base * height;
printf("\nEnter included angle : "); printf("\nArea of Right Angle Triangle : %f", area);
scanf("%d", &angle);
return (0);
area = (s1 * s2 * sin((M_PI / 180) * angle)) / 2; }

printf("\nArea of Scalene Triangle : %f", area); Output :


return (0);
Enter the base of Right Angle Tr
}
Enter the height of Right Angle T
Area of Right Angle Triangle : 1
Output :

Enter Side1 : 3
Enter Side2 : 4
reads the customer number and power consumed
Enter included angle : 30
Area of Scalene Triangle : 3.000
and prints the amount to be paid by the customer.

1
Area of equi triangle :
2 Consumption Rate of Units Charge
#include<stdio.h>
#include<math.h>
3 0-200 Rs.0.50 per unit
int main() {
int side;
float area, r_4; 4 201-400 Rs.100 plus Rs.0.65 per unit excess 200

r_4 = sqrt(3) / 4; 5 401-600 Rs.230 plus Rs.0.80 per unit excess of 400.

printf("\nEnter the Length of Side : "); 6


scanf("%d", &side);
7
area = r_4 * side * side;

printf("\nArea of Equilateral Triangle : %f", area); #include<stdio.h>


return (0);
} #include<conio.h>

void main() {
Output :
int cust_no, powerUsage;
Enter the Length of Side : 5
Area of Equilateral Triangle : 10 float amount;

clrscr();

printf("Enter the customer number: ");


scanf("%d", &cust_no); Enter value for a,b & c : 15 17 2
c is greatest
printf("Enter the power consumed: ");

scanf("%d", &powerUsage);

if (powerUsage >= 0 && powerUsage <= 200)

amount = powerUsage * 0.50;


Celsius to faren :
#include<stdio.h>
else if (powerUsage > 200 && powerUsage < 400)
int main() {
amount = 100 + ((powerUsage - 200) * 0.65); float celsius, fahrenheit;

else if (powerUsage > 400 && powerUsage <= 600) printf("\nEnter temp in Celsius : ");
scanf("%f", &celsius);
amount = 230 + ((powerUsage - 400) * 0.80);
fahrenheit = (1.8 * celsius) + 32;
printf("Amount to be paid by customer no. %d is printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
Rs.:%5.2f.", cust_no, amount);
return (0);
}
getch();

} Output :

Enter temp in Celsius : 32


Temperature in Fahrenheit :
OUTPUT
89.59998
Enter the customer number: 1
Enter the pow er consumed: 100
Amount to be paid by customer
no. 1 is Rs.:50.00.
2nd quad eqn solve :

#include<stdio.h>
GREATEST OF 3 NO #include<math.h>

#include<stdio.h> int main() {


int main() { float a, b, c;
int a, b, c; float desc, root1, root2;

printf("\nEnter value of a, b & c : "); printf("\nEnter the Values of a : ");


scanf("%d %d %d", &a, &b, &c); scanf("%f", &a);
printf("\nEnter the Values of b : ");
if ((a > b) && (a > c)) scanf("%f", &b);
printf("\na is greatest"); printf("\nEnter the Values of c : ");
scanf("%f", &c);
if ((b > c) && (b > a))
printf("\nb is greatest"); desc = sqrt(b * b - 4 * a * c);

if ((c > a) && (c > b)) root1 = (-b + desc) / (2.0 * a);
printf("\nc is greatest"); root2 = (-b - desc) / (2.0 * a);

return(0); printf("\nFirst Root : %f", root1);


} printf("\nSecond Root : %f", root2);

return (0); }
Output :
} rev = rev * 10 + rem;
num = num / 10;
}
Output :
printf("\nReversed Number : %d", rev);
Enter the Values of a : 1 return (0);
Enter the Values of a : -5 }

Enter the Values of a : 6


Output :
First Root : 3.000000
Second Root : 2.000000 Enter any no to be reversed : 12
Reversed Number : 321
FACTORIAL USING RECURSION :

#include<stdio.h>
#include<conio.h>

int fact(int); FIBONACCI SERIES :

int main() { #include<stdio.h>


int factorial, num;
int main() {
printf("Enter the value of num :"); int first, second, sum, num, counter = 0;
scanf("%d", &num);
printf("Enter the term : ");
factorial = fact(num); scanf("%d", &num);
printf("Factorial is %d", factorial);
printf("\nEnter First Number : ");
return (0); scanf("%d", &first);
}
printf("\nEnter Second Number : ");
int fact(int n) { scanf("%d", &second);
if (n == 0) {
return (1); printf("\nFibonacci Series : %d %d ", first, second);
}
return (n * fact(n - 1)); while (counter < num) {
} sum = first + second;
printf("%d ", sum);
first = second;
OUTPUT : second = sum;
counter++;
Enter the value of num : 5 }

Factorial is 120 return (0);


}

Output :
Reverse of given no :
Enter the term : 5
#include<stdio.h>
Enter First Number : 1
int main() {
int num, rem, rev = 0; Enter Second Number : 30
printf("\nEnter any no to be reversed : ");
Fibonacci Series : 1 3 4 7 11 18 29
scanf("%d", &num);

while (num >= 1) {


rem = num % 10;
5 is a prime number

ARMSTRONG NO : Palindrome :

#include<stdio.h> #include<stdio.h>
#include<string.h>
int main() {
int num, temp, sum = 0, rem; int main() {
int num, i, count = 0;
printf("\nEnter number for checking Armstrong : "); char str1[10], str2[10];
scanf("%d", &num);
printf("nEnter a number:");
temp = num; scanf("%d", &num);

while (num != 0) { //Convert Number to String


rem = num % 10; sprintf(str1, "%d", num);
sum = sum + (rem * rem * rem);
num = num / 10; //Copy String into Other
} strcpy(str2, str1);

if (temp == sum) //Reverse 2nd Number


printf("%d is Amstrong Number", temp); strrev(str2);
else
printf("%d is Amstrong Number", temp); count = strcmp(str1, str2);

return (0); if (count == 0)


} printf("%d is a pallindrome number", num);
else
printf("%d is not a pallindrome number", num);
Output :
1 Enter Number For Checking Armstrong : 153 return 0;
2 153 is Amstrong Number }

PRIME NO : OUTPUT :

#include<stdio.h> Enter a number : 121

int main() { 121 is a pallindrome number

int num, i, count = 0; BINARY TO DECIMAL


printf("Enter a number:");
scanf("%d", &num); #include<stdio.h>
for (i = 2; i <= num / 2; i++) { #include<conio.h>
if (num % i == 0) { #include<math.h>
count++;
break; void bin_dec(long int num) // Function Definition
} {
} Long int rem,sum=0,power=0;
while(num>0)
if (count == 0) {
printf("%d is a prime number", num); rem = num%10;
else num = num/10;
printf("%d is not a prime number", num); sum = sum + rem * pow(2,power);
power++;
return 0; }
}
Output : printf("Decimal number : %d",sum);
}
Enter a number: 5
void main()
{ Enter the decimal number : 7
long int num; Octal number : 111
clrscr();

printf("Enter the Binary number (0 and 1): ");


scanf("%ld",&num);

bin_dec(num);
Decimal to hexadecimal :
getch();
} #include<stdio.h>
#include<conio.h>
#include<math.h>
Output :
void dec_hex(long int num) // Function Definition
{
Enter the Binary number : 111 long int rem[50],i=0,length=0;
Decimal number : 7 while(num>0)
{
rem[i]=num%16;
num=num/16;
i++;
length++;
Decimal to binary : }

#include<stdio.h> printf("Hexadecimal number : ");


#include<conio.h> for(i=length-1;i>=0;i--)
#include<math.h> {
switch(rem[i])
void dec_bin(long int num) // Function Definition {
{ case 10:
long int rem[50],i=0,length=0; printf("A");
while(num>0) break;
{ case 11:
rem[i]=num%2; printf("B");
num=num/2; break;
i++; case 12:
length++; printf("C");
} break;
printf("nBinary number : "); case 13:
for(i=length-1;i>=0;i--) printf("D");
printf("%ld",rem[i]); break;
} case 14:
void main() printf("E");
{ break;
long int num; case 15:
clrscr(); printf("F");
break;
printf("Enter the decimal number : "); default :
scanf("%ld",&num); printf("%ld",rem[i]);
}
dec_bin(num); // Calling function }
}
getch(); void main()
} {
long int num;
clrscr();
Output :
printf("Enter the decimal number : ");
scanf("%ld",&num);

dec_hex(num); // Calling function


10 if (fp1 == NULL) {
getch(); 11 puts("cannot open this file");
} 12 exit(1);
Output : 13 }
14
1 Enter the decimal number : 87274 15 fp2 = fopen("test1.txt", "w");
2 Hexadecimal number : 154EA 16 if (fp2 == NULL) {
17 puts("Not able to open this file");
Decimal to octal :
18 fclose(fp1);
19 exit(1);
#include<stdio.h> 20 }
#include<conio.h> 21
#include<math.h> 22 do {
23 a = fgetc(fp1);
void dec_oct(long int num) // Function Definition 24 fputc(a, fp2);
{ 25 } while (a != EOF);
long int rem[50],i=0,length=0; 26
while(num>0) 27 fcloseall();
{ 28 getch();
rem[i]=num%8; 29 }
num=num/8;
i++;
length++; Output :
}
printf("nOctal number : ");
1 Content will be written successfully to file
for(i=length-1;i>=0;i--)
printf("%ld",rem[i]);
}
read last n chatacters of the file using appropriate file
void main() function
{
#include<stdio.h>
long int num;
clrscr();
int main() {
printf("Enter the decimal number : ");
FILE *fp;
scanf("%ld",&num);
char ch;
int num;
dec_oct(num); // Calling function
long length;
getch();
printf("Enter the value of num : ");
}
scanf("%d", &num);

Output : fp = fopen("test.txt", "r");


if (fp == NULL) {
puts("cannot open this file");
1 Enter the decimal number : 20 exit(1);
2 Octal number : 24 }

fseek(fp, 0, SEEK_END);
Program to copy the contents of one file into another length = ftell(fp);
using fgetc and fputc function fseek(fp, (length - num), SEEK_SET);

do {
1 #include<stdio.h> ch = fgetc(fp);
2 #include<process.h> putchar(ch);
3 } while (ch != EOF);
4 void main() {
5 FILE *fp1, *fp2; fclose(fp);
6 char a; return(0);
7 clrscr(); }
8
9 fp1 = fopen("test.txt", "r");
Output : gets(fname2);

fp1 = fopen(fname1, "r");


Enter the value of n : 4
.com fp2 = fopen(fname2, "r");

if (fp1 == NULL) {

printf("Cannot open %s for reading ", fname1);

exit(1);
Convert the file contents in Upper-case & Write
Contents in a Output file } else if (fp2 == NULL) {

#include<stdio.h> printf("Cannot open %s for reading ", fname2);


#include<process.h>
exit(1);
void main() { } else {
FILE *fp1, *fp2;
char a; ch1 = getc(fp1);
clrscr();
ch2 = getc(fp2);
fp1 = fopen("test.txt", "r");
if (fp1 == NULL) { while ((ch1 != EOF) && (ch2 != EOF) && (ch1 ==
puts("cannot open this file"); ch2)) {
exit(1);
} ch1 = getc(fp1);

fp2 = fopen("test1.txt", "w"); ch2 = getc(fp2);


if (fp2 == NULL) {
}
puts("Not able to open this file");
fclose(fp1); if (ch1 == ch2)
exit(1);
} printf("Files are identical n");
do { else if (ch1 != ch2)
a = fgetc(fp1);
a = toupper(a); printf("Files are Not identical n");
fputc(a, fp2);
} while (a != EOF); fclose(fp1);

fcloseall(); fclose(fp2);
getch();
} }

return (0);
Compare two files :
}
#include<stdio.h>
Copy Text From One File to Other File
int main() {

FILE *fp1, *fp2; #include<stdio.h>


#include<conio.h>
int ch1, ch2; #include<stdlib.h>

char fname1[40], fname2[40]; void main() {


FILE *fp1, *fp2;
printf("Enter name of first file :"); char ch;
clrscr();
gets(fname1);
fp1 = fopen("Sample.txt", "r");
printf("Enter name of second file:"); fp2 = fopen("Output.txt", "w");
while (1) {
ch = fgetc(fp1); Output :
Roll : 10
if (ch == EOF)
Name : SMJC
break;
Percent : 80
else
putc(ch, fp2);
}
printf("File copied Successfully!");
fclose(fp1);
fclose(fp2);
}
#include<stdio.h>

struct Student {
SUM OF NO FROM 1 TO N
int roll;
#include<stdio.h>
char name[12];
int calculateSum(int);
int percent;
int main() {
} s1 = { 10, "SMJC", 80 }; int i, num;
int result;
int main() {
printf("Input a number : ");
scanf("%d", &num);
FILE *fp;
result = calculateSum(num);
struct Student s2; printf("\nSum of Number From 1 to %d : %d", num, result);

//Write details of s1 to file return (0);


}
fp = fopen("ip.txt", "w");
int calculateSum(int num) {
fwrite(&s1, sizeof(s1), 1, fp); int res;
if (num == 1) {
fclose(fp); return (1);
} else {
res = num + calculateSum(num - 1);
fp = fopen("ip.txt", "r"); }
return (res);
fread(&s2, sizeof(s2), 1, fp); }

fclose(fp);
Output :
printf("\nRoll : %d", s2.roll);
Input a number : 5
printf("\nName : %s", s2.name);
Sum of Number From 1 to 5 :15
printf("\nPercent : %d", s2.percent); SUM OF DIGITS USING RECURSION

return (0); #include<stdio.h>


int calsum(int num) {
} int rem, sum;
if (num != 0) {
rem = num % 10;
sum = sum + rem;
calsum(num / 10);
}
return sum;
} Sizeof STRUCTURE:
int main() {
int num, val; #include<stdio.h>
printf("\nEnter a number: ");
scanf("%d", &num); struct stud {
val = calsum(num);
printf("\nSum of the digits of %d is : %d", num, val); int roll;
return 0;
}
char name[10];

Output : int marks;

Enter a number: 123 };


Sum of the digits of 123 is : 6
int main() {

int size;

struct stud s;
TOWER OF HANOI

#include<stdio.h> size = sizeof(s);

void TOH(int num, char x, char y, char z); printf("nSize of Structure : %d", size);

int main() { return(0);


int num;
printf("\nEnter number of plates:"); }
scanf("%d", &num);
POINTER TO STRUCTURE
TOH(num - 1, 'A', 'B', 'C');
return (0);
#include<stdio.h>
}

void TOH(int num, char x, char y, char z) { typedef struct XYZ {


if (num > 0) {
TOH(num - 1, x, z, y); int far *fptr;
printf("\n%c -> %c", x, y);
TOH(num - 1, z, y, x); double dvar;
}
} unsigned char ch;

} xyz;

int main() {

xyz *ptr = (XYZ *) 1000;

printf("Size of Pointer to structure : %d", sizeof(ptr));

return (0);

OUTPUT : Size of Pointer to structure : 2

Size of Float Pointer

#include<stdio.h>
int main() { To display the highest literate rate and the highest
income of a state using array of structures
float a = 3.14, *fptr;
#include<stdio.h>
fptr = &a;
#define M 50
printf("Size of Float Pointer : %d", sizeof(fptr));
struct state {
return (0);
char name[50];
}
long int population;
Output:
float literacyRate;
Size of Float Pointer : 2
float income;
integer pointer
} st[M]; /* array of structure */
#include<stdio.h>

int main() {
int main() {
int a = 10, *ptr;
int i, n, ml, mi, maximumLiteracyRate, maximumIncome;
ptr = &a;
float rate;
printf("Size of int pointer : %d", sizeof(ptr));
ml = mi = -1;
return (0);
maximumLiteracyRate = maximumIncome = 0;
}
printf("Enter how many states:");
Output :
scanf("%d", &n);
Size of int pointer : 2
for (i = 0; i < n; i++) {
character pointer
printf("\nEnter state %d details :", i);
#include<stdio.h>
printf("\nEnter state name : ");
int main() {
scanf("%s", &st[i].name);
char a = 'a', *cptr;
printf("\nEnter total population : ");
cptr = &a;
scanf("%ld", &st[i].population);
printf("\nSize of Char Pointer : %d", sizeof(cptr));
printf("\nEnter total literary rate : ");
return (0);
scanf("%f", &rate);
}
st[i].literacyRate = rate;
Output:

Size of Char Pointer : 2


printf("\nEnter total income : ");
scanf("%f", &st[i].income); Enter state 1 details :

} Enter state name : Goa

Enter total population : 300

for (i = 0; i < n; i++) { Enter total literary rate : 94

if (st[i].literacyRate >= maximumLiteracyRate) { Enter total income : 5000

maximumLiteracyRate = st[i].literacyRate;

ml++; Enter state 2 details :

} Enter state name : Gujrat

if (st[i].income > maximumIncome) { Enter total population : 900

maximumIncome = st[i].income; Enter total literary rate : 78

mi++; Enter total income : 7000

} The state whose literary rate is the highest : Goa

} The state whose income is the highest : Maharashtra

printf("\nState with highest literary rate :%s", Write a program to use structure within union , display
st[ml].name); the contents of structure elements

printf("\nState with highest income :%s", st[mi].name); #include<stdio.h>

#include<conio.h>

return (0); void main() {

} struct student {

Output : char name[30];

Enter how many states:3 char sex;

int rollno;

Enter state 0 details : float percentage;

Enter state name : Maharashtra };

Enter total population : 1000 union details {

Enter total literary rate : 90 struct student st;

Enter total income : 10000 };

union details set;


printf("Enter details:"); Count Number of Words using Pointer

printf("\nEnter name : "); #include<stdio.h>

scanf("%s", set.st.name); #include<stdlib.h>

printf("\nEnter roll no : ");


#include<ctype.h>
scanf("%d", &set.st.rollno);
#include<conio.h>
flushall();
/*low implies that position of pointer is within a word*/
printf("\nEnter sex : ");
#define low 1
scanf("%c", &set.st.sex);
/*high implies that position of pointer is out of word.*/
printf("\nEnter percentage :");
#define high 0
scanf("%f", &set.st.percentage);
void main() {
printf("\nThe student details are : \n");
int nob, now, nod, nov, nos, pos = high;
printf("\name : %s", set.st.name);

printf("\nRollno : %d", set.st.rollno); char *str;

printf("\nSex : %c", set.st.sex); nob = now = nod = nov = nos = 0;

printf("\nPercentage : %f", set.st.percentage); clrscr();

getch(); printf("Enter any string : ");

} gets(str);

Output : while (*str != '\0') {

Enter details:
if (*str == ' ') {
Enter name : Pritesh
// counting number of blank spaces.
Enter rollno: 10
pos = high;
Enter sex: M
++nob;
Enter percentage: 89
} else if (pos == high) {

// counting number of words.


The student details are:
pos = low;
Name : Pritesh

++now;
Rollno : 10

Sex : M }

Percentage : 89.000000 if (isdigit(*str)) /* counting number of digits. */


++nod; OUTPUT
Enter any string : pritesh a taral from c4learn.com
if (isalpha(*str)) /* counting number of vowels */
Number of words 5
Number of spaces 4
switch (*str) { Number of vowels 9
Number of digits 1
case 'a': Number of special characters 5

case 'e': #include<stdio.h>

case 'i': #include<string.h>

#include<stdlib.h>
case 'o':
int main() {
case 'u':
char *str[5], *temp;
case 'A':
int i, j, n;
case 'E':
printf("\nHow many names do you want to have?");
case 'I':
scanf("%d", &n);
case 'O':
for (i = 0; i < n; i++) {
case 'U':
printf("\nEnter the name %d: ", i);
++nov;
flushall();

break; gets(str[i]);

} }

/* counting number of special characters */ for (i = 0; i < n; i++) {

if (!isdigit(*str) && !isalpha(*str)) for (j = 0; j < n - 1; j++) {

++nos; if (strcmp(str[j], str[j + 1]) > 0) {

strcpy(temp, str[j]);
str++;

strcpy(str[j], str[j + 1]);


}
strcpy(str[j + 1], temp);
printf("\nNumber of words %d", now);
}
printf("\nNumber of spaces %d", nob);
}
printf("\nNumber of vowels %d", nov);
}
printf("\nNumber of digits %d", nod);
flushall();
printf("\nSorted List : "); getch();

for (i = 0; i < n; i++) int string_ln(char*p) /* p=&str[0] */

puts(str[i]); int count = 0;

while (*p != '\0') {

return (0); count++;

} p++;

}
Output :
return count;

1
How many names do you want to have? 4
2 Output :
Enter the name 0: pri
3 1 Enter the String : pritesh
Enter the name 1: prt
4 2 Length of the given string pritesh is : 7
Enter the name 2: prq
5
Enter the name 3: pra
6
Sorted List : pra pri prq prt
7

Length of the String using Pointer

#include<stdio.h>

#include<conio.h>

int string_ln(char*);

void main() {

char str[20];

int length;

clrscr();

printf("\nEnter any string : ");

gets(str);

length = string_ln(str);

printf("The length of the given string %s is : %d", str, length);


to Reverse Letter in Each Word of the Entered String void del(char str[], char ch) {
int i, j = 0;
#include<stdio.h> int size;
#include<conio.h> char ch1;
#include<string.h> char str1[10];

void main() { size = strlen(str);


char msg[] = "Welcome to Programming World";
char str[10]; for (i = 0; i < size; i++) {
if (str[i] != ch) {
int i = 0, j = 0; ch1 = str[i];
clrscr(); str1[j] = ch1;
j++;
while (msg[i] != '\0') { }
if (msg[i] != ' ') { }
str[j] = msg[i]; str1[j] = '\0';
j++;
} else { printf("\ncorrected string is : %s", str1);
str[j] = '\0'; }
printf("%s", strrev(str));
printf(" "); Output :
j = 0;
}
i++; Enter the string : abhiman
1
} Enter character which you want to delete : a
2
Corrected string is : bhimn
3
str[j] = '\0';
printf("%s", strrev(str));

getch();
} to Concat Two Strings without Using Library Function

Output : #include<stdio.h>

#include<string.h>
1 emocleW ot gnimmargorP dlroW
void concat(char[], char[]);
Program : Delete all occurrences of Character from the
String int main() {

#include<stdio.h> char s1[50], s2[30];


#include<conio.h>
#include<string.h> printf("\nEnter String 1 :");

void del(char str[], char ch); gets(s1);

void main() { printf("\nEnter String 2 :");


char str[10];
char ch; gets(s2);
printf("\nEnter the string : ");
gets(str); concat(s1, s2);

printf("\nEnter character which you want to delete : "); printf("nConcated string is :%s", s1);
scanf("%ch", &ch);
return (0);
del(str, ch);
getch(); }
}
void concat(char s1[], char s2[]) { i++;

int i, j; j--;

i = strlen(s1); }

for (j = 0; s2[j] != '\0'; i++, j++) { printf("\nReverse string is :%s", str);

s1[i] = s2[j]; return (0);

}
Output :
s1[i] = '\0';

} 1 Enter the string : Pritesh

2 Reverse string is : hsetirP

Output of Program :

C Program to Compare Two Strings Without Using


1 Enter String 1 : Pritesh Library Function

2 Enter String 2 : Taral


#include<stdio.h>
3 Concated string is : PriteshTaral int main() {
char str1[30], str2[30];
int i;
Reverse String Without Using Library Function [
Strrev ] printf("\nEnter two strings :");
gets(str1);
gets(str2);
#include<stdio.h>
i = 0;
#include<string.h> while (str1[i] == str2[i] && str1[i] != '\0')
i++;
int main() { if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
char str[100], temp;
printf("str1 < str2");
else
int i, j = 0; printf("str1 = str2");

printf("\nEnter the string :"); return (0);


}
gets(str);
C Program to Copy One String into Other Without
i = 0; Using Library Function.

j = strlen(str) - 1; #include<stdio.h>

while (i < j) { int main() {


char s1[100], s2[100];
temp = str[i]; int i;

str[i] = str[j]; printf("\nEnter the string :");


gets(s1);
str[j] = temp;
i = 0;
while (s1[i] != '\0') { Search whether character is present in the string or not
s2[i] = s1[i]; :
i++;
} #include<stdio.h>
#include<conio.h>
s2[i] = '\0';
printf("\nCopied String is %s ", s2); int main() {
char str[20], ch;
return (0); int count = 0, i;
}
printf("\nEnter a string : ");
Output : scanf("%s", &str);

printf("\nEnter the character to be searched : ");


1 Enter the string : c4learn.com scanf("%c", &ch);
2 Copied String is c4learn.com
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch)
count++;
}
Count Total number of Capital and Small Letters from
Accepted Line if (count == 0)
printf("\nCharacter '%c'is not present", ch);
#include<stdio.h> else
printf("\nOccurence of character '%c' : %d", ch, count);
int main() {
int upper = 0, lower = 0; return (0);
char ch[80]; }
int i;

printf("\nEnter The String : "); Output :


gets(ch);

i = 0; <strong>First Run :</strong>


1
while (ch[i] != '') { Enter a string : c4learn.blogspot.com
2
if (ch[i] >= 'A' && ch[i] <= 'Z') Enter the character to be searched : o
3
upper++; Occurence of character 'o' : 3
4
if (ch[i] >= 'a' && ch[i] <= 'z') 5
lower++; 6
i++; <strong>Second Run :</strong>
7
} Enter a string : c4learn.blogspot.com
8
Enter the character to be searched : x
9
printf("\nUppercase Letters : %d", upper); Character 'x'is not present
printf("\nLowercase Letters : %d", lower);
With using User-defined Function Write a Program to
return (0);
Find Length of String
}

#include<stdio.h>
Output :
int FindLength(char str[]);
1 Enter The String : Pritesh A Taral int main() {
2 Uppercase Letters : 3 char str[100];
3 Lowercase Letters : 10 int length;
printf("\nEnter the String : ");
gets(str);

length = FindLength(str);

printf("\nLength of the String is : %d", length);


return(0);
}
to compute the sum of all elements stored in an array
int FindLength(char str[]) { using pointers
int len = 0;
while (str[len] != '\0') #include<stdio.h>
len++;
return (len); #include<conio.h>
}
void main() {
Swap Two Numbers / Variables using Pointer
int numArray[10];
#include<stdio.h>
int i, sum = 0;
void swap(int *num1, int *num2) {
int *ptr;
int temp;
printf("\nEnter 10 elements : ");
temp = *num1;
for (i = 0; i < 10; i++)
*num1 = *num2;
scanf("%d", &numArray[i]);
*num2 = temp;
ptr = numArray; /* a=&a[0] */
}
for (i = 0; i < 10; i++) {
int main() {
sum = sum + *ptr;
int num1, num2;
ptr++;
printf("\nEnter the first number : ");
} printf("The sum of array elements : %d", sum);
scanf("%d", &num1);
}
printf("\nEnter the Second number : ");

scanf("%d", &num2); Output :

swap(&num1, &num2);
1 Enter 10 elements : 11 12 13 14 15 16 17 18 19 20
printf("\nFirst number : %d", num1);
2 The sum of array elements is 155
printf("\nSecond number : %d", num2);

return (0); C Program to Print Square of Each Element of 2D


Matrix

Output : #include<stdio.h>
#include<conio.h>
1 Enter the first number : 12 #define MAX_ROWS 3
#define MAX_COLS 4
2 Enter the Second number : 22
void print_square(int[]);
3 First number : 22
void main(void) {
4 Second number : 12 int row;
int num[MAX_ROWS][MAX_COLS] = { { 0, 1, 2, 3 },
{ 4, 5, 6, 7 }, printf("\nTranspose matrix is :");
{ 8, 9, 10, 11 } }; for (i = 0; i < size; i++) {
printf("\n");
for (row = 0; row < MAX_ROWS; row++) for (j = 0; j < size; j++) {
print_square(num[row]); printf("%d\t", arr[i][j]);
} }
}
void print_square(int x[]) {
int col; getch();
for (col = 0; col < MAX_COLS; col++) }
printf("%d\t", x[col] * x[col]);
printf("\n");
}

Output :

0 1 4 9
16 25 36 49
64 81 100 121

to find addition of Lower Triangular Elements in C


Programming
Program to find Transpose of Given Square Matrix
#include<stdio.h>
#include<stdio.h>
#include<conio.h> #include<conio.h>

void main() { int main() {


int arr[10][10], size, i, j, temp;
int i, j, a[10][10], sum, rows, columns;
printf("\nEnter the size of matrix :");
scanf("%d", &size);
printf("\nEnter the number of Rows : ");
printf("\nEnter the values a:");
for (i = 0; i < size; i++) { scanf("%d", &rows);
for (j = 0; j < size; j++) {
scanf("%d", &arr[i][j]); printf("\nEnter the number of Columns : ");
}
} scanf("%d", &columns);
printf("\nGiven square matrix is");
//Accept the Elements in Matrix
for (i = 0; i < size; i++) {
printf("\n");
for (j = 0; j < size; j++) { for (i = 0; i < rows; i++)
printf("%d\t", arr[i][j]);
} for (j = 0; j < columns; j++) {
}
printf("\nEnter the Element a[%d][%d] : ", i, j);
/* Find transpose */
for (i = 1; i < size; i++) { scanf("%d", &a[i][j]);
for (j = 0; j < i; j++) {
temp = arr[i][j]; }
arr[i][j] = arr[j][i];
arr[j][i] = temp;
} //Addition of all Diagonal Elements
}
sum = 0; scanf("%d", &rows);

printf("\nEnter the number of Columns : ");


for (i = 0; i < rows; i++)
scanf("%d", &columns);
for (j = 0; j < columns; j++) { //Accept the Elements in Matrix
for (i = 0; i < rows; i++)
// Condition for Lower Triangle for (j = 0; j < columns; j++) {
printf("\nEnter the Element a[%d][%d] : ", i, j);
if (i > j) { scanf("%d", &a[i][j]);
}
sum = sum + a[i][j];
//Addition of all Diagonal Elements
} sum = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < columns; j++) {
}
// Condition for Upper Triangle
if (i < j) {
//Print out the Result sum = sum + a[i][j];
}
printf("\nSum of Lower Triangle Elements : %d", sum); }

return (0); //Print out the Result


printf("\nSum of Upper Triangle Elements : %d", sum);
} return (0);
}

Output : Output
1 Enter the Element a[0][0] : 1
1 Enter the number of Rows : 3
2 Enter the Element a[0][1] : 2 2 Enter the number of Columns : 3
3
3 Enter the Element a[0][2] : 3 4 Enter the Element a[0][0] : 1
5 Enter the Element a[0][1] : 2
4 Enter the Element a[1][0] : 2 6 Enter the Element a[0][2] : 3
7 Enter the Element a[1][0] : 2
8 Enter the Element a[1][1] : 1
5 Enter the Element a[1][1] : 1
9 Enter the Element a[1][2] : 1
10 Enter the Element a[2][0] : 1
6 Enter the Element a[1][2] : 1 11 Enter the Element a[2][1] : 2
12 Enter the Element a[2][2] : 1
7 Enter the Element a[2][0] : 1 13
14 Sum of Upper Triangle Elements : 6
8 Enter the Element a[2][1] : 2

9 Enter the Element a[2][2] : 1

C Program to evaluate Subtraction of two matrices (


10 Sum of Lower Triangle Elements : 5
matrix ) in C

#include<stdio.h>
to calculate sum of Upper Triangular Elements in C
int main() {
#include<stdio.h> int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
#include<conio.h> int row1, col1, row2, col2;

int main() { printf("\nEnter the number of Rows of Mat1 : ");


int i, j, a[10][10], sum, rows, columns; scanf("%d", &row1);
printf("\nEnter the number of Cols of Mat1 : ");
printf("\nEnter the number of Rows : "); scanf("%d", &col1);
13 Enter the Element a[2][0] : 2
printf("\nEnter the number of Rows of Mat2 : "); 14 Enter the Element a[2][1] : 4
scanf("%d", &row2); 15 Enter the Element a[2][2] : 2
printf("\nEnter the number of Columns of Mat2 : "); 16
scanf("%d", &col2); 17
18 Enter the Element b[0][0] : 1
/* Before accepting the Elements Check if no of 19 Enter the Element b[0][1] : 2
rows and columns of both matrices is equal */ 20 Enter the Element b[0][2] : 3
if (row1 != row2 || col1 != col2) { 21 Enter the Element b[1][0] : 2
printf("\nOrder of two matrices is not same "); 22 Enter the Element b[1][1] : 1
exit(0); 23 Enter the Element b[1][2] : 1
} 24 Enter the Element b[2][0] : 1
25 Enter the Element b[2][1] : 2
//Accept the Elements in Matrix 1 26 Enter the Element b[2][2] : 1
for (i = 0; i < row1; i++) { 27
for (j = 0; j < col1; j++) { 28 The Subtraction of two Matrices is :
printf("Enter the Element a[%d][%d] : ", i, j); 29 1 2 3
scanf("%d", &mat1[i][j]); 30 2 1 1
} 31 1 2 1
}

//Accept the Elements in Matrix 2 C program for addition of two matrices in C


for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++) { #include<stdio.h>
printf("Enter the Element b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]); int main() {
} int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
//Subtraction of two matrices
for (i = 0; i < row1; i++) printf("\nEnter the number of Rows of Mat1 : ");
for (j = 0; j < col1; j++) { scanf("%d", &row1);
mat3[i][j] = mat1[i][j] - mat2[i][j]; printf("\nEnter the number of Cols of Mat1 : ");
} scanf("%d", &col1);

//Print out the Resultant Matrix printf("\nEnter the number of Rows of Mat2 : ");
printf("\nThe Subtraction of two Matrices is : \n"); scanf("%d", &row2);
for (i = 0; i < row1; i++) { printf("\nEnter the number of Columns of Mat2 : ");
for (j = 0; j < col1; j++) { scanf("%d", &col2);
printf("%d\t", mat3[i][j]);
} /* Before accepting the Elements Check if no of
printf("\n"); rows and columns of both matrices is equal */
} if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same ");
return (0); exit(0);
} }

//Accept the Elements in Matrix 1


Output :
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
1 Enter the number of Rows of Mat1 : 3 printf("Enter the Element a[%d][%d] : ", i, j);
2 Enter the number of Columns of Mat1 : 3 scanf("%d", &mat1[i][j]);
3 }
4 Enter the number of Rows of Mat2 : 3 }
5 Enter the number of Columns of Mat2 : 3
6 //Accept the Elements in Matrix 2
7 Enter the Element a[0][0] : 2 for (i = 0; i < row2; i++)
8 Enter the Element a[0][1] : 4 for (j = 0; j < col2; j++) {
9 Enter the Element a[0][2] : 6 printf("Enter the Element b[%d][%d] : ", i, j);
10 Enter the Element a[1][0] : 4 scanf("%d", &mat2[i][j]);
11 Enter the Element a[1][1] : 2 }
12 Enter the Element a[1][2] : 2
//Addition of two matrices Addition of All Elements in Matrix
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) { #include<stdio.h>
mat3[i][j] = mat1[i][j] + mat2[i][j];
}

//Print out the Resultant Matrix


printf("\nThe Addition of two Matrices is : \n"); int main() {
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) { int i, j, mat[10][10], row, col;
printf("%d\t", mat3[i][j]);
} int sum = 0;
printf("\n");
} printf("\nEnter the number of Rows : ");
return (0);
scanf("%d", &row);
}
printf("\nEnter the number of Columns : ");
Output
scanf("%d", &col);
1 Enter the number of Rows of Mat1 : 3
2 Enter the number of Columns of Mat1 : 3 //Accept the Elements in Matrix
3
4 Enter the number of Rows of Mat2 : 3 for (i = 0; i < row; i++) {
5 Enter the number of Columns of Mat2 : 3
6 for (j = 0; j < col; j++) {
7 Enter the Element a[0][0] : 1
8 Enter the Element a[0][1] : 2
printf("\nEnter the Element mat[%d][%d] : ", i, j);
9 Enter the Element a[0][2] : 3
10 Enter the Element a[1][0] : 2
11 Enter the Element a[1][1] : 1 scanf("%d", &mat[i][j]);
12 Enter the Element a[1][2] : 1
13 Enter the Element a[2][0] : 1 }
14 Enter the Element a[2][1] : 2
15 Enter the Element a[2][2] : 1 }
16
17 Enter the Element b[0][0] : 1 //Addition of all Elements
18 Enter the Element b[0][1] : 2
19 Enter the Element b[0][2] : 3 for (i = 0; i < row; i++) {
20 Enter the Element b[1][0] : 2
21 Enter the Element b[1][1] : 1
22 Enter the Element b[1][2] : 1 for (j = 0; j < col; j++) {
23 Enter the Element b[2][0] : 1
24 Enter the Element b[2][1] : 2 sum = sum + mat[i][j];
25 Enter the Element b[2][2] : 1
26 }
27 The Addition of two Matrices is :
28 246 }
29 422
30 242 //Print out the Result

printf("\nSum of All Elements in Matrix : %d", sum);

return (0);

}
Output : //Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
1 Enter the number of Rows : 2 sum = 0;
for (k = 0; k <= 2; k++) {
2 Enter the number of Columns : 2 sum = sum + a[i][k] * b[k][j];
}
3 c[i][j] = sum;
}
4 Enter the Element mat[0][0] : 1 }

5 Enter the Element mat[0][1] : 1 printf("\nMultiplication Of Two Matrices : \n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
6 Enter the Element mat[1][0] : 1
printf(" %d ", c[i][j]);
}
7 Enter the Element mat[1][1] : 1
printf("\n");
}
8
return (0);
9 Sum of All Elements in Matrix : 4 }

C Program to Multiply Two 3 X 3 Matrices

#include<stdio.h>

int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;

printf("\nEnter First Matrix : n");


for (i = 0; i < 3; i++) { Program : To delete duplicate elements in an array
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]); #include<stdio.h>
}
} int main() {
int arr[20], i, j, k, size;
printf("\nEnter Second Matrix:n");
for (i = 0; i < 3; i++) { printf("\nEnter array size : ");
for (j = 0; j < 3; j++) { scanf("%d", &size);
scanf("%d", &b[i][j]);
} printf("\nAccept Numbers : ");
} for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("The First Matrix is: \n");
for (i = 0; i < 3; i++) { printf("\nArray with Unique list : ");
for (j = 0; j < 3; j++) { for (i = 0; i < size; i++) {
printf(" %d ", a[i][j]); for (j = i + 1; j < size;) {
} if (arr[j] == arr[i]) {
printf("\n"); for (k = j; k < size; k++) {
} arr[k] = arr[k + 1];
}
printf("The Second Matrix is : \n"); size--;
for (i = 0; i < 3; i++) { } else
for (j = 0; j < 3; j++) { j++;
printf(" %d ", b[i][j]); }
} }
printf("\n");
} for (i = 0; i < size; i++) {
printf("%d ", arr[i]); Output :
}
1 Enter no of elements : 5
return (0);
} 2 11 44 22 55 99

Output : 3 Smallest Element : 11


Enter array size : 5
Accept Numbers : 1 3 4 5 3
Array w ith Unique list : 1 3 4 5

Find Largest Element in Array in C Programming

#include<stdio.h>
Find Smallest Element in Array in C Programming
int main() {

#include<stdio.h> int a[30], i, num, largest;

int main() { printf("\nEnter no of elements :");

int a[30], i, num, smallest; scanf("%d", &num);

printf("\nEnter no of elements :"); //Read n elements in an array

scanf("%d", &num); for (i = 0; i < num; i++)

//Read n elements in an array scanf("%d", &a[i]);

for (i = 0; i < num; i++) //Consider first element as largest

scanf("%d", &a[i]); largest = a[0];

//Consider first element as smallest for (i = 0; i < num; i++) {

smallest = a[0]; if (a[i] > largest) {

for (i = 0; i < num; i++) { largest = a[i];

if (a[i] < smallest) { }

smallest = a[i]; }

} // Print out the Result

} printf("\nLargest Element : %d", largest);

// Print out the Result

printf("\nSmallest Element : %d", smallest); return (0);

return (0); }
Output : }

1 Enter no of elements : 5 return (0);

2 11 55 33 77 22
Output :
3 Largest Element : 77

1 Enter no of elements : 5
C Program to Reversing an Array Elements in C
Programming 2 11 22 33 44 55

3 Result after reversal : 55 44 33 22 11


#include<stdio.h>

int main() {

int arr[30], i, j, num, temp; C Program to Merge Two arrays in C Programming

printf("\nEnter no of elements : "); #include<stdio.h>

scanf("%d", &num); int main() {

//Read elements in an array int arr1[30], arr2[30], res[60];

for (i = 0; i < num; i++) { int i, j, k, n1, n2;

scanf("%d", &arr[i]); printf("\nEnter no of elements in 1st array :");

} scanf("%d", &n1);

j = i - 1; // j will Point to last Element for (i = 0; i < n1; i++) {

i = 0; // i will be pointing to first element scanf("%d", &arr1[i]);

while (i < j) { printf("\nEnter no of elements in 2nd array :");

temp = arr[i]; scanf("%d", &n2);

arr[i] = arr[j]; for (i = 0; i < n2; i++) {

arr[j] = temp; scanf("%d", &arr2[i]);

i++; // increment i i = 0;

j--; // decrement j j = 0;

} k = 0;

//Print out the Result of Insertion // Merging starts

printf("\nResult after reversal : "); while (i < n1 && j < n2) {

for (i = 0; i < num; i++) { if (arr1[i] <= arr2[j]) {

printf("%d \t", arr[i]);


res[k] = arr1[i]; //Displaying elements of array 'res'

i++; printf("\nMerged array is :");

k++; f or (i = 0; i < n1 + n2; i++)

} else { printf("%d ", res[i]);

res[k] = arr2[j]; return (0);

k++; Output :

j++;
1 Enter no of elements in 1st array : 4

}
2 11 22 33 44

} 3

4 Enter no of elements in 2nd array : 3

/* Some elements in array 'arr1' are still remaining 5 10 40 80

where as the array 'arr2' is exhausted */ 6

7 Merged array is : 10 11 22 33 40 44 80

while (i < n1) {

res[k] = arr1[i]; C Program to Search an element in Array

i++; #include<stdio.h>

k++; int main() {

} int a[30], ele, num, i;

printf("\nEnter no of elements :");

/* Some elements in array 'arr2' are still remaining scanf("%d", &num);

where as the array 'arr1' is exhausted */ printf("\nEnter the values :");

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

while (j < n2) { scanf("%d", &a[i]);

res[k] = arr2[j]; }

k++; //Read the element to be searched

j++; printf("\nEnter the elements to be searched :");

} scanf("%d", &ele);

//Search starts from the zeroth location


i = 0; scanf("%d", &arr1[i]);

while (i < num && ele != a[i]) { }

i++; /* Copying data from array 'a' to array 'b */

} for (i = 0; i < num; i++) {

//If i < num then Match found arr2[i] = arr1[i];

if (i < num) { }

printf("Number found at the location = %d", i + 1); //Printing of all elements of array

} else { printf("The copied array is :");

printf("Number not found"); for (i = 0; i < num; i++)

} printf("\narr2[%d] = %d", i, arr2[i]);

return (0); return (0);

}
Output :

Output : 1 Enter no of elements : 5

1 Enter no of elements : 5 2 Enter the values : 11 22 33 44 55

2 11 22 33 44 55 3 The copied array is : 11 22 33 44 55

3 Enter the elements to be searched : 44


C Program to Insert an element in an Array
4 Number found at the location = 4

#include<stdio.h>

C Program to Copy all elements of an array into int main() {


Another array
int arr[30], element, num, i, location;
#include<stdio.h>
printf("\nEnter no of elements :");
int main() {
scanf("%d", &num);
int arr1[30], arr2[30], i, num;
for (i = 0; i < num; i++) {
printf("\nEnter no of elements :");
scanf("%d", &arr[i]);
scanf("%d", &num);
}
//Accepting values into Array
printf("\nEnter the element to be inserted :");
printf("\nEnter the values :");
scanf("%d", &element);
for (i = 0; i < num; i++) {
printf("\nEnter the location"); printf("\n location of the element to be deleted :");
scanf("%d", &loc);
scanf("%d", &location);
/* loop for the deletion */
while (loc < num) {
//Create space at the specified location arr[loc - 1] = arr[loc];
loc++;
for (i = num; i >= location; i--) { }
num--; // No of elements reduced by 1
arr[i] = arr[i - 1];
//Print Array
} for (i = 0; i < num; i++)
printf("\n %d", arr[i]);
num++;
return (0);
}
arr[location - 1] = element;

//Print out the result of insertion

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

printf("n %d", arr[i]);

return (0);

Output of the Program :

1 Enter no of elements : 5

2 12345

3 Enter the element to be inserted : 6

4 Enter the location : 2

5 162345

C Program to Delete an element from the specified


location from Array

#include<stdio.h>

int main() {
int arr[30], num, i, loc;

printf("\nEnter no of elements :");


scanf("%d", &num);

//Read elements in an array


printf("\nEnter %d elements :", num);
for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}

//Read the location

You might also like