Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

1.

C program to find largest and smallest of three numbers


#include<stdio.h>
int main()
{
int n1, n2, n3;
printf("ENTER FIRST NUMBER A :: ");
scanf("%d", &n1);
printf("\nENTER SECOND NUMBER B :: ");
scanf("%d",&n2);
printf("\nENTER THIRD NUMBER C :: ");
scanf("%d",&n3);
printf("\nTHE BIGGEST NUMBER IS :: ");
if( (n1 > n2) && (n1 > n3) )
printf("%d", n1);
else if(n2 > n3)
printf("%d", n2);
else
printf("%d", n3);
printf("\n\nTHE SMALlEST NUMBER IS :: ");
if( (n1 < n2) && (n1 < n3) )
printf("%d", n1);
else if(n2 < n3)
printf("%d", n2);
else
printf("%d",n3);
return 0;
}
Output:
ENTER FIRST NUMBER A:: 4

ENTER SECOND NUMBER B:: 7


ENTER THIRD NUMBER C:: 9

THE BIGGEST NUMBER IS:: 9

THE SMALlEST NUMBER IS:: 4

2. Checking for an Armstrong number using if, if-else

#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}

Output:
Enter the number=153
armstrong number

Enter the number=5


not armstrong number

3. Solving Quadratic equations using switch statement


#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
printf("Enter values of a, b, c of quadratic equation (ax^2 + bx + c): ");
scanf("%f%f%f", &a, &b, &c);
discriminant = (b * b) - (4 * a * c);
switch(discriminant > 0)
{
case 1:
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct and real roots: %.2f and %.2f", root1, root2);
break;
case 0:
switch(discriminant < 0)
{
case 1:
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f", root1, imaginary, root2,
imaginary);
break;
case 0:
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
break;
}
}
return 0;
}
Output:
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 4 -2 -10
Two distinct and real roots exists: 1.85 and -1.35

4. Finding the area of different shapes using switch statement.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int choice;
clrscr();
printf("1. For are of triangle");
printf("2. For area of circle");
printf("3. For area of ractangle");
printf("4. For are of square");
printf(" Enter your choice:n");
scanf("%d",choice);
{
case 1:
{
int b,h,a;
printf("Enter base and height:");
scanf("%d%d",&b,&h);
a=(b*h)/2;
printf("The aree of triangle=%d",a);
break;
}
case 2:
{
float r,a;
printf("Enter readius");
scanf("%g",&r);
a=3.14594*r*r;
printf("n area of circle=%g",a);
break;
}
case 3:
{
int l,b,a;
printf("Enter lenght and breadth:");
scanf("%d%d",&l, &b);
a=l*b;
printf("Area of rectangle:%dn",a);
break;
}
case 4:
{
int a,b;
printf("Enter lenght");
scanf("%d",&a);
b=a*a;
printf("The are of square:%d",b);
break;
}
default:
{
printf("Wrong Choice");
}
getch();
}

5. Ascending and descending order of numbers using arrays.(Largest and smallest numbers)
#include <stdio.h>
#include<conio.h>
int main()
{
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] > a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] < a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nDescending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
return 0;
getch();
}

Output:
Array size: 10
Elements : 3 4 7 6 5 1 2 8 10 9
Ascending : 1 2 3 4 5 6 7 8 9 10
Descending : 10 9 8 7 6 5 4 3 2 1
6. Sorting of names in alphabetical order.
#include<stdio.h>
#include<string.h>
main(){
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :\n");
scanf("%d",&n);
printf("Enter names in any order:\n");
for(i=0;i<n;i++){
scanf("%s",str[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("\nThe sorted order of names are:\n");
for(i=0;i<n;i++){
printf("%s\n",str[i]);
}
}
Output:
Enter number of names:
5
Enter names in any order:
Pinky
Lucky
Ram
Appu
Bob
The sorted order of names is:
Appu
Bob
Lucky
Pinky
Ram

7. Program to search the given element by using linear search.


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

int i, keynum, found = 0;


printf("Enter the number of elements ");
scanf("%d", &num);
int array[num];
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the element to be searched ");
scanf("%d", &keynum);
/* Linear search begins */
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
}

Output:
1. Enter the number of elements 6
Enter the elements one by one
4
6
1
2
5
3
Enter the element to be searched 6
Element is present in the array at position 2

2. Enter the number of elements 3


Enter the elements one by one
66
-3
31
Enter the element to be searched 66
Element is present in the array at position 1
3. Enter the number of elements 5
Enter the elements one by one
1
3
6
1
9
Enter the element to be searched 9
Element is present in the array at position 5

8. Matrix operations i) Addition ii) Subtraction iii) Multiplication iv) Transpose


