Posts

Showing posts from 2015

Find the GCD of two numbers

Using For Loop #include<stdio.h> int main(){ int x,y,m,i; do { printf("Enter two number: "); scanf("%d%d",&x,&y); if(x==0 || y==0) printf("Please check the input and try again...\n"); }while(x==0 || y==0); if(x>y) m=y; else m=x; for(i=m;i>=1;i--){ if(x%i==0&&y%i==0){ printf("\nGCD of %d and %d is %d",x,y,i) ; break; } } return 0; } Using Recursion #include <stdio.h> int gcd(int a, int b) { if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } int main() { int x,y,m,i; do { printf("Enter two number: "); scanf("%d%d",&x,&y); if(x==0 || y==0) printf("Please check the input and try again...\n"); }while(x==0 || y==0); printf("GCD of %d and %d

Find the Factorial of a number

Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!). n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1. Using While Loop #include<stdio.h> void main() { int a,f,i; printf("Enter a number: "); scanf("%d",&a); f=1; i=1; while(i<=a) { f = f * i; i++; } printf("\nFactorial of %d is: %d",a,f); } Using For Loop #include<stdio.h> void main() { int a,f,i; printf("Enter a number: "); scanf("%d",&a); f=1; for(i=1;i<=a;i++) f = f * i; printf("\nFactorial of %d is: %d",a,f); } Using Recursion #include<stdio.h> int fact(int); int main(){ int num,f; printf("\nEnter a number: "); scanf("%d",&num); f=fact(num); printf("\nFactorial of %d is: %d",num,f); return 0; } int fact(int n){ if(n==1)

Solve All-Pairs Shortest Path Problem using Floyd - Warshall Algorithm

#include<stdio.h> #include<conio.h> #define inf 999 void main() { int i,j,k,n,w[20][20]; printf("\n Enter the no. of vertices : "); scanf("%d",&n); printf("\n Enter the weights : "); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { printf("w[%d][%d]= ",i,j); scanf("%d",&w[i][j]); if(i!=j && (w[i][j]==0)) w[i][j]=inf; } for(i=1;i<=n;i++) { printf("\n"); for(j=1;j<=n;j++) printf("%d\t",w[i][j]); } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(w[i][k]+w[k][j]<w[i][j]) w[i][j]=w[i][k]+w[k][j]; } } } printf("\n The resultant Matrix : \n"); for(i=1;i<=n;i++) { printf("\n"); for(j=1;j<=n;j++) printf("%d\t",w[i][j]); } getch(); } Output   Print   Output Enter the no. of vertices : 5 E

Binary Search using Recursion

#include<stdio.h> void bins(int lb,int ub,int item,int a[]); void main() { int i,n,item,a[20]; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: " ); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&item); bins(0,n-1,item,a); } void bins(int lb,int ub,int item,int a[]) { int mid,flag=0; if(lb<=ub) { mid=(lb+ub)/2; if(item==a[mid]) { flag=flag+1; } else if(item<a[mid]) { return bins(lb,mid-1,item,a); } else return bins(mid+1,ub,item,a); } if(flag==0) printf("Number is not found."); else printf("Number is found at %d", mid+1); } Output   Print   Out

Find the maximum length of the subarray in increasing order.

For example we have an array {5,4,6,-2,-1,0,1,9}. The biggest sub-array in increasing order is: {-2,-1,0,1,9}. So the length is 5. #include<stdio.h> void main () { int arr[8] = {5,4,6,-2,-1,0,1,9},i,arrn[8],count=1,j=0,max; printf("The given array is: "); for(i=0;i<8;i++) printf(" %d" ,arr[i]); // printing the actual array - unnecessary step. printf("\n"); for(i=0;i<7;i++){ if(arr[i]<arr[i+1]) { count++; } else { arrn[j]=count; // adding the length of sub array count=1; j++; } } arrn[j]=count; // adding the final length max=arrn[0]; printf("Length of sub arrays in increasing order"); for(i=0;i<=j;i++){ if(max<arrn[i]) max=arrn[i]; printf(" %d",arrn[i]); // printing the new array - unnecessary step. } printf("\n Maximum Length of the substring: %d",max); // print max va

Spiral Matrix

