[go: up one dir, main page]

0% found this document useful (0 votes)
14 views40 pages

PIC Experiment 1-8

The document contains a series of C programming experiments covering various topics such as finding ASCII values, checking for prime numbers, calculating LCM, array manipulations, matrix operations, and string handling. Each experiment includes problem statements, corresponding C code, and expected outputs. The document serves as a practical guide for students to learn and practice fundamental programming concepts in C.

Uploaded by

manthan.moondra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views40 pages

PIC Experiment 1-8

The document contains a series of C programming experiments covering various topics such as finding ASCII values, checking for prime numbers, calculating LCM, array manipulations, matrix operations, and string handling. Each experiment includes problem statements, corresponding C code, and expected outputs. The document serves as a practical guide for students to learn and practice fundamental programming concepts in C.

Uploaded by

manthan.moondra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

PIC

Experiments 1-8 (Semester II)

Experiment 1
a)

Code

// Problem Statement: Write a C program to find ASCII values of a


character.

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

printf("Enter character: ");


scanf("%c", &x);

printf("\nThe ASCII value is: %d.\n", x);


return 0;
}

Output

b)

Code

// Problem Statement: Write a C program to check whether a given number is


prime or not.

#include <stdio.h>

int main()
{

#define PRIME 0;
#define COMPOSITE 1;
int x;
int toggle = PRIME;

printf("Enter value: ");


scanf("%d", &x);

for (int i = 2; i < x; i++)


{
if (x % i == 0)
{
toggle = COMPOSITE;
}
}
if (!toggle)
{
printf("\n%d is a prime number.", x);
}
else
{
printf("\n%d is a composite number.", x);
}
printf("\n");
return 0;
}

Output

c)

Code

// Problem Statement: Write a C program to find out the LCM of two


numbers.

#include <stdio.h>
int main()
{
int x, y, hcf, lcm;
printf("Enter value x: ");
scanf("%d", &x);

printf("Enter value y: ");


scanf("%d", &y);

for (int i = 1; i <= x && i <= y; i++)


{
if (x % i == 0 && y % i == 0) {
hcf = i;
}
}

lcm = (x * y) / hcf;
printf("The LCM of %d and %d is %d.\n", x, y, lcm);
return 0;
}

Output

Experiment 2
a)

Code

// Problem Statement: Write a C program to take 5 values from user


and store them in an array.

#include <stdio.h>

int main()
{
int x[5];

for (int i = 0; i < 5; i++)


{
printf("\nEnter value of element %d: ", i);
scanf("%d", &x[i]);
}
for (int i = 0; i < 5; i++)
{
printf("\nElement %d: %d", i, x[i]);
}
printf("\n");
return 0;
}

Output

b)

Code

// Problem Statement: Write a C program to find the average of n numbers using


an array.

#include <stdio.h>

int main()
{
int n;

printf("Enter number of elements: ");


scanf("%d", &n);

int x[n];

for (int i = 0; i < n; i++)


{
printf("\nEnter value of element %d: ", i);
scanf("%d", &x[i]);
}
float average, sum = 0;

for (int i = 0; i < n; i++)


{
sum += x[i];
}

average = sum / n;
printf("\nThe average of given numbers is: %f. \n", average);
return 0;
}

Output

c)

Code

// Problem Statement: Write a C program to print the following C pattern.


// *
// ##
// &&&
/// @@@@

#include <stdio.h>
int main()
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
{
switch (i)
{
case 1:
printf("#");
break;
case 2:
printf("*");
break;
case 3:
printf("&");
break;
case 4:
printf("@");
break;
}
}
printf("\n");
}
return 0;
}

Output

d)

Code

// Problem Statement: Write a C program to sort the elements of an


array in ascending order.

#include <stdio.h>

int main()
{
int n;

printf("Enter number of elements: ");


scanf("%d", &n);

int x[n];

for (int i = 0; i < n; i++)


{
printf("\nEnter value of element %d: ", i);
scanf("%d", &x[i]);
}

for (int i = 0; i < n; i++)


{
for (int j = i + 1; j < n; j++)
{
if (x[j] < x[i])
{
int temp = x[j];
x[j] = x[i];
x[i] = temp;
}
}
}
printf("\nSorted array is: ");

for (int i = 0; i < n; i++)


{
printf("\nElement %d: %d", i, x[i]);
}
printf("\n");
return 0;
}

