KEMBAR78
Navgujrat Computer College of Application | PDF | Pointer (Computer Programming) | Fahrenheit
0% found this document useful (0 votes)
37 views67 pages

Navgujrat Computer College of Application

Uploaded by

mrpatel05307
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)
37 views67 pages

Navgujrat Computer College of Application

Uploaded by

mrpatel05307
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/ 67

1

NAVGUJRAT COMPUTER COLLEGE OF


APPLICATION,USMANPURA
NAME: JADAV JAYDEEP MAHESHBHAI
ROLL NO:1080
BATCH: A2
COURSE NAME: C PROGRAMMING (PRACTICAL)
CODE: DSC-C-BCA-112 P

1. Write a program to calculate simple interest. Accept Principle amount, rate of interest
and duration in years from the user.

#include <stdio.h>

#include <conio.h>

void main()

int p,r,t,simpleinterest;

clrscr();

printf("\n Enter Principal::");

scanf("%d",&p);

printf("\n Enter rate::");

scanf("%d",&r);

printf("\n Enter time::");

scanf("%d",&t);

simpleinterest=(p*r*t)/100;

printf("\n your simple intrest is =%d",simpleinterest);

getch();

Created By: Jadav Jaydeep


2

2. Write a program to swap two variables by taking third variable and without taking third
variable.

#include <stdio.h>

#include <conio.h>

void main()

int a, b, temp;

clrscr();

printf("Enter Number of A:: ");

scanf("%d",&a);

printf("Enter Number of B:: ");

scanf("%d",&b);

printf("Before swapping: a = %d, b = %d\n", a, b);

temp = a;

a = b;

b = temp;

printf("After swapping: a = %d, b = %d\n", a, b);

getch();

3.Write a program to find greatest number from given 3 numbers from the user using
nested if and using conditional operator.

#include <stdio.h>

#include <conio.h>

void main()

int a, b, c;

clrscr();

Created By: Jadav Jaydeep


3

printf("Enter three numbers (a, b, c): ");

scanf("%d %d %d", &a, &b, &c);

if (a >= b)

if (a >= c)

printf("The greatest number is: %d\n", a);

else

printf("The greatest number is: %d\n", c);

else

if (b >= c)

printf("The greatest number is: %d\n", b);

else

printf("The greatest number is: %d\n", c);

getch();

Created By: Jadav Jaydeep


4

4.Write a program to convert temperature from Fahrenheit to Celsius and from Celsius to
Fahrenheit.

#include <stdio.h>

#include <conio.h>

void main()

float temperature;

float celsius;

float fahrenheit;

int choice;

printf("\nTemperature Conversion Menu::");

printf("\n1. Convert Fahrenheit to Celsius");

printf("\n2. Convert Celsius to Fahrenheit");

printf("\nEnter your choice (1 or 2):: ");

scanf("%d", &choice);

if (choice == 1)

printf("\nEnter temperature in Fahrenheit: ");

scanf("%f", &temperature);

celsius=(temperature - 32) * 5 / 9;

printf("\n%.2f Fahrenheit is equal to %.2f Celsius", temperature, celsius);

else if (choice == 2)

printf("\nEnter temperature in Celsius: ");

scanf("%f", &temperature);

fahrenheit = (temperature * 9 / 5) + 32;

Created By: Jadav Jaydeep


5

printf("\n%.2f Celsius is equal to %.2f Fahrenheit", temperature, fahrenheit);

else

printf("\nInvalid choice! Please enter 1 or 2.");

getch();

5.Demonstrate printing Table of given number using loop.

#include <stdio.h>

#include <conio.h>

void main()

int number, i;

printf("Enter a number to print its multiplication table: ");

scanf("%d", &number);

printf("Multiplication Table of %d:\n", number);

for (i = 1; i <= 10; i++)

printf("%d x %d = %d\n", number, i, number * i);

getch();

6.Demonstrate to find factorial of given number.

#include <stdio.h>

#include <conio.h>

void main()

Created By: Jadav Jaydeep


6

int number,i;

unsigned long long factorial = 1;

clrscr();

printf("\nEnter a non-negative integer: ");

scanf("%d", &number);

if(number<0)

printf("\nFactorial is not defined for negative numbers.");

else

for(i=1;i<=number;i++)

factorial *= i;

printf("\nFactorial of %d = %llu", number, factorial);

getch();

7.Demonstrate to find maximum from given N inputs by user.

#include <stdio.h>

#include <conio.h>

void main()

int n, i, number, max;

clrscr();

Created By: Jadav Jaydeep


7

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

scanf("%d", &n);

if(n<=0)

printf("\nPlease enter a positive integer for the number of inputs.");

printf("\nEnter number 1: ");

scanf("%d", &number);

max=number;

for (i=2;i<=n;i++)

printf("\nEnter number %d: ", i);

scanf("%d", &number);

if(number>max)

max=number;

printf("\nThe maximum value is: %d", max);

getch();