#include <stdio.h> #define R 3 #define C 3 void spiralPrint(int m, int n, int a[R][C]) { int i, k = 0, l = 0; /* k - starting row index m - ending row index l - starting column index n - ending column index i - iterator */ while (k < m && l < n) { /* Print the first row from the remaining rows */ for (i = l; i < n; ++i) { printf("%d ", a[k][i]); } k++; /* Print the last column from the remaining columns */ for (i = k; i < m; ++i) { printf("%d ", a[i][n-1]); } n--; /* Print the last row from the remaining rows */ if ( k < m) { for (i = n-1; i >= l; --i) { printf("%d ", a[m-1][i]); } m--; } /* Print the first column from the remaining columns */ if (l < n)

Find the maximum occurrence of a character in a string

#include<stdio.h> #include<conio.h> int main() { char str[20], ch; int count = 0, i,j=0 , max; int arr[10]; printf("\nEnter a string : "); scanf("%s", &str); printf("\nEnter the character to be searched : "); scanf("%c", &ch); for (i = 0; str[i] != '\0'; i++) { if (str[i] == ch) count++; else if(count != 0) { arr[j] = count; j++; count = 0; } } arr[j]=count; max = 0; for(i=0;i<=j;i++) if(max<arr[i]) max=arr[i]; if (max == 0) printf("\nCharacter '%c'is not present", ch); else { printf("\nMaximum occurrence of character '%c' : %d", ch, max); } return (0); } Output   Print   Output Enter a string : aabbbaaab Enter the character to be searched : Maximum occurrence of character 'a' : 3

Basic Structure of a C Program

A C program generally has the following parts: §   Preprocessor Commands §   Functions §   Variables §   Statements & Expressions §   Comments Let us consider a C Program: #include<stdio.h> void main() { printf("Hello, World!"); } Preprocessor Commands: These commands tells the compiler to do some preprocessing before executing the actual compilation. For example, “#include <stdio.h>” is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. Functions:  These are main building blocks of any C Program. Every C Program will at least have one function which is a mandatory function called main() function. This function is prefixed with keyword int or void or any other datatype. This defines the return type of the function. For int main() the program will return an integer value to the terminal (system). The C Programming language provides a set of built-in functions. In the above example printf() is a C b

Get your hands dirty

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages: Print the words hello, world! In C, the program to print `` Hello, world! '' is #include <stdio.h> main() { printf("Hello, world!\n"); } Just how to run this program depends on the system you are using.

Basics of C

What Is C? C is a highly flexible and adaptable language. Since its creation in 1970, it's been used for a wide variety of programs including firmware for micro-controllers, operating systems, applications, and graphics programming. When was C developed? C was developed at Bell Laboratories in 1972 by Dennis Ritchie along with Ken Thompson. Many of its principles and ideas were taken from the earlier language B and B's earlier ancestors BCPL and CPL. Who is the inventor of C? Dennis Ritchie invented C, the computer-programming language that underlies Microsoft Windows, the Unix operating system and much of the other software running on computers around the world. Mr. Ritchie was a longtime research scientist at Bell Labs, originally AT&T's research division.

Accept Array elements

#include<stdio.h> void main() { int arr[10], i, j, k, size; printf("\nEnter array size : "); scanf("%d", &size); printf("\nAccept Numbers : \n"); for (i = 0; i < size; i++) { printf("Data [%d]: ",i+1); scanf("%d", &arr[i]); } printf("\nThe Array is : "); for (i = 0; i < size; i++) printf("%d ", arr[i]); getch(); } Output   Print   Download Code Output Enter array size : 5 Accept Numbers : Data [1]: 2 Data [2]: 6 Data [3]: 7 Data [4]: 1 Data [5]: 2 The Array is : 2 6 7 1 2

Clear Screen without clrscr() function call

#include<stdio.h> void clearmyscreen(); void main() { printf("Hello"); getch(); clearmyscreen(); printf("World"); getch(); } void clearmyscreen() { system("cls"); } Output   Print   Download Code Output World

Find Perimeter of a Circle. Take the value of the radius from user.

#include<stdio.h> #define pi 3.14 void main() { int r; float p; printf("Enter the value of radius: "); scanf("%d", &r); p=2*pi*r; printf("Perimeter=%f",p); } Output   Print   Download Code Output Enter the value of radius: 9 Perimeter=56.520000