Output
Experiment 3
a)

Code

// Problem Statement: Write C program for finding the greatest and


the smallest element in the array

#include <stdio.h>

int main()
{
int n;

printf("Enter number of elements: ");


scanf("%d", &n);

int x[n];

for (int i = 0; i < n; i++)


{
printf("\nEnter value of element %d: ", i);
scanf("%d", &x[i]);
}

int max = x[0];


int min = x[0];

for (int i = 0; i < n; i++)


{
if (x[i] > max)
{
max = x[i];
}
if (x[i] < min)
{
min = x[i];
}
}
printf("\nThe minimum value is %d and the maximum value is %d. \n", min, max);
return 0;
}

Output
b)

Code

// Problem Statement: Write C program squaring the even positioned and cubing
the odd positioned elements.

#include <stdio.h>

int main()
{
int n;

printf("Enter number of elements: ");


scanf("%d", &n);

int x[n], y[n];

for (int i = 0; i < n; i++)


{
printf("\nEnter value of element %d: ", i);
scanf("%d", &x[i]);
}

for (int i = 0; i < n; i++)


{
if (i % 2 == 0)
{
y[i] = x[i] * x[i];
}
else
{
y[i] = x[i] * x[i] * x[i];
}
}
printf("\nThe new array is:\n");
for (int i = 0; i < n; i++)
{
printf("Element %d: %d\n", i, y[i]);
}

return 0;
}

Output

c)

Code

// Problem Statement: Write a C program to move all the negative elements to


one side of the array

#include <stdio.h>

int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

int x[n];

for (int i = 0; i < n; i++)


{
printf("\nEnter value of element %d: ", i);
scanf("%d", &x[i]);
}

int left = 0;
int right = n - 1;

while (left < right)


{
if (x[left] < 0)
{

int temp = x[left];


x[left] = x[right];
x[right] = temp;
right--;
}
else
{
left++;
}
}

printf("\nNew array is: ");

for (int i = 0; i < n; i++)


{
printf("\nElement %d: %d", i, x[i]);
}
printf("\n");
return 0;
}

Output
Experiment 4
a)

Code

// Problem Statement: Write a C Program for matrix multiplication for 3*3


matrix.

#include <stdio.h>

int main()
{
int m = 3;
int n = 3;

int A[m][n], B[m][n], C[m][n];

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
printf("\nEnter value of element (%d, %d) in Matrix A: ", i + 1, j +
1);
scanf("%d", &A[i][j]);
}
}

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
printf("\nEnter value of element (%d, %d) in Matrix B: ", i + 1, j +
1);
scanf("%d", &B[i][j]);
}
}

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
C[i][j] = 0;
for (int k = 0; k < n; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}

printf("\nNew Matrix is: ");

for (int i = 0; i < m; i++)


{
printf("\n[");
for (int j = 0; j < n; j++)
{
printf(" %d ", C[i][j]);
}
printf("]");
}

printf("\n");
return 0;
}

Output
b)

Code

// Problem Statement: Write a C Program for counting positive,


negative, even, odd and zeros in an array.

#include <stdio.h>

int main()
{
int n;

printf("Enter number of elements: ");


scanf("%d", &n);

int x[n], positive, negative, even, odd, zero;

positive = negative = even = odd = zero = 0;

for (int i = 0; i < n; i++)


{
printf("\nEnter value of element %d: ", i);
scanf("%d", &x[i]);
}

for (int i = 0; i < n; i++)


{
if (x[i] > 0)
{
positive++;
}
else if (x[i] < 0)
{
negative++;
}
else if (x[i] == 0)
{
zero++;
}

if (x[i] % 2 == 0)
{

even++;
}
else
{
odd++;
}
}

printf("\nThere are %d positive integers, %d negative integers, %d zeros, %d


even numbers and %d odd numbers.", positive, negative, zero, even, odd);
return 0;
}

Output

c)
Code