i)Addition
#include <stdio.h>
int main()
{
//fill your code
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat2[i][j]);
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
printf(“%d “, mat3[i][j]);
printf(“\n”);
}
return 0;
}
Output:
2 2 (order of the matrix)

1 2 3 4 (matrix 1 elements)

2 3 4 5 (matrix 2 elements)

3 5 (resultant matrix)

79

ii) Subtraction
#include <stdio.h>
int main()
{
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat2[i][j]);
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] – mat2[i][j];
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
printf(“%d “, mat3[i][j]);
printf(“\n”);
}
return 0;
}
Output:
2 2 (order of the matrix)

5 6 7 8 (matrix 1 elements)

1 2 3 4 (matrix 2 elements)
4 4 (resultant matrix)

44

iii)Multiplication
#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int mat1[10][10], mat2[10][10], mat3[10][10];
printf(“Enter number of rows and columns of mat1 matrix\n”);
scanf(“%d%d”, &m, &n);
printf(“Enter elements of matrix 1\n”);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf(“%d”, &mat1[c][d]);
printf(“\nEnter number of rows and columns of mat2 matrix\n”);
scanf(“%d%d”, &p, &q);
if (n != p)
printf(“\nThe matrices can’t be multiplied with each other.\n”);
else
{
printf(“\nEnter elements of matrix2\n”);
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf(“%d”, &mat2[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + mat1[c][k]*mat2[k][d];
}
mat3[c][d] = sum;
sum = 0;
}
}
printf(“\nProduct of the matrices:\n”);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf(“%d\t”, mat3[c][d]);
printf(“\n”);
}
}
return 0;
}
Output:
Enter number of rows and columns of mat1 matrix

22

Enter elements of matrix 1

2345

Enter number of rows and columns of mat2 matrix

22

Enter elements of matrix 2

1234

Product of the matrices:

11 16

19 28

iv) Transpose
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Output:

Enter rows and columns: 2


3
Enter matrix elements:
Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7

Entered matrix:
1 4 0
-5 2 7

Transpose of the matrix:


1 -5
4 2
0 7

9. Finding factorial of a number Using Recursive function

#include<stdio.h>
int factorial(int n)
{
if(n!=0)
return n*factorial(n-1); // general case
else
return 1; // base case
}

int main()
{
int num, result;
printf("Enter a positive number: ");
scanf("%d",&num);
result= factorial(num); //function call
printf("Result = %d\n",result);
return 0;
}
Output:
Enter a positive number: 5
Result = 120
Enter a positive number: 7
Result = 5040

10. Generating Fibonacci series Using Recursive function


#include<stdio.h>
int main()
{
int first=0, second=1, i, n, sum=0;
printf("Enter the number of terms: ");
scanf("%d",&n);
//accepting the terms
printf("Fibonacci Series:");
for(i=0 ; i<n ; i++)
{
if(i <= 1)
{
sum=i;
}
//to print 0 and 1
else
{
sum=first + second;
first=second;
second=sum;
//to calculate the remaining terms.
//value of first and second changes as new term is printed.
}
printf(" %d",sum)
}
return 0;
}

Output:
Enter the number of terms: 5
Fibbonacci series:0 1 1 2 3

11. String manipulations using string functions


i) String length ii) String comparison iii) String copy
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str1[20],str2[20];
int ch,i,j;
do
{
printf("\tMENU");
printf("\n------------------------------\n");
printf("1:Find Length of String");
printf("\n2:Find Reverse of String");
printf("\n3:Concatenate Strings");
printf("\n5:Copy String ");
printf("\n5:Compare Strings");
printf("\n6:Exit");
printf("\n------------------------------\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter String: ");
scanf("%s",str1);
i=strlen(str1);
printf("Length of String : %d\n\n",i);
break;
case 2:
printf("Enter String: ");
scanf("%s",str1);
//strrev(str1);
printf("Reverse string : %s\n\n",str1);
break;
case 3:
printf("\nEnter First String: ");
scanf("%s",str1);
printf("Enter Second string: ");
scanf("%s",str2);
strcat(str1,str2);
printf("String After Concatenation : %s\n\n",str1);
break;
case 4:
printf("Enter a String1: ");
scanf("%s",str1);
printf("Enter a String2: ");
scanf("%s",str2);
printf("\nString Before Copied:\nString1=\"%s\",String2=\"%s\"\n",str1,str2);
strcpy(str2,str1);
printf("-----------------------------------------------\n");
printf("\"We are copying string String1 to String2\" \n");
printf("-----------------------------------------------\n");
printf("String After Copied:\nString1=\"%s\", String2=\"%s\"\n\n",str1,str2);
break;
case 5:
printf("Enter First String: ");
scanf("%s",str1);
printf("Enter Second String: ");
scanf("%s",str2);
j=strcmp(str1,str2);
if(j==0)
{
printf("Strings are Same\n\n");
}
else
{
printf("Strings are Not Same\n\n");
}
break;
case 6:
exit(0);
break;
default:
printf("Invalid Input. Please Enter valid Input.\n\n ");
}
}while(ch!=6);
return 0;
}