8.Demonstrate to find reverse of a given number.

#include <stdio.h>

#include <conio.h>

void main()

int number, reversedNumber = 0, remainder;

Created By: Jadav Jaydeep


8

int originalNumber = number;

clrscr();

printf("\nEnter an integer: ");

scanf("%d", &number);

number = (number < 0) ? -number : number;

while (number != 0)

remainder = number % 10;

reversedNumber = reversedNumber * 10 + remainder;

number /= 10;

if (originalNumber < 0)

reversedNumber = -reversedNumber;

printf("\nThe reverse of %d is: %d", originalNumber, reversedNumber);

getch();

9.Demonstrate to generate Fibonacci series up to N numbers.

#include <stdio.h>

#include <conio.h>

void main()

int n, i;

unsigned long long first = 0, second = 1, next;

clrscr();

printf("\nEnter the number of terms in the Fibonacci series: ");

Created By: Jadav Jaydeep


9

scanf("%d", &n);

if (n <= 0)

printf("Please enter a positive integer.\n");

else

printf("\nFibonacci Series: ");

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

if (i == 1)

printf("%llu, ", first);

continue;

if (i == 2)

printf("%llu, ", second);

continue;

next = first + second;

first = second;

second = next;

printf("%llu, ", next);

printf("\n");

Created By: Jadav Jaydeep


10

getch();

10.Demonstrate to find GCD and LCM of given 2 numbers.

Note: Help For Chat Gpt

#include <stdio.h>

#include <conio.h>

int gcd(int a, int b)

while (b != 0)

int temp = b;

b = a % b;

a = temp;

return a;

int lcm(int a, int b)

return (a * b) / gcd(a, b);

int main()

int num1, num2;

int gcdValue = gcd(num1, num2);

int lcmValue = lcm(num1, num2);

printf("Enter two integers: ");

Created By: Jadav Jaydeep


11

scanf("%d %d", &num1, &num2);

printf("GCD of %d and %d is: %d\n", num1, num2, gcdValue);

printf("LCM of %d and %d is: %d\n", num1, num2, lcmValue);

return 0;

11.Demonstrate to check whether given is Palindrome or not.

NOTE:50% ChatGpt use because I could not understand this programme……

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define MAX_LENGTH 100

int isPalindrome(char str[])

int left = 0;

int right = strlen(str) - 1;

clrscr();

while (left < right)

while (left < right && !isalnum(str[left]))

left++;

while (left < right && !isalnum(str[right]))

right--;

Created By: Jadav Jaydeep


12

if (tolower(str[left]) != tolower(str[right]))

return 0;

left++;

right--;

return 0;

int main()

char str[MAX_LENGTH];

clrscr();

printf("\nEnter a string: ");

fgets(str, sizeof(str), stdin);

str[strcspn(str, "\n")] = 0;

if (isPalindrome(str))

printf("\"%s\" is a palindrome.\n", str);

else

printf("\"%s\" is not a palindrome.\n", str);

return 0;

12.Demonstrate to check whether the given number is Prime or not.

Created By: Jadav Jaydeep


13

#include <stdio.h>

#include <conio.h>

void main()

int num, i, isPrime = 1;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

if (num <= 1)

isPrime = 0;

else

for (i = 2; i * i <= num; i++)

if (num % i == 0)

isPrime = 0;

break;

if (isPrime)

printf("%d is a prime number.\n", num);

Created By: Jadav Jaydeep


14

else

printf("%d is not a prime number.\n", num);

getch();

1.Print the following pyramid. [Left aligned, Right Aligned, Center Aligned]

**

***

****

#include <stdio.h>

#include <conio.h>

void main()

int i,n=4,j;

clrscr();

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

printf("\n");

for(j=1;j<=i;j++)

printf("*");

getch();

Created By: Jadav Jaydeep


15

2.To accept an integer N, if the integer N = 4, then print the pyramid :

121

12321

1234321

#include <stdio.h>

#include <conio.h>

void main()

int N,i,j;

clrscr();

printf("Enter an integer N: ");

scanf("%d", &N);

for (i = 1; i <= N; i++)

for (j = 1; j <= i; j++)

printf("%d", j);

for (j =i-1; j >= 1; j--)

printf("%d", j);

printf("\n");

getch();

3.A program which will take 10 numbers from user and stored it in the array. It will print all the
numbers, their sum and average of it.

#include <stdio.h>

#include <conio.h>

Created By: Jadav Jaydeep


16

void main()

int numbers[10],sum=0,i;

float average;

printf("\nEnter 10 numbers:");

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

printf("Number %d: ", i + 1);

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

sum += numbers[i];

printf("\n\nYou entered:");

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

printf("%d ", numbers[i]);

average = sum / 10.0;

printf("\n\nSum: %d", sum);

printf("\n\nAverage: %.2f", average);

getch();

4.Demonstrate to sort an array.

#include <stdio.h>

#include <conio.h>

void main()

