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

practical file c

C program question

Uploaded by

Raju Negi
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)
2 views

practical file c

C program question

Uploaded by

Raju Negi
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/ 21

Name: Mayank Gusain

Practical:1|Write a ‘C’ program to evaluate an expression ax2+bx+c. Roll no: 24010157

#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter the coefficient a (non-zero): ");
scanf("%f", &a);
if (a == 0) {
printf("Coefficient 'a' cannot be zero for a quadratic equation.\n");
return 1;
}
printf("Enter the coefficient b: ");
scanf("%f", &b);
printf("Enter the constant c: ");
scanf("%f", &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two real and distinct roots: %.2f and %.2f\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("The equation has one real and repeated root: %.2f\n", root1);
} else {
float realPart = -b / (2 * a);
float imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("The equation has complex roots: %.2f + %.2fi and %.2f - %.2fi\n",
realPart, imaginaryPart, realPart, imaginaryPart);
}

return 0;
}

Output:

Enter the coefficient a (non-zero): 1


Enter the coefficient b: -3
Enter the constant c: 2
The equation has two real and distinct roots: 2.00 and 1.00
Name: Mayank Gusain
Roll no: 24010157

Practical:2|Write a ‘C’ program to find the sum of first n even natural numbers using the formula
n(n+1).

#include<stdio.h>
int main()
{
int n,sum,i;
printf("Enter a natural number:");
scanf("%d",&n);
sum=n*(n+1);
printf("The sum of first %d even natural numbers is:%d",n,sum);
return 0;
}

Output:

Enter a natural number:5


The sum of first 5 even natural numbers is:30
Process returned 0 (0x0) execution time : 1.289 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:3|Write a ‘C’ program to check weather a year is leap year or not.

#include<stdio.h>
int main()
{
int year;
printf("Enter a year to check:");
scanf("%d",&year);
if(year%400==0 || (year%4==0 && year%100!=0))
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap year",year);
}
return 0;
}

Output:

Enter a year to check:2024


2024 is a leap year
Process returned 0 (0x0) execution time : 2.217 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157
Practical:4| Write a ‘C’ program to accept an input from user and check weather it is a character,digit or
special symbol using a switch-case.

#include<stdio.h>
int main()
{
char ch;
printf("Enter a Character:");
scanf("%c",&ch);
switch(1)
{
case 1:
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
printf("Given character is an Alphabet...");
break;
}
else if(ch>='0' && ch<='9')
{
printf("Given character is a digit...");
break;
}
else if(ch == ' ')
{
printf("Given character is an space...");
break;
}
else
{
printf("Given character is a special symbol...");
break;
}
default:printf("Invalid character...");
}
return 0;
}

Output:

Enter a Character:$
Given character is a special symbol...
Process returned 0 (0x0) execution time : 4.214 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:5|Write a ‘C’ program to input a month number and print the month name using switch-case.

#include<stdio.h>
int main()
{
int month;
printf("Enter the month number 1-12: ");
scanf("%d", &month);
switch(month)
{
case 1:
printf("It is January\n");
break;
case 2:
printf("It is February\n");
break;
case 3:
printf("It is March\n");
break;
case 4:
printf("It is April\n");
break;
case 5:
printf("It is May\n");
break;
case 6:
printf("It is June\n");
break;
case 7:
printf("It is July\n");
break;
case 8:
printf("It is August\n");
break;
case 9:
printf("It is September\n");
break;
case 10:
printf("It is October\n");
break;
case 11:
printf("It is November\n");
break;
case 12:
printf("It is December\n");
break;
default:
printf("Invalid month number! Please enter a number between 1 and 12.\n");
}

return 0;
}

Output:

Enter the month number 1-12: 8


It is August

Process returned 0 (0x0) execution time : 1.438 s


Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:6| Write a ‘C’ program to find the maximum and minimum numbers of an array of integers.

#include<stdio.h>
int main()
{
int arr[20],n,i,lar,sml;
printf("Enter the number of elements in an array max 20:");
scanf("%d",&n);
printf("Now, Enter %d elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
lar=arr[0];
sml=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]>lar)
{
lar=arr[i];
}
else if(arr[i]<sml)
{
sml=arr[i];
}
}
printf("Largest element in array is:%d\nSmallest element in array is:%d",lar,sml);
return 0;
}

Output:

Enter the number of elements in an array max 20:10


Now, Enter 10 elements:23 45 78 34 67 342 67 -56 4 -9
Largest element in array is:342
Smallest element in array is:-56
Process returned 0 (0x0) execution time : 21.445 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157
Practical:7|Write a ‘C’ program to find the transpose of a given matrix.