// Problem Statement: Write a C Program for generating the transpose of the


matrix.

#include <stdio.h>

int main()
{
int m;
int n;

printf("Enter number of rows: ");


scanf("%d", &m);

printf("Enter number of columns: ");


scanf("%d", &n);

int A[m][n], B[n][m];

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
printf("\nEnter value of element (%d, %d) in Matrix: ", i + 1, j + 1);
scanf("%d", &A[i][j]);
}
}

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
B[j][i] = A[i][j];
}
}

printf("\nNew Matrix is: ");

for (int i = 0; i < n; i++)


{
printf("\n[");
for (int j = 0; j < m; j++)
{
printf(" %d ", B[i][j]);
}
printf("]");
}
printf("\n");
return 0;
}
Output

d)

Code

// Problem Statement: C program to store temperature of two cities


of a week and display it

#include <stdio.h>

int main()
{
int m = 7;
int n = 2;

float A[m][n];

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
printf("Enter temperature of city %d on day %d of the week: ", j + 1, i
+ 1);
scanf("%f", &A[i][j]);
}
}

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
printf("The temperature of city %d on day %d: %f \n", j + 1, i + 1,
A[i][j]);
}
}

printf("\n");
return 0;
}

Output

e)

Code

// Problem Statement: Write a C Program to store and Print 12 values entered by


the user using a three-dimensional array.

#include <stdio.h>
int main()
{
int l = 3; // Side
int m = 2; // Row
int n = 2; // Column

int A[l][m][n];

for (int i = 0; i < l; i++)


{
for (int j = 0; j < m; j++)
{
for (int k = 0; k < n; k++)
{
printf("\nEnter value of element (%d, %d) in Matrix of side %d: ",
j + 1, k + 1, i + 1);
scanf("%d", &A[i][j][k]);
}
}
}

printf("\nThe values entered are: ");

for (int i = 0; i < l; i++)


{
printf("\nSide %d", i + 1);
for (int j = 0; j < m; j++)
{
printf("\n[");
for (int k = 0; k < n; k++)
{
printf(" %d ", A[i][j][k]);
}
printf("]");
}
}
printf("\n");
return 0;
}

Output
Experiment 5
a)
Code
// Problem Statement: Write a program in C to input a string and print reverse
string.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char x[255];

printf("Enter string: ");


fgets(x, sizeof(x), stdin);

printf("%s", strrev(x));
printf("\n");
return 0;
}

Option 1 B
// If the above code gives a function not declared error, it means
your compiler does not support strrev.
// Add the below code above the int main() function.

char *strrev(char *src)


{

int n = 0;
while (src[n] != '\0')
{
n++;
}

for (int i = 0, j = n - 1; i < j; i++, j--)


{
char temp = src[i];
src[i] = src[j];
src[j] = temp;
}

return src;
}

Option 2

// Problem Statement: Write a program in C to input a string and print reverse


string.

#include <stdio.h>
#include <string.h>

int main()
{
char x[255];
char y[255];

printf("Enter string: ");


fgets(x, sizeof(x), stdin);

int i, j;
j = strlen(x) - 1;

for (i = 0; j >= 0; i++, j--)


{
y[i] = x[j];
}
printf("%s", y);
printf("\n");
return 0;
}
Output

b)
Code

// Problem Statement: Write a program in C to copy one string to another


string.

#include <stdio.h>
#include <string.h>

int main()
{

char x[255], y[255];

printf("Enter string: ");


fgets(x, sizeof(x), stdin);

strcpy(y, x);

printf("%s", y);
printf("\n");
return 0;
}

Output

c)

Code
// Problem Statement: Write a C program to count digits, spaces, special
character, alphabets in string.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
char x[255];

printf("Enter string: ");


fgets(x, sizeof(x), stdin);

int numbers = 0, alphabets = 0, spaces = 0, specialcharacters = 0;

for (int i = 0; x[i] != '\0'; i++)


{
if (isdigit(x[i]))
{
numbers++;
}
else if (isalpha(x[i]))
{
alphabets++;
}
else if (isspace(x[i]))
{
spaces++;
}
else
{
specialcharacters++;
}
}

// Adjusting special character count for newline character