int arr[] = {5, 2, 9, 1, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

int temp,i,j;

clrscr();

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

Created By: Jadav Jaydeep


17

for ( 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;

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

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

printf("\n");

getch();

5.Demonstrate to find addition of two matrices of 3*3.

#include <stdio.h>

#include <conio.h>

void main()

int a[3][3], b[3][3], sum[3][3],i,j;

clrscr();

printf("Enter elements of first 3x3 matrix:\n");

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

for (j = 0; j < 3; j++)

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

printf("Enter elements of second 3x3 matrix:\n");

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

Created By: Jadav Jaydeep


18

for (j = 0; j < 3; j++)

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

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

for ( j = 0; j < 3; j++)

sum[i][j] = a[i][j] + b[i][j];

printf("Sum of the two matrices:\n");

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

for ( j = 0; j < 3; j++)

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

printf("\n");

getch();

6.Demonstrate to find multiplication of two matrices of 3*3.

#include <stdio.h>

#include <conio.h>

void main()

int a[3][3], b[3][3], product[3][3] = {0},i,j,k;

clrscr();

printf("Enter elements of first 3x3 matrix:\n");

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

for (j = 0; j < 3; j++)

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

printf("Enter elements of second 3x3 matrix:\n");

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

for ( j = 0; j < 3; j++)

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

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

for ( j = 0; j < 3; j++)

Created By: Jadav Jaydeep


19

for ( k = 0; k < 3; k++)

product[i][j] += a[i][k] * b[k][j];

printf("Product of the two matrices:\n");

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

for ( j = 0; j < 3; j++)

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

printf("\n");

getch();

7.Input a string from the user and check whether the string is palindrome or not.

#include <stdio.h>

#include <string.h>

#include <conio.h>

void main()

char str[100];

int len, isPalindrome = 1,i;

clrscr();

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

str[strcspn(str, "\n")] = 0;

len = strlen(str);

for ( i = 0; i < len / 2; i++)

if (str[i] != str[len - i - 1])

isPalindrome = 0;

break;

Created By: Jadav Jaydeep


20

if (isPalindrome)

printf("The string is a palindrome.\n");

else

printf("The string is not a palindrome.\n");

getch();

8.Demonstrate to print factorial of a given number by recursive user defined function fact(int).

#include <stdio.h>

#include <conio.h>

int fact(int n)

if (n == 0)

return 1;

else

return n * fact(n - 1);

void main()

int number;

clrscr();

printf("Enter a number: ");

scanf("%d", &number);

printf("Factorial of %d is %d\n", number, fact(number));

getch();

9.Demonstrate to convert lowercase string to uppercase string (without including string.h).

#include <stdio.h>

#include <conio.h>

void main()

Created By: Jadav Jaydeep


21

char str[100];

int i;

clrscr();

printf("Enter a lowercase string: ");

fgets(str, sizeof(str), stdin);

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

if (str[i] >= 'a' && str[i] <= 'z')

str[i] = str[i] - ('a' - 'A');

printf("Uppercase string: %s", str);

getch();

10.Take a lowercase string from the user and print its length and uppercase string.

#include <stdio.h>

#include <conio.h>

void main()

char str[100];

int length = 0,i;

clrscr();

printf("Enter a lowercase string: ");

fgets(str, sizeof(str), stdin);

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

length++;

if (str[i] >= 'a' && str[i] <= 'z')

str[i] = str[i] - ('a' - 'A');

Created By: Jadav Jaydeep


22

printf("Length of the string: %d\n", length - 1);

printf("Uppercase string: %s", str);

getch();

11.A program that uses function digit(n, k) that return the value of the kth digit the right of the
number N. For e.g. The function call digit (254693, 2) should return 9.

#include <stdio.h>

#include <conio.h>

int digit(int n, int k)

while (k-- > 1)

n /= 10;

return n % 10;

void main()

int n = 254693, k = 2;

clrscr();

printf("The %d-th digit from the right of %d is: %d\n", k, n, digit(n, k));

getch();

12.Demonstrate to find if the given no. is prime or not. The function should accept the number as
an argument and return if the no. is prime or not.

1.Define structure called state having member variables as

state name, number of districts and total population. Input and display 5 State data.

Created By: Jadav Jaydeep


23

#include <stdio.h>

#include <conio.h>

struct state

char name[50];

int districts;

long population;

};

int main()

struct state states[5];

int i;

clrscr();

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

printf("Enter name of state %d: ", i + 1);

scanf("%s", states[i].name);

printf("Enter number of districts: ");

scanf("%d", &states[i].districts);

printf("Enter total population: ");

scanf("%ld", &states[i].population);

printf("\nState Data:\n");

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

printf("\nState: %s\n", states[i].name);

printf("Districts: %d\n", states[i].districts);

printf("Population: %ld\n", states[i].population);

Created By: Jadav Jaydeep


24

return 0;

2.Define a structure called Item having member variables: Item code, Item name, item price.
Create an array of structure to store five Items. Create a function which accepts the Item array and
modifies each element with an increase of 10% in the price.

#include<stdio.h>

#include<conio.h>

struct item

int code;

char name[10];

int price;

int qty;

int totalvalue;

}A[10];

