#include<stdio.h> int main() { int i,n,t,a=0,b=1; printf("Enter no. of terms in Fibonacci series:"); scanf("%d",&n); for (i=0;i
#include <stdio.h> int main() { int n,i,j,f; printf("Enter the range till which prime nos are to be found:"); scanf("%d",&n); printf("Prime nos in given range:\n"); for (i=2;i<=n;i++) { f=0; for (j=2;j<=i;j++) if (i%j==0) f++; if (f==1) printf("%d,",i); } return 0; } Output : Enter the range till which prime nos are to be found:20 Prime nos in given range: 2,3,5,7,11,13,17,19,
#include <stdio.h> int main() { int a,t,a1,sum=0; printf("Enter a no:"); scanf("%d",&a); a1=a; while (a) { t=a%10; sum+=(t*t*t); a/=10; } if (a1==sum) printf("No is armstrong"); else printf("No is not armstrong"); return 0; } Output : Enter a no:153 No is armstrong
#include <stdio.h> #include <string.h> int main() { char a[30]; int t; printf("Enter a sentence:"); gets(a); for(t = 0;a[t] != '\0';t++); printf("No of characters including spaces in given sentence=%d",t); return 0; } Output : Enter a sentence:This is a simple sentence No of characters including spaces in given sentence=25
#include <stdio.h> #include <string.h> int main() { char a[30],b[30]; printf("Enter a word:"); gets(a); strcpy(b,a); strrev(a); if (!strcmpi(a,b)) printf("The given word is a palindrome"); else printf("The given word is not a palindrome"); return 0; } Output : Enter a word:malayalam The given word is a palindrome
#include <stdio.h> #include <string.h> int main() { char sin[50], sout[50], b[20][10], temp[20]; int i,c=0,start, len; printf("Enter a string:"); gets(sin); start=0; for (i=0;i<=strlen(sin);i++) { if (sin[i]==' ') { sin[i]='\0'; strcpy(b[c++],&sin[start]); sin[i]=' '; start=i+1; } else if (sin[i]=='\0') strcpy(b[c++],&sin[start]); } strcpy(temp, b[c1]); strcpy(b[c1], b[1]); strcpy(b[1], temp); sout[0] = '\0'; for (i=0;i
#include <stdio.h> int main() { int s,l,a[30],i,n; printf("Enter size of array:"); scanf("%d",&n); printf("Enter the elements of array:"); for (i=0;il) l=a[i]; printf("Smallest no in array=%d\n",s); printf("Largest no in array=%d\n",l); return 0; } Output : Enter size of array:5 Enter the elements of array:1 2 8 5 4 Smallest no in array=1 Largest no in array=8
#include <stdio.h> int main() { int a[30],i,j,n,s,t; printf("Enter size of array:"); scanf("%d",&n); printf("Enter elements of array:"); for (i=0;i
#include <stdio.h rel="nofollow"> int main() { int m,n,i,j,a[10][10],b[10][10]; printf("Enter size of array(row&col):"); scanf("%d%d",&m,&n); printf("Enter elements of array rowise:"); for (i=0;i<m;i++) for (j=0;j
#include <stdio.h> void init(int a[][10],int b[][10],int *m,int *n,int *p,int *q) { int i,j; printf("Enter size of 1st array(row&col):"); scanf("%d%d",m,n); printf("Enter size of 2nd array(row&col):"); scanf("%d%d",p,q); printf("Enter elements of 1st array:"); for (i=0;i<*m;i++) { for (j=0;j<*n;j++) { scanf("%d",&a[i][j]); } } printf("Enter elements of 2nd array:"); for (i=0;i<*p;i++) for (j=0;j<*q;j++) scanf("%d",&b[i][j]); } void add(int a[][10],int b[][10],int m,int n,int p,int q) { int i,j; if (m==p && n==q) { printf("Sum of 2 arrays:\n"); for (i=0;i<m;i++) { printf("\n"); for (j=0;j
int i,j,k,s=0; if (n==p) { printf("Multiplied array:\n"); for (i=0;i<m;i++) { printf("\n"); for (j=0;j
4 Multiply 2 arrays 5 Exit Enter choice(15):1 Enter size of 1st array(row&col):3 3 Enter size of 2nd array(row&col):3 3 Enter elements of 1st array:1 2 3 4 5 6 7 8 9 Enter elements of 2nd array:1 2 3 4 5 6 7 8 9 1 Initialize the 2 arrays 2 Add 2 arrays 3 Subtract 2 arrays 4 Multiply 2 arrays 5 Exit Enter choice(15):2 Sum of 2 arrays: 2 4 6 8 10 12 14 16 18 1 Initialize the 2 arrays 2 Add 2 arrays 3 Subtract 2 arrays 4 Multiply 2 arrays 5 Exit Enter choice(15):3 Difference of 2 arrays: 0 0 0 0 0 0 0 0 0 1 Initialize the 2 arrays 2 Add 2 arrays 3 Subtract 2 arrays 4 Multiply 2 arrays 5 Exit Enter choice(15):4 Multiplied array: 30 36 42 66 81 96 102 126 150 1 Initialize the 2 arrays 2 Add 2 arrays 3 Subtract 2 arrays 4 Multiply 2 arrays 5 Exit Enter choice(15):5
#include <stdio.h> int main() { int a[10][10], n, sum, i, j; printf("Enter the number of elements : "); scanf("%d", &n); printf("Enter the elements :\n"); for(i = 0;i < n;i++) { for(j = 0;j < n;j++) { scanf("%d", &a[i][j]); } } sum = 0; for(i = 0;i < n;i++) { sum += a[i][i]; } printf("Sum of diagonal elements = %d", sum); return 0; } Output : Enter the number of elements : 3 Enter the elements : 1 2 3 4 5 6 7 8 9 Sum of diagonal elements = 15
#include <stdio.h> struct student { char name[20]; int roll_num; int marks; }; int main() { struct student s[20], t; int n, i, j, max; printf("Enter the number of students : "); scanf("%d", &n); for(i = 0;i < n;i++) { printf("Input student details\n"); printf("Name : "); scanf("%s", s[i].name); printf("Roll Number : "); scanf("%d", &s[i].roll_num); printf("Marks : "); scanf("%d", &s[i].marks); } for(i = 0;i < n;i++) { max = i; for(j = i;j < n;j++) { if(s[j].marks > s[max].marks) max = j; } t = s[i]; s[i] = s[max]; s[max] = t; } printf("Ranklist\n"); for(i = 0;i < n;i++) { printf("%d. %s (%d)\n", (i+1), s[i].name, s[i].roll_num); } return 0; } Output : Enter the number of students : 5 Input student details Name : Anoop Roll Number : 214 Marks : 94 Input student details Name : Jacob Roll Number : 145 Marks : 84 Input student details Name : Thomas Roll Number : 263
Marks : 70 Input student details Name : Antony Roll Number : 215 Marks : 90 Input student details Name : Vasu Roll Number : 235 Marks : 88 Ranklist 1. Anoop (214) 2. Antony (215) 3. Vasu (235) 4. Jacob (145) 5. Thomas (263)
#include <stdio.h> #include <string.h> #include int main() { int count = 0; char str[50], *ptr, *sptr; printf("Enter the sentence :\n"); gets(str); ptr = str; while((*ptr) != '\0') { while(!isalpha(*ptr) && ((*ptr) != '\0')) ptr++; sptr = ptr; while(isalpha(*ptr)) ptr++; if(sptr != ptr) count++; } printf("The number of words = %d", count); return 0; } Output : Enter the sentence : this is, a simple sentence *** + The number of words = 5
#include <stdio.h> int main() { FILE *file; file = fopen("input.txt", "r"); if(!file) { printf("Unable to open text file input.txt\n"); return 0; } fseek(file, 2, SEEK_END); printf("The contents of file in reverse order is : \n"); do { putchar(getc(file)); } while(!fseek(file, 2, SEEK_CUR)); return 0; } input.txt abcdefghijklmnopqrstuvwxyz Output : The contents of file in reverse order is : zyxwvutsrqponmlkjihgfedcba
#include <stdio.h> struct bill { int bill_no; char name[30]; int units; }; int main() { struct bill b; FILE *file; int choice, n, flag, cost, q = 1; while(q) { printf("1. Enter a new bill 2. Print a new bill 3. Exit\nEnter choice(13) : "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter Bill No. : "); scanf("%d", &b.bill_no); printf("Enter Consumer Name : "); scanf("%s", b.name); printf("Enter the number of units consumed : "); scanf("%d", &b.units); file = fopen("electricity.txt", "a"); fwrite(&b, sizeof(struct bill), 1, file); fclose(file); break; case 2: printf("Enter the bill number : "); scanf("%d", &n); file = fopen("electricity.txt", "r"); flag = 0; while(fread(&b, sizeof(struct bill), 1, file)) { if(b.bill_no == n) { flag = 1; break; } } fclose(file); if(flag) { if(b.units > 0 && b.units <= 50) cost = b.units * 0.5; else if(b.units > 50 && b.units <= 100) cost = 25 + ((b.units 50) * 1); else if(b.units > 100 && b.units <= 300) cost = 75 + ((b.units 100) * 2); else cost = 475 + ((b.units 300) * 3); printf("Bill No : %d\n", b.bill_no); printf("Consumer Name : %s\n", b.name); printf("Units Consumed : %d\n", b.units); printf("Total cost : %d\n", cost);
} else
}
printf("Bill no. not found\n"); break; case 3: q = 0; break; default: printf("Invalid choice\n");
} }
return 0;
Output : 1. Enter a new bill 2. Print a new bill 3. Exit Enter choice(13) : 1 Enter Bill No. : 214 Enter Consumer Name : Mohan Enter the number of units consumed : 1025 1. Enter a new bill 2. Print a new bill 3. Exit Enter choice(13) : 1 Enter Bill No. : 256 Enter Consumer Name : Rahul Enter the number of units consumed : 2564 1. Enter a new bill 2. Print a new bill 3. Exit Enter choice(13) : 2 Enter the bill number : 230 Bill no. not found 1. Enter a new bill 2. Print a new bill 3. Exit Enter choice(13) : 2 Enter the bill number : 214 Bill No : 214 Consumer Name : Mohan Units Consumed : 1025 Total cost : 2650 1. Enter a new bill 2. Print a new bill 3. Exit Enter choice(13) : 2 Enter the bill number : 256 Bill No : 256 Consumer Name : Rahul Units Consumed : 2564 Total cost : 7267 1. Enter a new bill 2. Print a new bill 3. Exit Enter choice(13) : 3
#include <stdio.h> struct student { char name[20]; int roll_num; int marks; }; int main() { FILE *file; struct student s[20], st; int q = 1, count = 0, n, choice, i, cnt; file = fopen("student.txt", "r"); if(file) { count = getw(file); fread(s, sizeof(struct student), count, file); } fclose(file); while(q) { printf("1. Add new entry 2. Update new entry 3. Display 4. Exit\ n"); printf("Enter choice : "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter name : "); scanf("%s", s[count].name); printf("Enter roll name : "); scanf("%d", &s[count].roll_num); printf("Enter marks : "); scanf("%d", &s[count].marks); count++; file = fopen("student.txt", "w"); putw(count, file); fwrite(s, sizeof(struct student), count, file); fclose(file); break; case 2: printf("Enter the roll number to be updated : "); scanf("%d", &n); for(i = 0;i < count;i++) { if(s[i].roll_num == n) break; } printf("Enter the new marks : "); scanf("%d", &s[i].marks); file = fopen("student.txt", "w"); putw(count, file); fwrite(s, sizeof(struct student), count, file); fclose(file); break; case 3: file = fopen("student.txt", "r");
if(file) { cnt = getw(file); for(i = 0;i < cnt;i++) { fread(&st, sizeof(struct student), 1, file); printf("%s\t%d\t%d\n", st.name, st.roll_num, st.marks); } } fclose(file); break; case 4: q = 0; break; default: printf("Invalid choice!!"); } } return 0; } Output : 1. Add new entry 2. Update new entry 3. Display 4. Exit Enter choice : 1 Enter name : ramesh Enter roll name : 345 Enter marks : 56 1. Add new entry 2. Update new entry 3. Display 4. Exit Enter choice : 1 Enter name : vasu Enter roll name : 743 Enter marks : 90 1. Add new entry 2. Update new entry 3. Display 4. Exit Enter choice : 1 Enter name : gopalan Enter roll name : 690 Enter marks : 100 1. Add new entry 2. Update new entry 3. Display 4. Exit Enter choice : 3 ramesh 345 56 vasu 743 90 gopalan 690 100 1. Add new entry 2. Update new entry 3. Display 4. Exit Enter choice : 2 Enter the roll number to be updated : 743 Enter the new marks : 40 1. Add new entry 2. Update new entry 3. Display 4. Exit Enter choice : 3 ramesh 345 56 vasu 743 40 gopalan 690 100 1. Add new entry 2. Update new entry 3. Display 4. Exit Enter choice : 4
#include<stdio.h> void add(float a,float b) {printf("%g + %g= %g",a,b,a+b); } void sub(float a,float b) { printf("%g %g= %g",a,b,ab); } void mul(float a,float b) { printf("%g * %g= %g",a,b,a*b); } void div(float a,float b) { if(b==0) printf("Divided by zero error"); else printf("%g / %g= %g",a,b,a/b); } void mod(float a,float b) { if(b==0) printf("Divided by zero error"); else printf("%d %% %d= %d",(int)a,(int)b,((int)a%(int)b)); } int main() { float a,b; int c; do{ printf("\n1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit\n"); printf("Enter choice(16):"); scanf("%d",&c); if(c==6) break; printf("Enter 2 nos:"); scanf("%f%f",&a,&b); switch(c) { case 1 : add(a,b); break; case 2 : sub(a,b); break; case 3 : mul(a,b); break; case 4 : div(a,b); break; case 5 : mod(a,b); break; } }while(c!=6); return 0; }
Output : 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):1 Enter 2 nos:2.3 3.6 2.3 + 3.6= 5.9 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):2 Enter 2 nos:5.7 1.2 5.7 1.2= 4.5 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):3 Enter 2 nos:7.7 3.3 7.7 * 3.3= 25.41 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):4 Enter 2 nos:5.5 1.1 5.5 / 1.1= 5 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):5 Enter 2 nos:5.5 2.1 5 % 2= 1 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):4 Enter 2 nos:34.2 0 Divided by zero error 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):5 Enter 2 nos:43.33 0 Divided by zero error 1. Add 2. Subtract 3. Multiply 4. Division 5. Modulus 6. Exit Enter choice(16):6
#include <stdio.h> int factorial(int n) { if (n == 0) return 1; else return (n * factorial(n1)); } int main() { int n; printf("Enter the number whose factorial is to be found : "); scanf("%d", &n); printf("The factorial of %d is %d", n, factorial(n)); return 0; } Output : Enter the number whose factorial is to be found : 7 The factorial of 7 is 5040
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *ptr; }; typedef struct node node; int length(node *head) { node *n; int count = 0; if (head == NULL) return 0; n = head; do { count++; n = n>ptr; } while (n); return count; } node *insert_node(node *head, int pos, int data) { node *new_node, *n; int i = 0; new_node = (node *)malloc(sizeof(node)); new_node>data = data; if (head == NULL) { new_node>ptr = NULL; return new_node; } if (pos == 0) { new_node>ptr = head; return new_node; } n = head; do { if (i == (pos1)) { new_node>ptr = n>ptr; n>ptr = new_node; break; } i++; n = n>ptr; } while (n); return head; } node *append_node(node *head, int data) { return insert_node(head, length(head), data); }
node *delete_node(node *head, int pos) { node *p, *n; int i = 0; if (head == NULL) return NULL; if (pos == 0) { p = head>ptr; free(head); return p; } n = head; do { if (i == (pos1)) { p = n>ptr; n>ptr = n>ptr>ptr; free(p); break; } i++; n = n>ptr; }while (n); return head; } int search_node(node *head, int query) { node *n; int i = 0; if (head == NULL) return 1; n = head; do { if (n>data == query) { return i; } i++; n = n>ptr; } while (n); return 1; } void print_list(node *head) { node *n; n = head; if (head == NULL) { printf("Link list is empty\n"); return; } do { printf("%d, ", n>data); n = n>ptr; } while (n);
printf("\n"); } void free_list(node *head) { node *n; n = head; do { n = delete_node(n, 0); }while (n); } int main() { int q = 1, choice, pos, data, s; node *head = NULL; while (q) { printf("1. Insert 2. Append 3. Delete "); printf("4. Display 5. Search 6. Exit\n"); printf("Enter choice (16) : "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter the position : "); scanf("%d", &pos); printf("Enter the data : "); scanf("%d", &data); head = insert_node(head, pos, data); break; case 2: printf("Enter the data : "); scanf("%d", &data); head = append_node(head, data); break; case 3: printf("Enter the position to be deleted : "); scanf("%d", &pos); head = delete_node(head, pos); break; case 4: print_list(head); break; case 5: printf("Enter the number to be searched : "); scanf("%d", &data); s = search_node(head, data); if (s > 0) printf("Found at %d\n", s); else printf("Not found!!\n"); break; case 6: q = 0; break; default: printf("Invalid choice!!\n"); } }
free_list(head); return 0; } Output : 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 2 Enter the data : 123 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 2 Enter the data : 345 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 2 Enter the data : 567 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 4 123, 345, 567, 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 1 Enter the position : 1 Enter the data : 555 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 4 123, 555, 345, 567, 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 3 Enter the position to be deleted : 2 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 4 123, 555, 567, 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 5 Enter the number to be searched : 555 Found at 1 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 5 Enter the number to be searched : 20 Not found!! 1. Insert 2. Append 3. Delete 4. Display 5. Search 6. Exit Enter choice (16) : 6