Use Macro to define the value of PI and Find the Perimeter of a Circle

#include<stdio.h> #define pi 3.14 void main() { int r; r=7; printf("Perimeter=%f",2*pi*r); } Output   Print   Download Code Output Perimeter=43.960000

Print Pattern 5 

#include <stdio.h> void main() { int i, j, k; for(i=5;i>=1;i--) { for(k=5;k>=i;k--) { printf(" "); } for(j=1;j<i;j++) { printf("*"); } printf("\n"); } getch(); } Output   Print   Download Code Output **** *** ** *

Print Pattern 4 

#include <stdio.h> void main() { int i, j, k; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) printf("*"); printf("\n"); } getch(); } Output   Print   Download Code Output ***** **** *** ** *

Print Pattern 3 

#include <stdio.h> void main() { int i, j, k; for(i=5;i>=1;i--) { for(j=1;j<i;j++) { printf(" "); } for(k=5;k>=i;k--) { printf("*"); } printf("\n"); } getch(); } Output   Print   Download Code Output * ** *** **** *****

Print Pattern 2 

#include <stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } getch(); } Output   Print   Download Code Output * ** *** **** *****

Print Pattern 1 

#include<stdio.h> int main() { int num,r,c; printf("Enter number of rows/columns: "); scanf("%d",&num); for(r=1; r<=num; r++) { for(c=1; c<=num; c++) printf("* "); printf("\n"); } getch(); return 0; } Output   Print   Download Code Output Enter number of rows/columns: 5 * * * * * * * * * * * * * * * * * * * * * * * * *

List all the Prime Numbers

#include<stdio.h> void main() { int n,i,j,flag; printf("\n\nEnter the limit\t:"); scanf("%d",&n); printf("\nThe prime numbers within the given limit:\n"); for(i=1;i<=n;i++) { flag=0; for(j=2;j<=i/2;j++) { if(i%j==0) { flag=1; break; } } if(flag==0) printf("%d\t",i); } } Output   Print   Download Code Output Enter the limit :60 The prime numbers within the given limit: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59

Solve Quadratic Equation

#include <stdio.h> #include <math.h> void main() { float a,b,c,d,temp,r1,r2; printf("\nEnter the value of a\t:"); scanf("%f",&a); printf("Enter the value of b\t:"); scanf("%f",&b); printf("Enter the value of c\t:"); scanf("%f",&c); d=((b*b)-(4*c*a)); if(d==0) { printf("\nRoots are real and equal\n"); r1=-b/(2*a); printf("\t X1=%f \n \t X2=%f ", r1, r1 ); } else if(d>0) { printf("\nRoots are real and distinct\n"); temp=sqrt(d); r1=(-b+temp)/(2*a); r2=(-b-temp)/(2*a); printf("\n\tX1=%f\n\tX2=%f",r1,r2); } else { printf("\nRoots are Imaginary\n"); d*=-1; temp=sqrt(d); r1=-b/(2*a); r2=temp/(2*a); printf("\n\tX1=(%f + %fi)\n\tX2=(%f - %fi)",r1,r2,r1,r2); } } Output   Print   Download Code Output Enter the value of a :1 Enter the value of b :-11 Enter the value of c :30 Roots are real and d

Find the greatest of two numbers using Conditional Operator

#include<stdio.h> void main() { int n1,n2,g; printf("Enter Numbers (n1, n2) : "); scanf("%d %d",&n1,&n2); g=n1>n2?n1:n2; printf("Greatest=%d",g); } Output   Print   Download Code Output Enter Numbers (n1, n2) : 5 6 Greatest=6

Generate the Fibonacci Series

#include<stdio.h> void main() { int t,i,m=0,n=1,f=1; printf("\n\nEnter the number of terms:"); scanf("%d",&t); printf("\n The Fibonacci Series of %d terms:\n",t); for(i=1;i<=t;i++) { printf("%d\t",f); f=m+n; m=n; n=f; } } Output   Print   Download Code Output Enter the number of terms:10 The Fibonacci Series of 10 terms: 1 1 2 3 5 8 13 21 34 55

Print 1-10 using do-while loop