void main()

int i,n;

clrscr();

printf("\nHow many items are there ?");

scanf("%d",&n);

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

A[i].code = i+1;

printf("\nEnter item name ::");

scanf("%s",A[i].name);

printf("\nEnter item Quantity :: ");

Created By: Jadav Jaydeep


25

scanf("%d",&A[i].qty);

printf("\nEnter item price ::");

scanf("%d",&A[i].price);

A[i].totalvalue = A[i].qty * A[i].price ;

printf("\n=========================================================================
====");

printf("\nCode\tName\t\t Qty\t Price\t Total");

printf("\n=========================================================================
====");

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

printf("\n%02d\t%-10s\t%4d\t%5d\t%8d",A[i].code,A[i].name,A[i].qty,A[i].price,A[i].totalvalue);

printf("\n=========================================================================
====");

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

A[i].price = A[i].price * 1.1;

A[i].totalvalue = A[i].qty * A[i].price;

printf("\nAfter 10%% increment in price data are....");

printf("\n=========================================================================
====");

printf("\nCode\tName\t\t Qty\t Price\t Total");

printf("\n=========================================================================
====");

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

Created By: Jadav Jaydeep


26

printf("\n%02d\t%-10s\t%4d\t%5d\t%8d",A[i].code,A[i].name,A[i].qty,A[i].price,A[i].totalvalue);

printf("\n=========================================================================
====");

getch();

3.Define a structure to represent a date. Use your structures that accept two different dates in the
format mm dd of the same year. Write a C program to display the month names of both dates.

#include <stdio.h>

#include <conio.h>

struct Date

int month;

int day;

};

const char* getMonthName(int month)

const char* months[] =

"Invalid", "January", "February", "March", "April", "May",

"June", "July", "August", "September", "October",

"November", "December"

};

if (month >= 1 && month <= 12)

return months[month];

else

return "Invalid";

Created By: Jadav Jaydeep


27

void main()

struct Date date1, date2;

printf("Enter the first date (mm dd): ");

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

printf("Enter the second date (mm dd): ");

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

printf("The month of the first date is: %s\n", getMonthName(date1.month));

printf("The month of the second date is: %s\n", getMonthName(date2.month));

getch();

4.Define a structure that can describe a Hotel. It should have members that include name, address,
grade, room charges, grade and no of rooms. Write a function to print out all hotel details with
room charges less than a given value.

#include <stdio.h>

#include <conio.h>

struct Hotel

char name[50];

char address[100];

int grade;

float roomCharges;

int numRooms;

};

void main()

Created By: Jadav Jaydeep


28

struct Hotel hotels[3];

int i,n;

float maxCharge;

clrscr();

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

printf("Enter Hotel %d name: ", i + 1);

scanf(" %[^\n]", hotels[i].name);

printf("Enter Hotel %d address: ", i + 1);

scanf(" %[^\n]", hotels[i].address);

printf("Enter Hotel %d grade: ", i + 1);

scanf("%d", &hotels[i].grade);

printf("Enter Hotel %d room charges: ", i + 1);

scanf("%f", &hotels[i].roomCharges);

printf("Enter Hotel %d number of rooms: ", i + 1);

scanf("%d", &hotels[i].numRooms);

printf("Enter the maximum room charge to filter: ");

scanf("%f", &maxCharge);

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

if (hotels[i].roomCharges < maxCharge)

printf("\nHotel Name: %s\n", hotels[i].name);

printf("Address: %s\n", hotels[i].address);

printf("Grade: %d\n", hotels[i].grade);

printf("Room Charges: %.2f\n", hotels[i].roomCharges);

printf("Number of Rooms: %d\n", hotels[i].numRooms);

Created By: Jadav Jaydeep


29

getch();

5.Accept records of different states using array of structures. The structure should contain state
and number of engineering colleges, medical colleges, management colleges and universities.
Calculate total colleges and display the state, which is having highest number of colleges.

#include <stdio.h>

#include <conio.h>

struct State

char name[50];

int engineering_colleges;

int medical_colleges;

int management_colleges;

int universities;

};

int main()

int n, i, max_state_index = 0;

int total_colleges, max_colleges = 0;

struct State states[n];

printf("Enter number of states: ");

scanf("%d", &n);

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

printf("\nEnter information for state %d\n", i + 1);

printf("State Name: ");

scanf(" %[^\n]", states[i].name);

printf("Number of Engineering Colleges: ");

Created By: Jadav Jaydeep


30

scanf("%d", &states[i].engineering_colleges);

printf("Number of Medical Colleges: ");

scanf("%d", &states[i].medical_colleges);

printf("Number of Management Colleges: ");

scanf("%d", &states[i].management_colleges);

printf("Number of Universities: ");

scanf("%d", &states[i].universities);

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

total_colleges = states[i].engineering_colleges + states[i].medical_colleges +

states[i].management_colleges + states[i].universities;

if (total_colleges > max_colleges)

max_colleges = total_colleges;

max_state_index = i;

printf("\nState with the highest number of colleges: %s\n", states[max_state_index].name);

printf("Total Colleges: %d\n", max_colleges);

return 0;

6.Define a structure by name time with members seconds, minutes and hours. If time1 and time2
are two variables of the structure type, write a program to find the difference in time using a
function.

#include <stdio.h>

#include <conio.h>

struct time

Created By: Jadav Jaydeep


31

int hours;

int minutes;

int seconds;

};

struct time timeDifference(struct time t1, struct time t2)

struct time diff;

int totalSeconds1 = t1.hours * 3600 + t1.minutes * 60 + t1.seconds;

int totalSeconds2 = t2.hours * 3600 + t2.minutes * 60 + t2.seconds;

int diffSeconds = totalSeconds1 - totalSeconds2;

diff.hours = diffSeconds / 3600;

diffSeconds %= 3600;

diff.minutes = diffSeconds / 60;

diff.seconds = diffSeconds % 60;

return diff;

int main()

struct time time1, time2, result;

printf("Enter time1 (hours minutes seconds): ");

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

printf("Enter time2 (hours minutes seconds): ");

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

Created By: Jadav Jaydeep


32

result = timeDifference(time1, time2);

printf("Time difference: %d hours %d minutes %d seconds\n", result.hours, result.minutes,


result.seconds);

return 0;

7.Declare structure having state name, population, literacy rate and per capita income. Input 5
records. Display the state whose literacy rate is highest and whose per capita income is highest.

#include <stdio.h>

#include <string.h>

struct State

char name[50];

int population;

float literacyRate;

float perCapitaIncome;

};