if (x[strlen(x) - 1] == '\n')
{
specialcharacters--;
}

printf("\nThere are %d integers, %d alphabets, %d spaces and %d special


characters.\n", numbers, alphabets, spaces, specialcharacters);
return 0;
}

Option 2
// Problem Statement: Write a C program to count digits, spaces, special
character, alphabets in string.

// This method does not use any standard function.

#include <stdio.h>
#include <string.h>

int main()
{
char x[255];

printf("Enter string: ");


fgets(x, sizeof(x), stdin);

int numbers = 0, alphabets = 0, spaces = 0, specialcharacters = 0;

for (int i = 0; x[i] != '\0'; i++)


{
if (x[i] >= '0' && x[i] <= '9')
{
numbers++;
}
else if ((x[i] >= 'A' && x[i] <= 'Z') || (x[i] >= 'a' && x[i] <= 'z'))
{
alphabets++;
}
else if (x[i] == ' ')
{
spaces++;
}
else
{
specialcharacters++;
}
}

// Adjusting special character count for newline character


if (x[strlen(x) - 1] == '\n')
{
specialcharacters--;
}

printf("\nThere are %d integers, %d alphabets, %d spaces and %d special


characters.\n", numbers, alphabets, spaces, specialcharacters);
return 0;
}

Output
d)

Code

// Problem Statement: Write a C program to sort 5 strings entered by the user


in the lexicographical order (dictionary order).

#include <stdio.h>
#include <string.h>

int main()
{
char x[5][255];

for (int i = 0; i < 5; i++)


{
printf("Enter string %d: ", i);
fgets(x[i], sizeof(x)[i], stdin);
int a = strlen(x[i]) - 1;

if (x[i][a] == '\n')
{
x[i][a] = '\0';
}
}
for (int i = 0; i < 5 - 1; i++)
{
for (int j = i + 1; j < 5; j++)
{
int comparison = strcmp(x[i], x[j]);
if (comparison > 0)
{
char temp[255];
strcpy(temp, x[i]);
strcpy(x[i], x[j]);
strcpy(x[j], temp);
}
}
}
printf("Sorted string is: \n");
for (int i = 0; i < 5; i++)
{
printf("%s \n", x[i]);
}
return 0;
}
Output

e)

Code

// Problem Statement: Write a program to ask the user to enter a username, If


the username entered is one of the names in the master list then the user is
allowed to enter a number and calculate its factorial using C. Otherwise, display
error message. (Initialize master list of username using array).

#include <stdio.h>
#include <string.h>

int main()
{
int size;
printf("Enter number of names in the database: ");
scanf("%d", &size);
getchar();

char x[size][255];

for (int i = 0; i < size; i++)


{
printf("Enter name %d: ", i + 1);
fgets(x[i], sizeof(x)[i], stdin);
int a = strlen(x[i]) - 1;

if (x[i][a] == '\n')
{
x[i][a] = '\0';
}
}
char username[255];
int toggle = 0;

printf("Enter your username: ");


fgets(username, sizeof(username), stdin);

int b = strlen(username) - 1;

if (username[b] == '\n')
{
username[b] = '\0';
}

for (int i = 0; i < size; i++)


{
if (strcmp(username, x[i]) == 0)
{
toggle = 1;
break;
}
}

if (toggle)
{
int n, factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);

for (int i = 1; i <= n; i++)


{
factorial = factorial * i;
}
printf("The factorial of given number is: %d.\n", factorial);
}
else
{
printf("This username does not exist. Access denied. \n");
}
return 0;
}

Output
Experiment 6
a)
Code

// Problem Statement: Write a C Program to declare the structure of the Book


with data member book name, book price and number of pages

#include <stdio.h>

struct Book
{
char book[50];
float price;
int pages;
};

int main()
{
struct Book book1;

printf("Enter book title: ");


fgets(book1.book, sizeof(book1.book), stdin);

printf("Enter book price: ");


scanf("%f", &book1.price);
printf("Enter number of pages: ");
scanf("%d", &book1.pages);

printf("\nBook Information:\n");
printf("Title: %s", book1.book);
printf("Price: $%.2f\n", book1.price);
printf("Pages: %d\n", book1.pages);

return 0;
}