#include<stdio.h>

int main()
{
int rows,cols;
// Input number of rows and columns for the matrix
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols], transpose[cols][rows]; // Declare matrix and transpose
// Input matrix elements
printf("Enter elements of the matrix:\n");
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
printf("Element [%d][%d]: ", i+1, j+1);
scanf("%d",&matrix[i][j]);
}
}

// Calculating the transpose of the matrix


for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
transpose[j][i] = matrix[i][j];
}
}

// Printing the original matrix


printf("\nOriginal Matrix:\n");
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Printing the transpose of the matrix
printf("\nTranspose of the Matrix:\n");
for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
{
printf("%d ", transpose[i][j]);
}
printf("\n");
}

return 0;
}

Output:

Enter number of rows: 2


Enter number of columns: 3
Enter elements of the matrix:
Element [1][1]: 1
Element [1][2]: 2
Element [1][3]: 3
Element [2][1]: 4
Element [2][2]: 5
Element [2][3]: 6

Original Matrix:
123
456

Transpose of the Matrix:


14
25
36

Process returned 0 (0x0) execution time : 6.802 s


Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157
Practical:8|Write a ‘C’ program that finds the sum of diagonal elements of a given square matrix.

#include<stdio.h>
int main()
{
int mat[3][3];
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Element [%d][%d]: ", i+1, j+1);
scanf("%d",&mat[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
sum+=mat[i][j];
}
}
}
printf("\nSum of diagonal elements:%d",sum);
return 0;
}

Output:

Element [1][1]: 1
Element [1][2]: 2
Element [1][3]: 3
Element [2][1]: 4
Element [2][2]: 5
Element [2][3]: 6
Element [3][1]: 7
Element [3][2]: 8
Element [3][3]: 9
123
456
789

Sum of diagonal elements:15


Process returned 0 (0x0) execution time : 5.708 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157
Practical:9|Write a ‘C’ program to read an integer and find the sum of digits. Use a function for finding
the sum of digits.

#include<stdio.h>
int sum(n)
{
int rem,sum=0;
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n/=10;
}
return sum;
}

int main()
{
int n,result;
printf("Enter an integer number that consist of more than one digit:");
scanf("%d",&n);
result=sum(n);
printf("Sum of all digits in %d is:%d",n,result);
}

Output:

Enter an integer number that consists of more than one digit:852674


Sum of all digits in 852674 is:32
Process returned 0 (0x0) execution time : 8.486 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157
Practical:10|Write a ‘C’ program to convert Decimal number to Binary number. Use recursive function.

#include<stdio.h>
#include<math.h>
int decimalToBinary(int n, int count) {
if (n == 0) {
return 0;
}
int k = n % 2;
int c = pow(10, count);
return (k * c) + decimalToBinary(n / 2, count + 1);
}
int main() {
int n; // Input number
printf("Enter a decimal number:");
scanf("%d",&n);
int result = decimalToBinary(n, 0);
printf("Binary is:%d", result);
return 0;
}

Output:

Enter a decimal number:13


Binary is :1101
Process returned 0 (0x0) execution time : 1.784 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:11|Write a ‘C’ program to perform the following operations on a string.


a): Count the number of characters and digits.

#include<stdio.h>
int main()
{
char str[100];
int charCount=0,digitCount=0,i;
printf("Enter a string with characters and digits:");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))
{
charCount++;
}
else if(str[i]>='0' && str[i]<='9')
{
digitCount++;
}
}
printf("Total character in string:%d",charCount);
printf("\nTotal digits in string:%d",digitCount);
return 0;
}

Output:

Enter a string with characters and digits:My name is Abdur Rehman 9279183 & 2783496293
Total character in string:19
Total digits in string:17
Process returned 0 (0x0) execution time : 29.627 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:11|Write a ‘C’ program to perform the following operations on a string.


b):Check whether the given string is Palindrome or not.

#include<stdio.h>
int main()
{
char str[50],str2[50];
printf("Enter a string:");
gets(str);
strcpy(str2,str);
strrev(str2);
printf("Reverse string is:%s",str2);
if(strcmpi(str,str2)==0){
printf("\nString is Palindrome");
}
else{
printf("\nString is not Palindrome");
}
return 0;
}

Output:

Enter a string:level
Reverse string is:level
String is Palindrome
Process returned 0 (0x0) execution time : 8.326 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:12| Write a ‘C’ program to swap two numbers.[Demonstrate the difference between call by
value and call by reference for swapping two numbers].