void main()

struct State states[5];

int i;

int highestLiteracyIndex = 0, highestIncomeIndex = 0;

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

printf("Enter details for state %d:\n", i+1);

printf("State name: ");

Created By: Jadav Jaydeep


33

getchar();

fgets(states[i].name, sizeof(states[i].name), stdin);

states[i].name[strcspn(states[i].name, "\n")] = 0;

printf("Population: ");

scanf("%d", &states[i].population);

printf("Literacy rate (in percentage): ");

scanf("%f", &states[i].literacyRate);

printf("Per capita income: ");

scanf("%f", &states[i].perCapitaIncome);

for (i = 1; i < 5; i++)

if (states[i].literacyRate > states[highestLiteracyIndex].literacyRate)

highestLiteracyIndex = i;

for (i = 1; i < 5; i++)

if (states[i].perCapitaIncome > states[highestIncomeIndex].perCapitaIncome)

highestIncomeIndex = i;

printf("\nState with the highest literacy rate:\n");

Created By: Jadav Jaydeep


34

printf("State: %s\n", states[highestLiteracyIndex].name);

printf("Literacy Rate: %.2f%%\n", states[highestLiteracyIndex].literacyRate);

printf("\nState with the highest per capita income:\n");

printf("State: %s\n", states[highestIncomeIndex].name);

printf("Per Capita Income: %.2f\n", states[highestIncomeIndex].perCapitaIncome);

getch();

8.Define a structure employee with member variables having employee name, basic pay, dearness
allowance, house rent, and net salary. Store data of 5 employees. Write a function which calculates
the net salary of employees and prints all employee details in descending order of their net salary.

#include <stdio.h>

#include <string.h>

struct Employee

char name[50];

float basicPay;

float dearnessAllowance;

float houseRent;

float netSalary;

};

void calculateNetSalary(struct Employee *e)

e->netSalary = e->basicPay + e->dearnessAllowance + e->houseRent;

void sortEmployeesBySalary(struct Employee employees[], int n)

struct Employee temp;

Created By: Jadav Jaydeep


35

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

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

if (employees[j].netSalary < employees[j+1].netSalary)

temp = employees[j];

employees[j] = employees[j+1];

employees[j+1] = temp;

void printEmployeeDetails(struct Employee employees[], int n)

printf("Employee Details:\n");

printf("---------------------------------------------------\n");

printf("Name\t\tBasic Pay\tDA\tHR\tNet Salary\n");

printf("---------------------------------------------------\n");

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

printf("%s\t\t%.2f\t%.2f\t%.2f\t%.2f\n",

employees[i].name, employees[i].basicPay,

employees[i].dearnessAllowance, employees[i].houseRent,

employees[i].netSalary);

int main()

Created By: Jadav Jaydeep


36

struct Employee employees[5];

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

printf("Enter details for employee %d\n", i+1);

printf("Name: ");

getchar();

fgets(employees[i].name, sizeof(employees[i].name), stdin);

employees[i].name[strcspn(employees[i].name, "\n")] = '\0';