#include<stdio.h> void main() { int i=1; do{ printf("%d\n",i); i++; }while(i=100); } Output   Print   Download Code Output 1 2 3 4 5 6 7 8 9 10

Find the perimeter of a Circle

#include <stdio.h> void main() { int r; float pi=3.14; r=7; printf("Perimeter=%f",2*pi*r); } Output   Print   Download Code Output Perimeter=43.960003

Check whether an alphabet is Vowel or not

#include <stdio.h> void main() { char in; printf("Enter Character: "); scanf("%c", &in); if(in>=65&&in<=90 || in>=97&& in<=122) { if(in<97) in=in+32; if(in=='a'||in=='e'||in=='i'||in=='o'||in=='u') printf("vowel"); else printf("consonent"); } else printf("Not an valid Alphabet"); } Output   Print   Download Code Output Enter Character: A vowel

Print the ASCII values of all the English Alphabets

#include<stdio.h> void main() { char chl='a'; char chu='A'; int i; printf("\n==========================="); for(i=1;i<=26;i++,chu++,chl++) printf("\n|%c | %d \t|| %c| %d |",chl,chl, chu, chu); printf("\n==========================="); } Output   Print   Download Code Output =========================== |a | 97 || A| 65 | |b | 98 || B| 66 | |c | 99 || C| 67 | |d | 100 || D| 68 | |e | 101 || E| 69 | |f | 102 || F| 70 | |g | 103 || G| 71 | |h | 104 || H| 72 | |i | 105 || I| 73 | |j | 106 || J| 74 | |k | 107 || K| 75 | |l | 108 || L| 76 | |m | 109 || M| 77 | |n | 110 || N| 78 | |o | 111 || O| 79 | |p | 112 || P| 80 | |q | 113 || Q| 81 | |r | 114 || R| 82 | |s | 115 || S| 83 | |t | 116 || T| 84 | |u | 117 || U| 85 | |v | 118 || V| 86 | |w | 119

Find Area and Circumference of a Circle

#include<stdio.h> #define PI 3.142 void main() { float r,a,c; printf("\nEnter the radius of circle:\t"); scanf("%f",&r); a=PI*r*r; printf("\nThe area of the circle:\t%f",a); c=2*PI*r; printf("\nThe circumference of the circle: %f",c); } Output   Print   Download Code Output Enter the radius of circle: 2 The area of the circle: 12.568000 The circumference of the circle: 12.568000 Enter the radius of circle: 1 The area of the circle: 3.142000 The circumference of the circle: 6.284000

Commonly Asked Interview Questions

Note :  All the programs are tested under Turbo C/C++ compilers and it is assumed that, Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).  Predict the output or error(s) for the following: void main() { int const * p=5; printf("%d",++(*p)); }     Answer     Compiler error: Cannot modify a constant value. Explanation p is a pointer to a "constant integer". But we tried to change the value of the "constant integer". void main() { int const * p=5; printf("%d",++(*p)); }     Answer     Compiler error: Cannot modify a constant value. Explanation p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

Print the Truth Table

#include<stdio.h> int OR(int,int); int AND(int,int); int XOR(int,int); int NOT(int); void main() { int a,b,ch,o; printf("\n-:MENU:-\n1.AND\n2.OR\n3.NOT\n4.XOR\n0.Exit\n"); do{ printf("\nEnter Choice:"); scanf("%d",&ch); switch(ch) { case 1: printf ("\nTruth Table for AND\nA B \tO"); for(a=0;a<=1;a++) { for(b=0;b<=1;b++) { o=AND(a,b); printf("\n%d %d \t%d",a,b,o); } } break; case 2: printf ("\nTruth Table for OR\nA B \tO"); for(a=0;a<=1;a++) { for(b=0;b<=1;b++) { o=OR(a,b); printf("\n%d %d \t%d",a,b,o); } } break; case 3: printf ("\nTruth Table for NOT\

Simple Interest

#include<stdio.h> void main() { float p; float ir,t; float in; printf("Principle amount : "); scanf("%f",&p); printf("\nInterest : "); scanf("%f",&ir); printf("\nTime (in year) : "); scanf("%f",&t); in=p*ir*t/100; printf("\nYou will get interest : Rs. %.2f",in); } Output   Print   Download Code Output Principle amount : 5000 Interest : 10 Time (in year) : 3 You will get interest :Rs. 1500.00