#include <stdio.h>
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside swapByValue: a = %d, b = %d\n", a, b);
}
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
printf("Inside swapByReference: a = %d, b = %d\n", *a, *b);
}
int main() {
int x, y;
printf("Enter two different numbers: ");
scanf("%d %d", &x, &y);
printf("Before swapping: x = %d, y = %d\n", x, y);
swapByValue(x, y);
printf("After swapByValue: x = %d, y = %d\n", x, y);
swapByReference(&x, &y);
printf("After swapByReference: x = %d, y = %d\n", x, y);
return 0;
}

Output:

Enter two different numbers: 6 9


Before swapping: x = 6, y = 9
Inside swapByValue: a = 9, b = 6
After swapByValue: x = 6, y = 9
Inside swapByReference: a = 9, b = 6
After swapByReference: x = 9, y = 6

Process returned 0 (0x0) execution time : 3.516 s


Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:13| Write a ‘C’ program to store and retrieve the personal information about students –
Enrollment Number,Name,Address,City and State. Demonstrate nusing files.]

#include<stdio.h>
int main()
{
int enr_no,e;
char name[20],addr[50],city[20],state[20];
char n[20],a[50],c[20],s[20];
printf("Enter your Enrollment No:");
scanf("%d",&enr_no);
printf("Enter your Name:");
scanf("%s",&name);
printf("Enter your Adress:");
scanf("%s",&addr);
printf("Enter your City:");
scanf("%s",&city);
printf("Enter your State:");
scanf("%s",&state);
FILE *fp;
fp=fopen("personal_info.txt","w"); // To store data in personal_info.txt
fprintf(fp,"%d %s %s %s %s",enr_no,name,addr,city,state);
fclose(fp);
printf("Data stored Enter to retrieve the data from file\n");
getch();
fp=fopen("personal_info.txt","r");
fscanf(fp,"%d",&e);
fscanf(fp,"%s",&n);
fscanf(fp,"%s",&a);
fscanf(fp,"%s",&c);
fscanf(fp,"%s",&s);
fclose(fp);
printf("your Enrollment No is:%d",e);
printf("\nyour Name is :%s",n);
printf("\nyour Address is :%s",a);
printf("\nyour City is :%s",c);
printf("\nyour State is :%s",s);
return 0;
}

Output:

Enter your Enrollment No:241004


Enter your Name:Abdur
Enter your Adress:Sanpla
Enter your City:Deoband
Enter your State:Saharanpur
Data store
Press Enter to retrieve the data from file
your Enrollment No is:241004
your Name is :Abdur
your Address is :Sanpla
your City is :Deoband
your State is :Saharanpur
Process returned 0 (0x0) execution time : 26.742 s
Press any key to continue.
Name: Mayank Gusain
Roll no: 24010157

Practical:14|Write a ‘C’ program to find


a):The frequency of characters that are in a sentence stored in a text file.
The file named is to be supplied as a command-line argument.

#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
char *filename = argv[1];
char str[500], c;
int freq[256] = {0};
printf("Enter a sentence: ");
fgets(str, sizeof(str), stdin);
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error opening file '%s' for writing!\n", filename);
return 1;
}
fprintf(fp, "%s", str
fclose(fp);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file '%s' for reading!\n", filename);
return 1;
}
while ((c = fgetc(fp)) != EOF) {
freq[(unsigned char)c]++;
}
fclose(fp);
printf("\nCharacter Frequencies from '%s':\n", filename);
for (int i = 0; i < 256; i++) {
if (freq[i] > 0 && isprint(i)) {
printf("'%c' -> %d\n", i, freq[i]);
}
}

return 0;
}
Output:

Enter a sentence: Hello, World! This is a test sentence.


Character Frequencies:
' ' -> 7
',' -> 1
'!' -> 1
'H' -> 2
'T' -> 1
'a' -> 1
'c' -> 2
'd' -> 1
'e' -> 6
'h' -> 1
'i' -> 3
'l' -> 3
'n' -> 2
'o' -> 2
'r' -> 1
's' -> 5
't' -> 3
'w' -> 1
Name: Mayank Gusain
Roll no: 24010157

Practical:14|Write a ‘C’ program to find


b):TCount the number of words in a file
The file named is to be supplied as a command-line argument

#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
char *filename = argv[1];
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file '%s' for reading!\n", filename);
return 1;
}
int word_count = 0;
char c, prev = ' ';
while ((c = fgetc(fp)) != EOF) {
if (isspace(c) && !isspace(prev)) {
word_count++;
}
prev = c;
}
if (!isspace(prev)) {
word_count++;
}
fclose(fp);
printf("The number of words in '%s' is: %d\n", filename, word_count);
return 0;
}

Output:

Hello, World! This is a test sentence with multiple words.


The number of words in 'essay.txt' is: 10

You might also like