printf("Basic Pay: ");

scanf("%f", &employees[i].basicPay);

printf("Dearness Allowance: ");

scanf("%f", &employees[i].dearnessAllowance);

printf("House Rent: ");

scanf("%f", &employees[i].houseRent);

calculateNetSalary(&employees[i]);

sortEmployeesBySalary(employees, 5);

printEmployeeDetails(employees, 5);

return 0;

9.Define a structure with tag population with fields Men and Women. Create structure with in
structure using state and population structure. Read and display the data.

#include <stdio.h>

struct Population

Created By: Jadav Jaydeep


37

int men;

int women;

};

struct State

char name[50];

struct Population population;

};

int main()

struct State state;

printf("Enter state name: ");

fgets(state.name, sizeof(state.name), stdin);

printf("Enter the number of men: ");

scanf("%d", &state.population.men);

printf("Enter the number of women: ");

scanf("%d", &state.population.women);

printf("\nState: %s", state.name);

printf("Men: %d\n", state.population.men);

printf("Women: %d\n", state.population.women);

return 0;

10.Demonstrate the use of Union.

#include <stdio.h>

union Data

int i;

Created By: Jadav Jaydeep


38

float f;

char c;

};

void main()

union Data data;

clrscr();

data.i = 10;

printf("data.i = %d\n", data.i);

data.f = 3.14;

printf("data.f = %.2f\n", data.f);

data.c = 'A';

printf("data.c = %c\n", data.c);

printf("\nAfter overwriting:\n");

printf("data.i = %d\n", data.i);

printf("data.f = %.2f\n", data.f);

printf("data.c = %c\n", data.c);

getch();

1.Demonstrate a user-defined function which will swap the

values of two variables using pointer (Call by reference).

#include <stdio.h>

void swap(int *a, int *b)

int temp = *a;

Created By: Jadav Jaydeep


39

*a = *b;

*b = temp;

void main()

int x, y;

printf("Enter value for x: ");

scanf("%d", &x);

printf("Enter value for y: ");

scanf("%d", &y);