Output:
MENU
---------------------------------------
1. Find length of string
2. Find reverse of string
3. Concatenate strings
4. Copy string
5. Compare strings
6. Exit
---------------------------------------
Enter your choice: 3
Enter first string: Tutorial
Enter second string: Ride
String after concatenation: TutorialRide
MENU
---------------------------------------
1. Find length of string
2. Find reverse of string
3. Concatenate strings
4. Copy string
5. Compare strings
6. Exit
-----------------------------------------
Enter your choice: 6

12. String manipulations without using string functions


i) String length ii) String comparison iii) String copy
///fundamental string operation, lenth, concatenation, compare and copy strings without string.h
#include <stdio.h>
#include <stdlib.h>
int find_length(char string[]) {
int len = 0, i;
for (i = 0; string[i] != '\0'; i++) {
len++;
}
return len;
}
void join_strings(char string1[], char string2[]) {
int i, len1, len2;
len1 = find_length(string1);
len2 = find_length(string2);
for (i = len1; i < len1 + len2; i++) {
string1[i] = string2[i - len1];
}
string1[i] = '\0'; //adding null character at the end of input
}
/*returns 0 if thery are same otherwise returns 1*/
int compare_strings(char string1[], char string2[]) {
int len1, len2, i, count = 0;
len1 = find_length(string1);
len2 = find_length(string2);
if (len1 != len2)
return 1;
for (i = 0; i < len1; i++) {
if (string1[i] == string2[i])
count++;
}
if (count == len1)
return 0;
return 1;
}
void copy_string(char destination[], char source[]) {
int len, i;
len = find_length(source);
for (i = 0; i < len; i++) {
destination[i] = source[i];
}
destination[i] = '\0';
}
int main() {
char string1[20], string2[20]; //string variables declaration with size 20
int choice;
while (1) {
printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("Enter the string: ");
scanf("%s", string1);
printf("The length of string is %d", find_length(string1));
break;
case 2:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
join_strings(string1, string2);
printf("The concatenated string is %s", string1);
break;
case 3:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
if (compare_strings(string1, string2) == 0) {
printf("They are equal");
} else {
printf("They are not equal");
}
break;
case 4:
printf("Enter a string: ");
scanf("%s", string1);
printf("String1 = %s\n");
printf("After copying string1 to string 2\n");
copy_string(string2, string1);
printf("String2 = %s", string2);
break;
case 5:
exit(0);
}
}
return 0;
}

Output:
1.Find Length
2.Concatenate
3.Compare
4.Copy
5.Exit
Enter your choice:1
Enter the string:Welcome to Programming
The length of string is 7

13. Palindrome checking Using function


#include <stdio.h>
int isPalindrome(int num);
int main()
{
int num,result;
printf("Enter the number:");
scanf("%d",&num);
result = isPalindrome(num);
if(result==1)
printf("%d is Palindrome number",num);
else
printf("%d is not an Palindrome number",num);
return 0;
}
int isPalindrome(int num)
{
int rnum=0,r,n;
n=num;
while(n>0)
{
r=n%10;
rnum=rnum*10+r;
n=n/10;
}
if(rnum==num)
return 1;
else
return 0;
}

Output:
Enter the number: 151
151 is Palindrome number

14. Counting characters, words and lines using function


#include <stdio.h>
int main()
{
char str[100];//input string with size 100

int words=0,newline=0,characters=0; // counter variables

scanf("%[^~]",&str);//scanf formatting

for(int i=0;str[i]!='\0';i++)
{
if(str[i] == ' ')
{
words++;
}
else if(str[i] == '\n')
{
newline++;
words++;//since with every next line new words start. corner case 1
}
else if(str[i] != ' ' && str[i] != '\n'){
characters++;
}
}
if(characters > 0)//Corner case 2,3.
{
words++;
newline++;
}
printf("Total number of words : %d\n",words);
printf("Total number of lines : %d\n",newline);
printf("Total number of characters : %d\n",characters);
return 0;
}
Output:
Input : Deepak Kumar is the
brother of Aditya Kumar~
Output : Total number of words : 8
Total number of lines : 2
Total number of characters : 36