Print the Multiplication Table

#include<stdio.h> void main() { int x=1,num,res; printf("Enter a Number : "); scanf("%d",&num); while(x<=10) { res=num*x; printf("\n%d x %d = %d",num,x,res); x++; } } Output   Print   Download Code Output Enter a Number : 6 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 6 x 10 = 60

Find the Greatest of Three Numbers

#include<stdio.h> void main() { int x,y,z; printf("Enter values of x, y and z : "); scanf("%d,%d,%d",&x,&y,&z); if(x>=y && x>=z) printf("\n%d is greatest",x); else if(y>=z) printf("\n%d is greatest",y); else printf("\n%d is greatest",z); } Output   Print   Download Code Output Enter values of x, y and z : 4, 9, 2 9 is greatest

Find Greatest number using Conditional Operator

#include<stdio.h> void main() { int n1,n2,n3; printf("Enter Numbers (n1 n2 n3) : "); scanf("%d %d %d",&n1,&n2,&n3); printf("Greatest= %d",n1>n2?n1>n3?n1:n3:n2>n3?n2:n3); } Output   Print   Download Code Output Enter Numbers (n1 n2 n3) : 4 9 2 Greatest= 9

Print 1 to 10 using For Loop

#include<stdio.h> int main() { int x=1; for(x=1; x<=10; x++) printf("\n%d",x); return 0; } Output   Print   Download Code Output 1 2 3 4 5 6 7 8 9 10

Print 1 to 10 using While Loop

#include<stdio.h> #include<conio.h> int main() { int x=1; while(x<=10) { printf("\n%d",x); x++; } getch(); return 0; } Output   Print   Download Code Output 1 2 3 4 5 6 7 8 9 10

Swap two Numbers

#include<stdio.h> void main() { int x,y,z; printf("Enter Values x and y : "); scanf("%d%d",&x,&y); z=y; y=x; x=z; printf("\nValue of x=%d",x); printf("\nValue of y=%d",y); } Output   Print   Download Code Output Enter Values x and y : 5 4 Value of x= 4 Value of y= 5

Sum of two numbers

#include<stdio.h> void main() { int a,b,sum; printf("Enter Numbers a and b : "); scanf("%d %d",&a,&b); sum=a+b; printf("\n%d + %d = %d",a,b,sum); } Output   Print   Download Code Output Enter Numbers a and b :5 4 5 + 4 = 9

Print "Hello, World!" without semicolon

Solution: 1 #include<stdio.h> void main(){ if(printf("Hello world")){ } } Solution: 2 #include<stdio.h> void main(){ while(!printf("Hello world")){ } } Solution: 3 #include<stdio.h> void main(){ switch(printf("Hello world")){ } } Output   Print   Download Code Output Hello, World!

Print "Hello, World!"

#include<stdio.h> int main() { printf("\nHello, World! "); return 0; } Output   Print   Download Code Output Hello, World!

Accept Name, Age from User and Print them on the Screen

#include<stdio.h> int main() { char nam[20]; int age; printf("Enter Your Name : "); scanf("%s",&nam); printf("Enter Your Age : "); scanf("%d",&age); printf("\nYour Name is %s ",nam); printf("and your age is %d",age); return 0; } Output   Print   Download Code Output Enter Your Name : Sumit Enter Your Age : 21 Your Name is Sumit and your age is 21
Image
Print PDF

Normalization

Image
Normalization is the process of splitting relations into well-structured relations that allow users to insert, delete, and update tuples without introducing database inconsistencies. The focus of normalization is to reduce redundant data to the minimum.  Normalization is also called “Bottom-up approach”, because this technique requires very minute details like every participating attribute and how it is dependant on the key attributes, is crucial. If you add new attributes after normalization, it may change the normal form itself. Through Normalization, the collection of data in a single table is distributed into multiple tables with specific relation. Without normalization many problems can occur when trying to load an integrated conceptual model into the DBMS. These problems arise from relations that are generated directly from user views are called anomalies. There are three types of anomalies: RollNo StudentName CourseNo CourseName Instructor 120 SKL CS-7