printf("\nBefore swapping: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("\nAfter swapping: x = %d, y = %d\n", x, y);

getch();

2.Demonstrate a user defined function calc(), which will return the sum, subtraction,
multiplication, and division values of two variable locally declared in the main function use of
pointer.

#include <stdio.h>

#include <conio.h>

void calc(int *a, int *b, int *sum, int *sub, int *mul, float *div)

*sum = *a + *b;

Created By: Jadav Jaydeep


40

*sub = *a - *b;

*mul = *a * *b;

if (*b != 0)

*div = (float)(*a) / *b;

else

*div = 0;

void main()

int x = 10, y = 5;

int sum, sub, mul;

float div;

clrscr();

calc(&x, &y, &sum, &sub, &mul, &div);

printf("Sum: %d\n", sum);

printf("Subtraction: %d\n", sub);

printf("Multiplication: %d\n", mul);

printf("Division: %.2f\n", div);

getch();

3.Demonstrate UDF which will return the length of the string declared locally in the main (use
pointer).

#include <stdio.h>

#include <conio.h>

int stringLength(char *str)

Created By: Jadav Jaydeep


41

int length = 0;

while (*str != '\0')

length++;

str++;

return length;

void main()

char str[] = "Hello, World!";

int len = stringLength(str);

clrscr();

printf("Length of the string: %d\n", len);

getch();

4.Write a program in C to find the maximum number between two numbers using a pointer.

#include <stdio.h>

#include <conio.h>

int max(int *a, int *b)

return (*a > *b) ? *a : *b;

void main()

int num1 = 10, num2 = 20;

printf("The maximum number is: %d\n", max(&num1, &num2));

getch();

Created By: Jadav Jaydeep


42

5.Write a C program to input and print array elements using pointer.

#include <stdio.h>

#include <conio.h>

int main()

int arr[5];

int *ptr = arr;

int i;

printf("Enter 5 integers:\n");

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

scanf("%d", ptr + i);

printf("Array elements are:\n");

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

printf("%d ", *(ptr + i));

getch();

6.Write a C program to copy one array to another using pointers.

#include <stdio.h>

#include <conio.h>

void main()

int arr1[5], arr2[5];

int *ptr1 = arr1, *ptr2 = arr2;

int i;

Created By: Jadav Jaydeep


43

clrscr();

printf("Enter 5 integers for the first array:\n");

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

scanf("%d", ptr1 + i);

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

*(ptr2 + i) = *(ptr1 + i);

printf("Elements of the second array (copied from the first array):\n");

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

printf("%d ", *(ptr2 + i));

getch();

7.Write a C program to compare two strings using pointers.

#include <stdio.h>

#include <conio.h>

void main()

char str1[100], str2[100];

char *ptr1 = str1, *ptr2 = str2;

printf("Enter the first string: ");

gets(str1);

printf("Enter the second string: ");

gets(str2);

while (*ptr1 && (*ptr1 == *ptr2))

Created By: Jadav Jaydeep


44

ptr1++;

ptr2++;

if (*ptr1 == *ptr2)

printf("Strings are equal.\n");

else

printf("Strings are not equal.\n");

getch();

8.Write a C program to find reverse of a string using pointers.

#include <stdio.h>

#include <conio.h>

void main()

char str[100], *ptr1, *ptr2, temp;

int length = 0,i;

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

ptr1 = str;

while (*ptr1 != '\0' && *ptr1 != '\n')

length++;

ptr1++;

Created By: Jadav Jaydeep


45

ptr2 = str + length - 1;

for ( i = 0; i < length / 2; i++)

temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

ptr1++;

ptr2--;

printf("Reversed string: %s\n", str);

getch();

9.Write a C program to sort array using pointers.

#include <stdio.h>

#include <conio.h>

void sort(int *arr, int n,int i,int j,int temp)

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

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

if (*(arr + i) > *(arr + j))

temp = *(arr + i);

*(arr + i) = *(arr + j);

*(arr + j) = temp;

Created By: Jadav Jaydeep


46

void main()

int arr[] = {32, 45, 12, 56, 7};

int n = sizeof(arr) / sizeof(arr[0]),i;

printf("Original array: ");

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

printf("%d ", *(arr + i));

printf("\n");

printf("Sorted array: ");

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

printf("%d ", *(arr + i));

printf("\n");

getch();

10.Write a C program to concatenate two strings using pointers.

#include <stdio.h>

#include <conio.h>

void concatenate(char *str1, char *str2)

while (*str1)

str1++;

Created By: Jadav Jaydeep


47

while (*str2)

*str1 = *str2;

str1++;

str2++;

*str1 = '\0';

void main()

char str1[100] = "Hello, ";

char str2[] = "World!";

printf("Original strings:\n");

printf("str1: %s\n", str1);

printf("str2: %s\n", str2);

concatenate(str1, str2);

printf("Concatenated string: %s\n", str1);

getch();

1.Demonstrate printing Tables for the range given by user. E.g. start no : 1, ending no: 10 Program
should print table(s) of 1 to 10 numbers.

#include <stdio.h>

#include <conio.h>

void main()

Created By: Jadav Jaydeep


48

int start,end,i,j;

printf("Enter the start number: ");

scanf("%d", &start);

printf("Enter the end number: ");

scanf("%d", &end);

for (i = start; i <= end; i++)

printf("\nMultiplication Table for %d:\n", i);

for (j = 1; j <= 10; j++)

printf("%d x %d = %d\n", i, j, i * j);

getch();

2.Demonstrate to find sum of N numbers.

#include <stdio.h>

#include <conio.h>

void main()

int N, sum = 0,i;

clrscr();

printf("Enter the value of N: ");

scanf("%d", &N);

for (i = 1; i <= N; i++)

sum += i;

Created By: Jadav Jaydeep


49

printf("The sum of the first %d numbers is: %d\n", N, sum);

getch();

3.Demonstrate to find sum of the digits entered by the user.

#include <stdio.h>

#include <conio.h>

void main()

int num, sum = 0;

printf("Enter a number: ");

scanf("%d", &num);

while (num != 0)

sum += num % 10;

num /= 10;

printf("Sum of digits: %d\n", sum);

getch();

4.Demonstrate to find the sum of first 100 odd nos. and even nos.

#include <stdio.h>

#include <conio.h>

void main()

int sum_odd = 0, sum_even = 0;

int i;

clrscr();

for (i = 1; i <= 200; i += 2)

Created By: Jadav Jaydeep


50

sum_odd += i;

for (i = 2; i <= 200; i += 2)

sum_even += i;

printf("Sum of first 100 odd numbers: %d\n", sum_odd);

printf("Sum of first 100 even numbers: %d\n", sum_even);

getch();

5.Demonstrate to check whether the given number is Armstrong or not.

#include <stdio.h>

#include <math.h>

void main()

int num, temp, remainder, sum = 0, digits = 0;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

temp = num;

while (temp != 0)

temp /= 10;

digits++;

temp = num;

while (temp != 0)

remainder = temp % 10;

Created By: Jadav Jaydeep


51

sum += pow(remainder, digits);

temp /= 10;

if (sum == num)

printf("%d is an Armstrong number.\n", num);

else

printf("%d is not an Armstrong number.\n", num);

getch();

6.Demonstrate to print all the prime numbers ranging from 50 to 100.

#include <stdio.h>

#include <conio.h>

void main()

int num, i, isPrime;

for (num = 50; num <= 100; num++)

isPrime = 1;

for (i = 2; i * i <= num; i++)

if (num % i == 0)

isPrime = 0;

break;

Created By: Jadav Jaydeep


52

if (isPrime && num > 1)

printf("%d ", num);

getch();

7.Demonstrate to find x1+x2+x3+x4+ ….+ xn.

#include <stdio.h>

#include <conio.h>

void main()

int n, sum = 0, x,i;

clrscr();

printf("Enter the number of terms (n): ");

scanf("%d", &n);

printf("Enter %d terms:\n", n);

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

scanf("%d", &x);

sum += x;

printf("The sum of the series is: %d\n", sum);

getch();

8.Demonstrate to find 1+1/2+1/3+1/4+ …+1/n.

#include <stdio.h>

#include <conio.h>

Created By: Jadav Jaydeep


53

void main()

int n,i;

double sum = 0.0;

clrscr();

printf("Enter the value of n: ");

scanf("%d", &n);

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

sum += 1.0 / i;

printf("The sum of the series is: %.6f\n", sum);

getch();

9.To accept an integer N, if the integer N =4, then print the pyramid.

#include <stdio.h>

#include <conio.h>

void main()

int i,n=4,j;

clrscr();

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

printf("\n");

for(j=1;j<=i;j++)

printf("*");

getch();

Created By: Jadav Jaydeep


54

10.Print the following Pattern:

B C

D E F

GH I J

#include <stdio.h>

#include <conio.h>

void main()

int n = 4,i,j;

char ch = 'A';

clrscr();

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

for ( j = 1; j <= i; j++)

printf("%c ", ch);

ch++;

printf("\n");

getch();

11.Print the following pattern:

ABA

ABCBA

ABCDCBA

#include <stdio.h>

#include <conio.h>

Created By: Jadav Jaydeep


55

void main()

int i, j, n = 4;

clrscr();

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

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

printf("%c", 'A' + j);

for(j = i - 2; j >= 0; j--)

printf("%c", 'A' + j);

printf("\n");

getch();

12.Print the following pattern:

01

010

1010

#include <stdio.h>

#include <conio.h>

void main()

int i, j, n = 4;

clrscr();

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

Created By: Jadav Jaydeep


56

for(j = 1; j <= i; j++)

if((i + j) % 2 == 0)

printf("1 ");

else

printf("0 ");

printf("\n");

getch();

13.Display this kind of output on screen.

CP

CPR

CPRO

CPROG

CPROGR

CPROGRA

CPROGRAM

CPROGRAMI

CPROGRAMIN

CPROGRAMING

CPROGRAMIN

CPROGRAMI

CPROGRAM

CPROGRA

Created By: Jadav Jaydeep


57

CPROGR

CPROG

CPRO

CPR

CP

#include <stdio.h>

#include <string.h>

void main()

char str[] = "CPROGRAMING";

int len = strlen(str),i,j;

for(i = 1; i <= len; i++)

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

printf("%c", str[j]);

printf("\n");

for( i = len - 1; i >= 1; i--)

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

printf("%c", str[j]);

printf("\n");

getch();

14.Demonstrate to search an element from the array.

Created By: Jadav Jaydeep


58

#include <stdio.h>

#include <conio.h>

int main()

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int key, i;

printf("Enter the element to search: ");

scanf("%d", &key);

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

if(arr[i] == key)

printf("Element %d found at index %d.\n", key, i);

return 0;

printf("Element %d not found in the array.\n", key);

return 0;

15.Demonstrate to find sum, average of two numbers passed to user defined functions called sum
and average.

#include <stdio.h>

#include <conio.h>

int sum(int a, int b)

return a + b;

float average(int a, int b)

Created By: Jadav Jaydeep


59

return (a + b) / 2.0;

int main()

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Sum: %d\n", sum(num1, num2));

printf("Average: %.2f\n", average(num1, num2));

return 0;

16.Demonstrate to print Fibonacci series using recursive UDF.

#include <stdio.h>

#include <conio.h>

int fibonacci(int n)

if (n <= 1)

return n;

else

return fibonacci(n - 1) + fibonacci(n - 2);

int main()

int n,i;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

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

printf("%d ", fibonacci(i));

Created By: Jadav Jaydeep


60

return 0;

17.Demonstrate to find length of the given string (without including string.h).

#include <stdio.h>

#include <conio.h>

int stringLength(char str[])

int length = 0;

while (str[length] != '\0')

length++;

return length;

int main()

char str[100];

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

printf("Length of the string: %d\n", stringLength(str));

return 0;

18.Accept two strings from the user and print the message that the strings are same or not.

#include <stdio.h>

#include <string.h>

void main()

char str1[100], str2[100];

clrscr();

printf("Enter the first string: ");

gets(str1);

Created By: Jadav Jaydeep


61

printf("Enter the second string: ");

gets(str2);

if (strcmp(str1, str2) == 0)

printf("The strings are the same.\n");

else

printf("The strings are not the same.\n");

getch();

Created By: Jadav Jaydeep


62

Created By: Jadav Jaydeep


63

Created By: Jadav Jaydeep


64

Created By: Jadav Jaydeep


65

Created By: Jadav Jaydeep


66

Created By: Jadav Jaydeep


67

Created By: Jadav Jaydeep

You might also like