15. Generate salary slip of employees using structures.


#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,
e[i].name, e[i].bpay, e[i].allow, e[i].ded, e[i].npay) ;
}
getch() ;
}

16. Program to generate student mark list using array of structures


#include<stdio.h>
#include<conio.h>
int k=0;
struct stud
{
int rn;
char name[30];
int m1,m2,m3,total;
float avg;
char grade,result;
}s[30];
void main()
{
int no,roll=101,i;
clrscr();
printf("Enter No of Students : ");
scanf("%d",&no);
for(i=0;i<no;i++)
{
clrscr();
s[k].rn=roll;
printf("\nEnter the Student Roll Number : %d ",s[k].rn);
printf("\nEnter the Student Name :");
fflush(stdin);
gets(s[k].name);
printf("\nEnter the Three Marks : ");
scanf("%d",&s[k].m1);
scanf("%d",&s[k].m2);
scanf("%d",&s[k].m3);
if(s[k].m1>=35 && s[k].m2>=35 && s[k].m3>=35)
{
s[k].result='P';
}
else
{
s[k].result = 'F';
}
s[k].total = s[k].m1+s[k].m2+s[k].m3;
printf("The Total is : %d",s[k].total);
s[k].avg=s[k].total/3;
if(s[k].avg>=60)
{
if(s[k].result == 'P')
{
s[k].grade = 'A';
}
else
{
s[k].grade = 'N';
}
}
else if(s[k].avg>=50)
{
if(s[k].result == 'P')
{
s[k].grade = 'B';
}
else
{
s[k].grade = 'N';
}
}
else if(s[k].avg>=35)
{
if(s[k].result == 'P')
{
s[k].grade = 'C';
}
else
{
s[k].grade = 'N';
}
}
getch();
k++;
roll++;
}
printf("\n*******************************************************");
printf("\n STUDENT MARKLIST ");
printf("\n*******************************************************");
printf("\nROLL \tNAME MARK1 MARK2 MARK3 TOTAL RESULT Average GRADE");
for(i=0;i<no;i++)
{
printf("\n%d\t%s %d %d %d %d %c %0.1f
%c",s[i].rn,s[i].name,s[i].m1,s[i].m2,s[i].m3,s[i].total,s[i].result,s[i].avg,s[i].grade);
}
getch();
}

17. Programs for file handling (Sequential, Random)


#include<stdio.h>
#include<stdlib.h>
int c,i,id;
char name[20];
FILE *fp;
int n;
int search(FILE *fp,int id);
void display(FILE *fp);
typedef struct details
{
int id;
char name[20];
}details;
details d;
void main()
{
printf("\nHow many records you would like to insert ? : ");
scanf("%d",&n);
fp=fopen("one.txt","a");
for(i=0;i<n;i++)
{
printf("\nEnter id and name");
scanf("%d%s",&d.id,d.name);
fwrite(&d,sizeof(d),1,fp);
}
fclose(fp);

while(1)
{
printf("\nWhat would you like to do now ? : \n");
printf("\n1.Display \t2.Search \t3.Modify \t4.Delete \t5.Exit");
scanf("%d",&c);
switch(c)
{
case 1:
fp=fopen("one.txt","r+");
display(fp);
fclose(fp);
break;
case 2:
fp=fopen("one.txt","r+");
printf("\nEnter ID to search : ");
scanf("%d",&id);
if(search(fp,id))
{
printf("\nThe record is as follows : ");
printf("\n%d\t%s",d.id,d.name);
}
else
printf("\nRecord not found");
fclose(fp);
break;
case 3:
fp=fopen("one.txt","r+");
printf("\nEnter ID to modify d record : ");
scanf("%d",&id);

if(search(fp,id))
{
printf("\nEnter new name");
scanf("%s",d.name);
fwrite(&d,sizeof(d),1,fp);
}
else
printf("\nSpecified record not found ");
fclose(fp);
break;
}
}
}

int search(FILE *fp,int id)


{
rewind(fp);
while(fread(&d,sizeof(d),1,fp))
{
if(id==d.id)
return 1;
}
return 0;
}

void display(FILE *fp)


{
rewind(fp);
while(fread(&d,sizeof(d),1,fp))
{
printf("\n%d\t%s",d.id,d.name);
}
}

You might also like