Output

b)
Code

// Problem Statement: Write a c program to calculate difference between two


time periods using Structure

#include <stdio.h>

struct Time
{
int hours;
int minutes;
int seconds;
};

int main()
{
struct Time startTime, endTime, difference;

printf("Enter start time (HH MM SS): ");


scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);

printf("Enter end time (HH MM SS): ");


scanf("%d %d %d", &endTime.hours, &endTime.minutes, &endTime.seconds);
difference.seconds = endTime.seconds - startTime.seconds;
difference.minutes = endTime.minutes - startTime.minutes;
difference.hours = endTime.hours - startTime.hours;

if (difference.seconds < 0)
{
difference.seconds += 60;
difference.minutes--;
}

if (difference.minutes < 0)
{
difference.minutes += 60;
difference.hours--;
}

if (difference.hours < 0)
{
difference.hours += 24;
}
printf("Difference: %d:%d:%d\n", difference.hours, difference.minutes,
difference.seconds);

return 0;
}

Output

c)
Code

// Problem Statement: Write a program that compares two given dates. To store
data use a structure say date that contains three members namely date, month and
year. If the dates are equal, then display the message as Equal otherwise Unequal.

#include <stdio.h>

struct Date
{
int day;
int month;
int year;
};

int main()
{
struct Date date1, date2;

printf("Enter first date (DD MM YYYY): ");


scanf("%d %d %d", &date1.day, &date1.month, &date1.year);

printf("Enter second date (DD MM YYYY): ");


scanf("%d %d %d", &date2.day, &date2.month, &date2.year);

if (date1.year == date2.year && date1.month == date2.month && date1.day ==


date2.day)
{
printf("Dates are equal.\n");
}
else
{
printf("Dates are not equal.\n");
}

return 0;
}
Output

d)
Code
// Problem Statement: Initialise and print the following union variables: 7,
and 7.0 using the following program that has a union and a member to track what's
in the union inside a structure.
#include <stdio.h>

struct MyStructure
{
char type;
union
{
int integerValue;
float floatValue;
} data;
};
int main()
{
struct MyStructure s1;

s1.type = 'i';
s1.data.integerValue = 7;
printf("Integer value: %d\n", s1.data.integerValue);

s1.type = 'f';
s1.data.floatValue = 7.0;
printf("Float value: %.2f\n", s1.data.floatValue);

return 0;
}
Output

Experiment 7
a)
Code
// Problem Statement: Create a structure named company which has name,
address, phone and noofemployee as member variables. Read name of 10 companies, its
address, phone and noOfEmployee. Finally display these members' value.

#include <stdio.h>
#include <string.h>

struct company
{
char name[255];
char address[1024];
char phone[15];
int noOfEmployee;
};

int main()
{
int numCompanies = 10;
printf("Enter the number of companies: ");
scanf("%d", &numCompanies);
getchar();

struct company companies[numCompanies];


for (int i = 0; i < numCompanies; ++i)
{
printf("Enter details for company %d\n", i + 1);
printf("Name: ");
fgets(companies[i].name, sizeof(companies[i].name), stdin);
companies[i].name[strlen(companies[i].name) - 1] = '\0';

printf("Address: ");
fgets(companies[i].address, sizeof(companies[i].address), stdin);
companies[i].address[strlen(companies[i].address) - 1] = '\0';

printf("Phone: ");
fgets(companies[i].phone, sizeof(companies[i].name), stdin);
companies[i].phone[strlen(companies[i].phone) - 1] = '\0';

printf("Number of Employees: ");


scanf("%d", &companies[i].noOfEmployee);
getchar();
}

printf("\nCompany Details:\n");
for (int i = 0; i < numCompanies; ++i)
{
printf("Company %d\n", i + 1);
printf(" | Name: %s", companies[i].name);
printf(" | Address: %s", companies[i].address);
printf(" | Phone: %s", companies[i].phone);
printf(" | Number of Employees: %d\n\n", companies[i].noOfEmployee);
}

return 0;
}
Output

