C Prog Java Data Structures DBMS Web Prog
Arrays-Cyclic Rotation of Elements
WAP in C to cyclically rotate the elements in array. Program should accept a choice in which direction to rotate i.e. left or right. Depending on choice it should perform cyclic rotation. Suppose array A contains elements {1,2,3,4,5} then if choice is rotate right o/p should be {5,1,2,3,4} and if choice is rotate left then o/p should be {2,3,4,5,1}1.
#include <stdio.h> #include <conio.h> void left(int a[],int n); void right(int a[],int n); void main() { int a[30],n; int choice,i; clrscr(); printf("Total elements n="); scanf("%d",&n); printf("Enter the elements of array A:\n"); for(i=0;i<=n-1;i++) { printf("%d",i+1); scanf("%d",&a[i]); } do { printf("The Array A is :\n"); for(i=0;i<=n-1;i++) { printf("%d ",a[i]); } printf("\nRotate the array in Left(1) or Right(2) direction or Quit(3)?\n"); printf("Enter your choice :"); scanf("%d",&choice); if(choice==1) { printf("Left\n"); left(a,n); } else if(choice==2) { printf("Right\n"); right(a,n); } else if(choice==3) printf("Quit\n"); else printf("Invalid Choice\n"); }while(choice != 3); getch(); } void left(int a[],int n) { int temp,i; temp=a[0]; for(i=0;i<=n-1;i++) { a[i]=a[i+1]; } a[n-1]=temp; } void right(int a[],int n) { int temp,i; temp=a[n-1]; for(i=n-1;i>0;i--) { a[i]=a[i-1]; } a[0]=temp; }
Arrays- 2-D Elements
2. Write a program to print the diagonal & non diagonal elements, sum of Odd Nos and Even Nos of an 2D array
#include <stdio.h> #include <conio.h> void main() { int a[10][10], m, n, i, j, d=0,x=0,e=0,o=0; printf("Enter a row"); scanf("%d", &m); n=m; for(i=0,i<=m-1;i++) { for(j=0;j<=n-1;j++) { scanf("%d", &a[i][j]); } } for(i=0,i<=m-1;i++) { for(j=0;j<=n-1;j++) { if(i==j) d =d + a[i][j]; else x = x+ a[i][j]; if(a[i][j]%2 == 0) e = e +a[i][j]; else o = o+a[i][j]; } } printf("Sum of diagonal is %d non diagonal is %d odd no is %d even no is %d", x, o, e); getch(); }
Arrays- Deleting an element from Array
3. Deleting an element from Array.
#include <stdio.h> #include <conio.h> void main() { int n,i,a[50],delnum,j,k; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); printf("Enter Data:-"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEntered Data:-\n"); for(i=0;i<n;i++) { printf("%d",a[i]); } printf("\nEnter Num to Delete :- "); scanf("%d",&delnum); for(i=0;i<n;i++) { if(a[i]==delnum) { for(k=i;k<n;k++) { a[k]=a[k+1]; } n--; i--; } } printf("\nAfter Deletion:-\n"); for(i=0;i<n;i++) { printf("%d",a[i]); } getch(); }
Patterns and Pascal Triangle
1. WAP to display the following pattern. 1 21 123 4321 12345
#include <stdio.h> #include <conio.h> void main() { int i, j; clrscr(); printf("The required pattern displayed is :\n"); for(i=1;i<=5;i++) { if(i%2!=0) { for(j=i; j>=1;j--) printf("%d",j); } else { for(j=1;j<=i; j++) printf("%d ",j); } printf("\n"); } getch(); } 2. WAP to produce the following pattern * * * * * * * * *
#include <stdio.h> #include <conio.h> void main() { int n,i,j,k; clrscr(); printf("Enter the no. of lines : "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=n-i;j>=1;j--) { printf(" "); } for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } k=n-1; for(i=2;i<=n;i++) { for(j=n-k;j>=1;j--) { printf(" "); } for(j=1;j<=k;j++) { printf("* "); } k--; printf("\n"); } getch(); } 3. Generate the following pattern 1 232 34543 4567654
#include <stdio.h> #include <conio.h> void main() { int n,i,s,count=0,count1 = 0,k; clrscr(); printf("Enter the number of rows :"); scanf("%d",&n); for(i=1;i<=n;++i) { for(s=1;s<=n-i;++s) { printf(" "); ++count; } while(k!=2*i-1) { if(count<=n-1) { printf("%d",(i+k)); ++count; } else { ++count1; printf("%d ", (i+k-2*count1)); } ++k; } count1 = count = k = 0; printf("\n"); } getch(); } 4. Write a C program to print Pascal triangle 1 1 1 1 2 1 1 3 3 1
#include <stdio.h> long factorial(int); void main() { int i, n, j; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for (i = 1; i <=n; i++) { for (j = 1; j <=n-i; j++) printf(" "); for (j = 1 ; j <= i; j++) printf("%ld ",factorial(i-1)/(factorial(j-1)*factorial(i-j))); printf("\n"); } getch(); } long factorial(int n) { int i; long fact= 1; for (i = 1; i <= n; i++) fact=fact*i; return fact; } 5. WAP to print the following pattern. A CB FED JIHG
#include <stdio.h> #include <conio.h> void main() { int i, k,j; clrscr(); for(i=1,k=0;i<=4;i++) { for(j=1;j<=4-i;j++) { printf(" "); } for(j=i;j>=1;j--) { printf("%c",(char)(j+k+64)); } k=k+i; printf("\n"); } getch(); }
Series
Q1. Write a program for computing sin x series Sin x = x - x^3/3! + x^5/5! - x^7/7! .... + - x^n/n!
# include <stdio.h> #include <conio.h> #include <math.h> void main() { float x, numerator, sum, t; int i, n, fact = 1, sign = -1; printf("Enter n, x values\n"); scanf("%d%f",&n,&x); x=x*(3.1412/180); sum=x; for(i=3;i<=n;i=i+2) { fact = fact * i * ( i -1); numerator=pow(x,i); t = numerator/fact; sum = sum + (sign * t); sign = sign * -1; } printf("Result=%f", sum); getch(); } Q2. WAP to evaluate the value of Standard deviation & display the result.
#include <stdio.h> #include <conio.h> #include <math.h> void main() { int xi[100],n,sum=0,i; float xiavg,sd,sum1=0; clrscr(); printf("Total no of elements :"); scanf("%d",&n); printf("Enter the elements of array :\n"); for(i=0;i<=n-1;i++) { printf("Enter element :"); scanf("%d",&xi[i]); sum=sum + xi[i]; } xiavg=sum/n; for(i=0;i<=n-1;i++) { sum1=sum1 + pow(xi[i]-xiavg,2); } sd=pow((sum1/n ),0.5); printf("The standard deviation is %f ",sd); getch(); } Q3. WAP to evaluate the value of the following series and display the result #include <stdio.h> #include <conio.h> void main() { int n, i, a[50],sum1=0, sum2 = 0, sum; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=0;i<=n-1;i++) { printf("Enter a value:"); scanf("%d",&a[i]); } for(i=0;i<=n-1;i++) { sum1=sum1 + a[i]*a[i]; sum2 = sum2 + a[i]; } sum = sum1 - sum2 * sum2; printf("The value of the series is %d :\n", sum); getch(); }
Strings: Palindrome, Title Case
1. Write a program to check whether entered string is palindrome or not
#include <stdio.h> #include <conio.h> void main() { int n, i; char a[50], rev[50]; clrscr(); printf("Enter the string:"); gets(a); printf("Enter a value:"); for(i=0;i<=n-1;i++) { rev[n-i-1]=a[i]; } for(i=0;i<=n-1;i++) { if(a[i]!=rev[i]) break; } if(i==n) printf("The string is palindrome:\n"); else printf("The string is NOT palindrome:\n"); getch(); } 2. WAP for change case of string or title case of string #include <stdio.h> #include <conio.h> void main() { int i; char str[30]; clrscr(); printf("Enter any string: "); gets(str); for(i=0; str[i]!='\0'; i++) { if(str[i-1]==' ' || i==0) { if(str[i]>='a' && str[i]<='z') str[i]=str[i]-32; } printf("%c",str[i]); } getch(); }
Structures
1. WAP to store the name, matches played and goals scored by 'n' hockey players using structure. Generate a list with goals scored in descending order i.e. display the output in tabular form in order of maximum goals scored to minimum goals scored.
#include <stdio.h> #include <conio.h> struct hockey { char name[30]; int matches, goals; }; void main() { struct hockey e[100], temp; int i,n, j; clrscr(); printf("Enter no of hockey players:\n"); scanf("%d", &n); for(i=0;i<n;i++) { printf("Enter Name, matches, goals scored"); scanf("%s",e[i].ename); scanf("%d",&e[i].matches); scanf("%d",&e[i].goals); } //to bubble-sort(descending order) according to goals scored for(i=0;i<n-1;i++) { for(j=0;j<n-1;j++) { if(e[j].goals < e[j+1].goals) { temp=e[j]; e[j]=e[j+1]; e[j+1]=temp; } } } printf("Name\tMatches\tGoals\n"); for(i=0;i<n;i++) { printf("%s\t%d\t%d \n", e[i].ename, e[i].matches, e[i].goals); } getch(); }2. Design a structure named employees to print names & nos. Of employees who have 5 years or more experience and salary less than Rs. 10,000 using array of structure. Employee details are as follows: (i) Employee Name (ii)Employee Experience (iii)Employee Salary (iv)Employee Number
#include <stdio.h> #include <conio.h> struct employee { char ename[30]; int salary, exp, number; }; void main() { struct employee e[100], i; clrscr(); for(i=0;i<10; i++) { printf("Enter Employees Name, number, exp & Salary"); scanf("%s",e[i].ename); scanf("%d",&e[i].number); scanf("%d",&e[i].exp); scanf("%d",&e[i].salary); } printf(quot\nEmployee list with 5+ years exp & salary 10000 or less\n Name\tNumber\n") for(i=0;i<10;i++) { if(e[j].exp>=5e && e[i].salary < 10000) { printf("%s\t%d\n", e[i].ename, e[i].number); } } getch(); }
Switch Case
1. An electric power distribution company charges its domestic consumer as follows:Program should read the units consumed for a customer and calculate the total bill.
Consumption Units Rate of Charges 0-200 0.50 per unit 201-400 Rs.100 + 0.65 per unit excess of 200 401-600 Rs.230 + 0.85 per unit excess of 400 600 - above Rs.390 + 1.00 per unit excess of 600
#include <stdio.h> #include <conio.h> void main() { int units; float charge; clrscr(); printf("Enter the units consumed:"); scanf("%d", &units); switch(units/200) { case 0: charge = units * 0.5; break; case 1: charge = (units - 200)*0.65+100; break; case 2: charge = (units - 400)*0.85+230; break; default: charge = (units - 600) +390; break; } printf("Charges = %f", charge); getch(); } 2. WAP to display a user entered number in words.
#include <stdio.h> #include <conio.h> void main() { int n, digit, rev=0; clrscr(); printf("Enter a number: "); scanf("%d",&n); while(n!=0) { digit= n%10; rev = rev * 10 + digit; n = n/10; } while(rev !=0) { digit = rev%10; rev =rev/10; switch(digit) { case 0: printf("Zero"); break; case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; case 4: printf("Four"); break; case 5: printf("Five"); break; case 6: printf("Six"); break; case 7: printf("Seven"); break; case 8: printf("Eight"); break; case 9: printf("Nine"); break; } } getch(); } 3. WAP to count vowels using switch case.
#include <stdio.h> #include <conio.h> void main() { char a[100]; int i, count = 0, len = 0; clrscr(); printf("Enter a string :"); gets(a); while(a[len] != '\0') { len++; } for(i=0;i<=len-1;i++) { switch(a[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': count++; } } printf("Number of vowels is %d", count); getch(); }
Misc- Prime Numbers, Fibonacci and armstrong Numbers
1. Write a program to display first 'n' prime numbers where the value of n is taken from the user
#include <stdio.h> #include <conio.h> void main() { int i, n, x=1; clrscr(); printf("Enter a number"); scanf("%d",&n); printf("The following are the %d prime numbers \n", n); while(n!=0) { x++; i=2; while(x%i!=0) { i++; } if(x==i) { printf("%d\n",x); n--; } } getch(); } 2. WAP to check a number is include in Fibonacci term or not.
#include <stdio.h> #include <conio.h> void main() { int a,b,c,num; printf("Enter any number: "); scanf("%d", &num); if((num==0)||(num==1)) printf("\n%d is a Fibonacci term",num); else { a=0; b=1; c=a+b; while(c<num) { a=b; b=c; c=a+b; } if(c==num) printf("\n%d is a Fibonacci term",num); else printf("\n%d is not a Fibonacci term",num); } getch(); } 3. WAP to Check Armstrong Number of n digits.
#include <stdio.h> #include <conio.h> void main() { int x, copy, digit, sum = 0, n = 0 ; printf("Enter an number: "); scanf("%d", &x); copy = x; while (copy!= 0) { copy= copy/ 10; n++; } copy = x; while (x!= 0) { digit = x %10; sum = sum+ pow(digit, n); x = x/ 10; } if(sum == copy) printf("Armstrong number."); else printf("Not an Armstrong number."); }
Some Tricky Problems
1. WAP to Check Whether a Number can be expressed as Sum of Two Prime Numbers. Output: Enter a Positive integer: 34 34 = 3 + 31 34 = 5 + 29 34 = 11 + 23 34 = 17 + 17
#include<stdio.h> #include<conio.h> void main() { int i, n, x, c, diff; clrscr(); printf("\nEnter a number: "); scanf("%d", &n); for(i=2;i<=n;i++) { x=2; while(i%x!=0) x++; if(x==i) { diff = n-i; x=2; while(diff%x!=0) x++; if(x==diff) { c++; printf("%d = %d + %d\n", n,i,diff); } } } if(c==0) printf("\nSolution not possible \n"); getch(); }
2. WAP to Convert Binary Number to Decimal or Decimal to Binary Number Output: Enter a Decimal Number: 439 Binary Number is: 110110111
#include<stdio.h> #include<conio.h> void main() { int a[16],i,x,d; clrscr(); for(i=0;i<=15;i++) { a[i]=0; } printf("Enter a decimal no"); scanf("%d", &x); i=15; while(x!=0) { d= x%2; a[i]=d; i--; x=x/2; } printf("Binary no is"); for(i=0;i<=15;i++) { printf("%d",a[i]); } getch(); }
3. WAP to Find the Frequency of Characters in a String. Output: Enter a String: This website is awesome Enter a character to find its frequency : e Frequency of e: 4
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char c, a[100]; int count=0, l, i; clrscr(); printf("\nEnter a String: "); gets(a); printf("\nEnter a character whose frequency is to be checked : "); scanf("%c", &c); l = strlen(a); for(i=0; i < l;i++) { if(a[i]==c || a[i] == c-32 || a[i] == c+32) count++; } printf("\nFrequency of %c is %d \n", c, count); getch(); }
4. WAP to Sort Elements in Lexicographical Order (Dictionary Order) Output: Enter 5 words: C++ Java Web Design PHP Sorted View: C++ Java PHP Web Design
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[10][20], t[50]; int n, i, j; clrscr(); printf("\nEnter number of words to accept"); scanf("%d", &n); for(i=0;i<n;i++) { printf("Enter a String:"); scanf("%s",s[i]); } for(i=1; i < n;i++) { for(j=0;j<=n-1; j++) { if(strcmp(s[j], s[j+1])>0) { strcpy(t, s[j]); strcpy(s[j], s[j+1]); strcpy(s[j+1], t); } } } printf("\nSorted View \n"); for(i=0;i<n;i++) { printf("%s\n", s[i]); } getch(); }
5. WAP to Add Two Distances (in inch-feet) System Using Structures Output: Enter information for 1st distance Enter feet: 23 Enter inch: 8.6 Enter information for 2nd distance Enter feet: 34 Enter inch: 2.4 Sum of distances = 57’-11.0”
#include<stdio.h> #include<conio.h> struct distance { float feet; float inch; }; void main() { struct distance d[3]; int i, feet_total=0; float inch_total=0; clrscr(); printf("\nEnter number of words to accept"); scanf("%d", &n); for(i=0;i<3;i++) { printf("\nEnter information for %d distance", i+1); printf("\nEnter feet"); scanf("%f",&d[i].feet); printf("\nEnter inch"); scanf("%f",&d[i].inch); feet_total = feet_total + d[i].feet; inch_total = inch_total + d[i].inch; } if(inch_total > 12.0) { inch_total = inch_total - 12.0; feet_total++; } printf("\nSum of distances = %d' - %f \n", feet_total,inch_total); printf("%s\n", s[i]); getch(); } OR #include <stdio.h> struct Distance { int feet; float inch; }; void main() { struct Distance d1, d2, dist_tot; printf("Enter information for 1st distance\n"); printf("Enter feet: "); scanf("%d", &d1.feet); printf("Enter inch: "); scanf("%f", &d1.inch); printf("\nEnter information for 2nd distance\n"); printf("Enter feet: "); scanf("%d", &d2.feet); printf("Enter inch: "); scanf("%f", &d2.inch); dist_tot.feet = d1.feet+d2.feet; dist_tot.inch = d1.inch+d2.inch; // If inch is greater than 12, changing it to feet. if (dist_tot.inch>12.0) { dist_tot.inch = dist_tot.inch-12.0; dist_tot.feet++; } printf("\nSum of distances = %d\'-%.1f\"", dist_tot.feet, dist_tot.inch); getch(); }
6. Find the first circular tour that visits all petrol pumps. Suppose there is a circle. There are n petrol pumps on that circle. You are given two sets of data. 1. The amount of petrol that every petrol pump has. 2. Distance from that petrol pump to the next petrol pump. Calculate the first point from where a car will be able to complete the circle. The car will stop at each petrol pump and it has infinite capacity. Expected time complexity is O(n). Expected Data Structure is Singly Linked List. Assume for 1 litre petrol, the car can go 1 unit of distance. For example, let there be 4 petrol pumps with amount of petrol and distance to next petrol pump value pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}. The first point from where truck can make a circular tour is 2nd petrol pump. Hence, Output should be “2”.
#include<stdio.h> #include<stdlib.h> struct petrolPump { int petrol; int distance; struct petrolPump *next; }; typedef struct petrolPump petrolPump; int tour(petrolPump *, int ); void displayPP(petrolPump *); int getIndex(petrolPump *, petrolPump *); petrolPump * getPpAtIndex(petrolPump *, int); int main() { int i=0, totalPP; petrolPump *head=NULL, *temp, *current; printf("How many petrol pumps?\n"); scanf("%d",&totalPP); for(i=1; i<=totalPP; i++) { //creation temp = (petrolPump*)malloc(sizeof(petrolPump)); //initialization printf("Enter petrol in Petrol Pump number %d : ", i); scanf("%d", &temp->petrol); printf("Enter distance to reach next Petrol Pump : "); scanf("%d", &temp->distance); temp->next = NULL; //connection if(head==NULL) { head = temp; current = head; } else { current->next = temp; current = temp; } } if((i = tour(head, totalPP))!=0) printf("\nWe should start tour from petrol pump : %d\n", i); else printf("\nTour can not be completed\n"); return (EXIT_SUCCESS); } int tour(petrolPump *p, int n)//using "petrolPump* getPpAtIndex(petrolPump *head, int pos)" { int i=0, ptr=0, index=0; for(i=0; i<n; i++) { ptr += getPpAtIndex(p,i)->petrol - getPpAtIndex(p,i)->distance; if(ptr<0) { if((index = i+1)>=n) return -1; ptr = 0; } } for(i=0; i<index; i++) { ptr += getPpAtIndex(p,i)->petrol - getPpAtIndex(p,i)->distance; if(ptr<0) return -1; } return ++index; } petrolPump * getPpAtIndex(petrolPump *head, int pos) { int i; for(i=0; i<pos; i++) head = head->next; return head; } /* int tour(petrolPump *head, int n) //without using //"petrolPump* getPpAtIndex(petrolPump *head, int pos)" { int i=0, ptr=0, index=0; petrolPump *pp; for(pp=head; pp!=NULL; pp=pp->next, i++) { ptr += pp->petrol - pp->distance; if(ptr<0) { if((index = i+1)>=n) return 0; ptr = 0; } } for(i=0, pp=head; i<next) { ptr += pp->petrol - pp->distance; //printf("\nPetrol : %d", ptr); if(ptr<<0) return 0; } return ++index; } */
LMR
WAP to accept 10 numbers from user using pointers and find their sum and average.
#include<stdio.h> #include<conio.h> void main() { int *p, i, sum=0, a[10]; float avg; clrscr(); p=a; printf("Enter 10 numbers\n"); for(i=0;i<=9;i++) { scanf("%d",(p+i)); sum+=*(p+i); } avg=sum/10; for(i=0;i<=9;i++) printf("%d\n",*(p+i)); printf("Average=\n",avg); getch(); }
WAP to copy text from one file to other after converting lower case letters to upper case and vice versa. Keep other characters as it is.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp1, *fp2; char ch; clrscr(); fp1=fopen("read.txt","r"); fp2=fopen("write.txt","w"); while(!feof(fp1)) { ch=fgetc(fp1); if(ch>='a' && ch <= 'z') ch = ch-32; putc(ch,fp2); } printf("file copied successfully"); fclose(fp1); fclose(fp2); getch(); }