b)
Code
// Problem Statement: Enter the marks of 5 students in Chemistry, Mathematics
and Physics (each out of 100) using a structure named Marks having elements rol
no., name, chem _marks, maths marks and phy_marks and then display the percentage
of each student.

#include <stdio.h>

struct Marks
{
int rollNo;
char name[255];
int chemMarks;
int mathsMarks;
int phyMarks;
};

int main()
{
int numStudents;
printf("Enter the number of students: ");
scanf("%d", &numStudents);
getchar();

struct Marks students[numStudents];

for (int i = 0; i < numStudents; ++i)


{
printf("Enter details for student %d\n", i + 1);
printf("Roll No: ");
scanf("%d", &students[i].rollNo);
getchar();

printf("Name: ");
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strlen(students[i].name) - 1] = '\0';

printf("Chemistry Marks: ");


scanf("%d", &students[i].chemMarks);
getchar();

printf("Mathematics Marks: ");


scanf("%d", &students[i].mathsMarks);
getchar();

printf("Physics Marks: ");


scanf("%d", &students[i].phyMarks);
getchar();
}
}
Output

c)
Code
// Problem Statement: Write a program to add two distances in inch-feet using
structure. The values of the distances is to be taken from the user. The distances
are entered in inch-feet. The sum of the distances is to be displayed in inch-feet.

#include <stdio.h>

struct Distance
{
int feet;
int inch;
};

int main()
{
int numDistancesInput = 2;
struct Distance distancesInput[numDistancesInput];
struct Distance distancesSum;

for (int i = 0; i < numDistancesInput; ++i)


{
printf("Enter distance %d\n", i + 1);
printf("Feet: ");
scanf("%d", &distancesInput[i].feet);
printf("Inch: ");
scanf("%d", &distancesInput[i].inch);
}

distancesSum.feet = 0;
distancesSum.inch = 0;

for (int i = 0; i < numDistancesInput; ++i)


{
distancesSum.feet += distancesInput[i].feet;
distancesSum.inch += distancesInput[i].inch;
}

printf("Sum of distances: %d feet %d inch\n", distancesSum.feet,


distancesSum.inch);

return 0;
}
Output

Experiment 8
a)
// Problem Statement: Write a program in C to check whether two given strings
are an anagram.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char *sortString(char *str)


{
int n = strlen(str);
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (str[i] > str[j])
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
return str;
}

char *toLowerCase(char *str)


{
for (int i = 0; str[i]; i++)
{
str[i] = tolower(str[i]);
}
return str;
}

int main()
{
char x[100], y[100];

printf("Enter the first string: ");


fgets(x, sizeof(x), stdin);
if (x[strlen(x) - 1] == '\n')
{
x[strlen(x) - 1] = '\0';
}

printf("Enter the second string: ");


fgets(y, sizeof(y), stdin);
if (y[strlen(y) - 1] == '\n')
{
y[strlen(y) - 1] = '\0';
}

if (strlen(x) != strlen(y))
{
printf("Both strings are not anagrams.\n");
return 0;
}

toLowerCase(x);
toLowerCase(y);

sortString(x);
sortString(y);

if (strcmp(x, y) == 0)
{
printf("Both strings are anagrams.\n");
}
else
{
printf("Both strings are not anagrams.\n");
}

return 0;
}
Output

b)
Code
// Problem Statement: Write a program in C to check armstrong number between 1
to 500.

#include <stdio.h>
#include <math.h>

int main()
{
printf("List of armstrong numbers: ");
for (int i = 1; i <= 500; i++)
{
int sum = 0, n = i;
while (n > 0)
{
sum += pow((n % 10), 3);
n /= 10;
}
if (sum == i)
{
printf("%d \n", i);
}
}
printf("\n");
return 0;
}
Output

c)
Code

// 8.c: Write a program in C to check whether a number is a perfect number or not.

#include <stdio.h>
#include <math.h>

int main()
{
int n;
printf("Enter the number to be checked: ");
scanf("%d", &n);

int sum = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
sum += i;
}
if (sum == n)
{
printf("The entered number is a perfect number.");
}
else
{
printf("The entered number is not a perfect number. ");
}
return 0;
}

Output

You might also like