Technocrat

  • Home

Tuesday, October 13, 2015

Find the GCD of two numbers

 Sumit Kar     October 13, 2015     DS, For Loop, Recursion     No comments   

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 is %d ", x, y, gcd(x, y));
return 0;
}

   

Output
Enter two number: 0 99
Please check the input and try again...
Enter two number: 99 121

GCD of 99 and 121 is 11

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find the Factorial of a number

 Sumit Kar     October 13, 2015     For Loop, Recursion, While Loop     No comments   


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)
return 1;
else
return(n*fact(n-1));
}



   
Output

Enter a number: 5

Factorial of 5 is: 120
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

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

 Sumit Kar     October 13, 2015     DS, Graph     No comments   

#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

Enter the no. of vertices : 5

Enter the weights : w[1][1]= 0
w[1][2]= 3
w[1][3]= 8
w[1][4]= 0
w[1][5]= 4
w[2][1]= 0
w[2][2]= 0
w[2][3]= 0
w[2][4]= 1
w[2][5]= 7
w[3][1]= 0
w[3][2]= 4
w[3][3]= 0
w[3][4]= 0
w[3][5]= 0
w[4][1]= 2
w[4][2]= 0
w[4][3]= 5
w[4][4]= 0
w[4][5]= 0
w[5][1]= 0
w[5][2]= 0
w[5][3]= 0
w[5][4]= 6
w[5][5]= 0

0 3 8 999 4
999 0 999 1 7
999 4 0 999 999
2 999 5 0 999
999 999 999 6 0
The resultant Matrix :

0 3 8 4 4
3 0 6 1 7
7 4 0 5 11
2 5 5 0 6
8 11 11 6 0

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Binary Search using Recursion

 Sumit Kar     October 13, 2015     DS, Recursion, Search     No comments   

#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
Enter the size of an array: 6
Enter the elements of the array: 90 95 1000 1001 1003 1006
Enter the number to be search: 95
Number is found at 2

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Wednesday, September 23, 2015

Find the maximum length of the subarray in increasing order.

 Sumit Kar     September 23, 2015     Array, Tricky     No comments   

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 value
}

   
Output
The given array is: 5 4 6 -2 -1 0 1 9
Length of sub arrays in increasing order 1 2 5
Maximum Length of the substring: 5
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Spiral Matrix

 Sumit Kar     September 23, 2015     If Else, Matrix, Tricky, User Defined Function, While Loop     No comments   

#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)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
}

/* Program to test above functions */
int main()
{
int a[R][C] = { {1, 2, 3},
{8, 9, 4},
{7, 6, 5}
};

spiralPrint(R, C, a);
return 0;
}

   
Output
1 2 3 4 5 6 7 8 9
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find the maximum occurrence of a character in a string

 Sumit Kar     September 23, 2015     Array, For Loop, If Else, Tricky     No comments   

#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

Enter a string : aabbbaaab

Enter the character to be searched :
Maximum occurrence of character 'a' : 3

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Monday, August 17, 2015

Basic Structure of a C Program

 Sumit Kar     August 17, 2015     No comments   

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 built-in function which is used to print anything on the screen.

Variables: These are used to hold numbers, strings and complex data for manipulation.

Statements & Expressions: Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.

Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/

Note
•C is a case sensitive programming language. It means in C printf and Printf will have different meanings.
•C has a free-form line structure. End of each C statement must be marked with a semicolon.
•Multiple statements can be on the same line.
•White Spaces (ie tab space and space bar ) are ignored.
•Statements can continue over multiple lines.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Get your hands dirty

 Sumit Kar     August 17, 2015     Basic     No comments   

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.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Basics of C

 Sumit Kar     August 17, 2015     Basic     No comments   

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.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Monday, July 27, 2015

Accept Array elements

 Sumit Kar     July 27, 2015     Array, Basic, For Loop     No comments   

#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

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

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Clear Screen without clrscr() function call

 Sumit Kar     July 27, 2015     Tricky, User Defined Function     No comments   

#include<stdio.h>
void clearmyscreen();
void main()
{
printf("Hello");
getch();
clearmyscreen();
printf("World");
getch();
}

void clearmyscreen()
{
system("cls");
}

   

Output
World

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

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

 Sumit Kar     July 27, 2015     Basic, Input, Macro     No comments   

#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
Enter the value of radius: 9
Perimeter=56.520000

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

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

 Sumit Kar     July 27, 2015     Basic, Macro     No comments   

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

   

Output
Perimeter=43.960000

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Saturday, July 25, 2015

Print Pattern 5 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

#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

****
***
**
*

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print Pattern 4 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

#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
*****
****
***
**
*

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print Pattern 3 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

#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

*
**
***
****
*****

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print Pattern 2 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

#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
*
**
***
****
*****

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Friday, July 24, 2015

Print Pattern 1 

 Sumit Kar     July 24, 2015     Basic, For Loop, Pattern     No comments   

#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
Enter number of rows/columns: 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Tuesday, June 30, 2015

List all the Prime Numbers

 Sumit Kar     June 30, 2015     Basic, For Loop     No comments   

#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
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

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Sunday, June 28, 2015

Solve Quadratic Equation

 Sumit Kar     June 28, 2015     If Else, Math     No comments   

#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
Enter the value of a :1
Enter the value of b :-11
Enter the value of c :30

Roots are real and distinct

X1=6.000000
X2=5.000000
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find the greatest of two numbers using Conditional Operator

 Sumit Kar     June 28, 2015     Basic, Conditional Operator     No comments   

#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
Enter Numbers (n1, n2) : 5 6

Greatest=6
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Generate the Fibonacci Series

 Sumit Kar     June 28, 2015     Basic, For Loop     No comments   

#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
Enter the number of terms:10

The Fibonacci Series of 10 terms:
1 1 2 3 5 8 13 21 34 55

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print 1-10 using do-while loop

 Sumit Kar     June 28, 2015     Basic, do-while     No comments   

#include<stdio.h>
void main()
{
int i=1;
do{
printf("%d\n",i);
i++;
}while(i=100);
}

  

Output

1
2
3
4
5
6
7
8
9
10

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find the perimeter of a Circle

 Sumit Kar     June 28, 2015     Basic     No comments   

#include <stdio.h>

void main()
{
int r;
float pi=3.14;
r=7;
printf("Perimeter=%f",2*pi*r);
}

  
Output
Perimeter=43.960003
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Check whether an alphabet is Vowel or not

 Sumit Kar     June 28, 2015     Basic, If Else     No comments   

#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
Enter Character: A
vowel
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print the ASCII values of all the English Alphabets

 Sumit Kar     June 28, 2015     Basic, For Loop     No comments   

#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
===========================
|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 || W| 87 |
|x | 120 || X| 88 |
|y | 121 || Y| 89 |
|z | 122 || Z| 90 |
===========================
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Friday, June 26, 2015

Find Area and Circumference of a Circle

 Sumit Kar     June 26, 2015     Basic, Macro     No comments   

#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
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
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Friday, May 22, 2015

Print the Truth Table

 Sumit Kar     May 22, 2015     Basic, If Else, Switch Case, User Defined Function     No comments   

#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\nA \tO");
for(a=0;a<=1;a++)
{
o=NOT(a);
printf("\n%d \t%d",a,o);
}
break;
case 4:
printf ("\nTruth Table for XOR\nA B \tO");
for(a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
o=XOR(a,b);
printf("\n%d %d \t%d",a,b,o);
}
}
break;

case 0: exit();
break;
default: printf("\nError");
}
}while(ch!=0);

}


int OR(int a, int b)
{
if(a==0&&b==0)
return 0;
else
return 1;
}

int AND(int a, int b)
{
if (a==1&&b==1)
return 1;
else
return 0;
}

int NOT(int a)
{
if (a==0)
return 1;
else
return 0;
}

int XOR(int a,int b)
{
if (a==0&&b==1||a==1&&b==0)
return 1;
else
return 0;
}

  

Output
-:MENU:-
1.AND
2.OR
3.NOT
4.XOR
0.Exit
Enter Choice: 3
Truth Table for NOT
A O
1 0
0 1
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Thursday, May 21, 2015

Simple Interest

 Sumit Kar     May 21, 2015     Basic     No comments   

#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
Principle amount : 5000
Interest : 10
Time (in year) : 3
You will get interest :Rs. 1500.00
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print the Multiplication Table

 Sumit Kar     May 21, 2015     Basic, While Loop     No comments   

#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
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
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find the Greatest of Three Numbers

 Sumit Kar     May 21, 2015     Basic, If Else     No comments   

#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
Enter values of x, y and z : 4, 9, 2
9 is greatest
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find Greatest number using Conditional Operator

 Sumit Kar     May 21, 2015     Basic, Conditional Operator, Tricky     No comments   

#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
Enter Numbers (n1 n2 n3) : 4 9 2
Greatest= 9
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print 1 to 10 using For Loop

 Sumit Kar     May 21, 2015     Basic, For Loop     No comments   

#include<stdio.h>
int main()
{
int x=1;

for(x=1; x<=10; x++)
printf("\n%d",x);

return 0;
}

  

Output
1
2
3
4
5
6
7
8
9
10
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print 1 to 10 using While Loop

 Sumit Kar     May 21, 2015     Basic, While Loop     No comments   

#include<stdio.h>
#include<conio.h>
int main()
{
int x=1;
while(x<=10)
{
printf("\n%d",x);
x++;
}
getch();
return 0;
}

  

Output
1
2
3
4
5
6
7
8
9
10
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Swap two Numbers

 Sumit Kar     May 21, 2015     Basic     No comments   

#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
Enter Values x and y : 5 4
Value of x= 4
Value of y= 5
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Sum of two numbers

 Sumit Kar     May 21, 2015     Basic     No comments   

#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
Enter Numbers a and b :5 4
5 + 4 = 9
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print "Hello, World!" without semicolon

 Sumit Kar     May 21, 2015     Basic, Tricky     No comments   

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
Hello, World!
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print "Hello, World!"

 Sumit Kar     May 21, 2015     Basic     No comments   

#include<stdio.h>

int main()
{
printf("\nHello, World! ");
return 0;
}

  

Output
Hello, World!
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

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

 Sumit Kar     May 21, 2015     Basic     No comments   

#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
Enter Your Name : Sumit
Enter Your Age : 21

Your Name is Sumit and your age is 21

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Thursday, May 14, 2015

Normalization

 Sumit Kar     May 14, 2015     DBMS, featured, Programming     No comments   



Normalization













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-75


DBMS


SB


89


KBC


CS-75


DBMS


SB


25


ABC


CS-75


DBMS


SB


48


XYZ


CS-30


CA


MC


57


TKC


CS-80


OS


JB


120


SKL


CS-80


OS


JB


68





CS-90


FLAT


LD







Update anomaly: An update anomaly is a data inconsistency that results from data redundancy and a partial update. For example, if there is a change in the name of the instructor for CS-75, we need to make change for all the rows. If we forget to update a single row the database will show two instructor names for the Course CS-75.





Deletion anomaly:  This means loss of useful information. In some cases it may occur that some useful data is lost. For example, consider the row for RollNo 48. XYZ is the only student who has opted for CS-30. If XYZ leaves the institute and the data is deleted for XYZ, the associated data for Course will also be deleted.





Insertion anomaly: An insertion anomaly is the inability to add data to the database due to absence of other data. For example, assume that the above table is defined so that null values are not allowed. If a course is added but not immediately students opt for the course, then this course could not be entered into the database (68). This results in database inconsistencies due to omission.





In database tables are normalized for the following reasons:


To allow data retrieval at an optimal speed


Data maintenance through update, insertion and deletion.


To reduce the need to restructure tables for new applications





A functional dependency is a constraint between two sets of attributes in a relation from a database. It occurs when an attribute in a relation uniquely determines another attribute.


In a given relation R, X and Y are attributes. Attribute Y is functionally dependent on attribute X if each value of X determines EXACTLY ONE value of Y, which is represented as X → Y (X can be composite in nature).


We say here “X determines Y” or “y is functionally dependent on x”.


 X→Y does not imply Y→X.


If the value of an attribute “Marks” is known then the value of an attribute “Grade” is determined since Marks→ Grade







RollNo


StudentName


Marks


Grade


59


ABC


90


A


96


XYZ


70


B







Types of functional dependencies:


1.       Full Functional dependency: X and Y are attributes. X Functionally determines Y (Note: Subset of X should not functionally determine Y)


2.       Partial Functional dependency:  X and Y are attributes. Attribute Y is partially dependent on the attribute X only if it is dependent on a sub-set of attribute X.


3.       Transitive dependency: X Y and Z are three attributes. X -> Y, Y-> Z => X -> Z








Now consider the following Relation


REPORT (STUDENT#, COURSE#, CourseName, IName, Room#, Marks, Grade)


• STUDENT# - Student Number


• COURSE# - Course Number


• CourseName - Course Name


• IName - Name of the Instructor who delivered the course


• Room# - Room number which is assigned to respective Instructor


• Marks - Scored in Course COURSE# by Student STUDENT#


• Grade - obtained by Student STUDENT# in Course COURSE#





The Functional Dependencies are:


• STUDENT# COURSE# → Marks


• COURSE# → CourseName,


• COURSE# → IName (Assuming one course is taught by one and only one


Instructor)


• IName → Room# (Assuming each Instructor has his/her own and non-shared room)


• Marks → Grade


           In above example Marks is fully functionally dependent on STUDENT# COURSE# and not on subset of STUDENT# COURSE#. This means Marks cannot be determined either by STUDENT# OR COURSE# alone. It can be determined only using STUDENT# AND COURSE# together. Hence Marks is fully functionally dependent on STUDENT# COURSE#. CourseName is not fully functionally dependent on STUDENT# COURSE# because subset of STUDENT# COURSE# i.e only COURSE# determines the CourseName and STUDENT# does not have any role in deciding CourseName. Hence CourseName is not fully functionally dependent on STUDENT# COURSE#.


Now CourseName, IName, Room# are partially dependent on composite attributes STUDENT# COURSE# because COURSE# alone defines the CourseName, IName, Room#.


Again, Room# depends on IName and in turn IName depends on COURSE#. Hence Room# transitively depends on COURSE#. Similarly Grade depends on Marks, in turn Marks depends on STUDENT# COURSE# hence Grade depends Fully transitively on STUDENT# COURSE#.





Now consider this table:







Student Data


Course Details


Result


STUDENT#


StudentName


COURSE#


CourseName


IName


Room#


Marks


Grade































First normal form (1NF)


A relation schema is in 1NF if:


·         If and only if all the attributes of the relation R are atomic in nature.


·         Atomic: the smallest level to which data may be broken down and remain meaningful.


In relational database design it is not practically possible to have a table which is not in 1NF.






STUDENT#


StudentName


COURSE#


CourseName


IName


Room#


Marks


Grade































Second normal form (2NF)


A Relation is said to be in Second Normal Form if and only if:


·         It is in the First normal form, and


·         No partial dependency exists between non-key attributes and key attributes.


An attribute of a relation R that belongs to any key of R is said to be a prime attribute and that which doesn’t is a non-prime attribute To make a table 2NF compliant, we have to remove all the partial dependencies


Note: - All partial dependencies are eliminated






STUDENT#


StudentName

















COURSE#


CourseName


IName


Room#























STUDENT#


COURSE#


Marks


Grade



















Third normal form (3 NF)


A relation R is said to be in the Third Normal Form (3NF) if and only if:


·         It is in 2NF and


·         No transitive dependency exists between non-key attributes andkey attributes.


• STUDENT# and COURSE# are the key attributes.


• All other attributes, except grade are nonpartially, non-transitively dependent on key attributes.









STUDENT#


StudentName

















COURSE#


CourseName


IName


Room#























STUDENT#


COURSE#


Marks




















Marks (Lower Bound)


Marks (Upper Bound)


Grade













Boyce-Codd Normal form (BCNF)


A relation is said to be in Boyce Codd Normal Form (BCNF)


·         If and only if all the determinants are candidate keys.


BCNF relation is a strong 3NF, but not every 3NF relation is BCNF.


Consider the table:






Student#


EmailID


Course#


Marks


ABC


abc@sumitkar.in


CS-75


98







Candidate Keys for the relation are [C#, S#] and [C#, Email]. Since Course # is overlapping, it is referred as Overlapping Candidate Key. Valid Functional Dependencies are:


S#  → EmailID


EmailID → S#


S#, C# → Marks


C#, EmailID →Marks






Student#


EmailID


ABC


abc@sumitkar.in











Student#


Course#


Marks


ABC


CS-75


98


















Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp
Newer Posts Older Posts Home

Pages

  • Home

Trending Now

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • Addition of two numbers on Server sent from Client [TCP] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • Concatenation of two strings sent from Client on the Server - [ TCP ] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Blog Archive

  • ►  2020 (1)
    • ►  May (1)
  • ▼  2015 (92)
    • ▼  October (4)
      • Find the GCD of two numbers
      • Find the Factorial of a number
      • Solve All-Pairs Shortest Path Problem using Floyd ...
      • Binary Search using Recursion
    • ►  September (3)
      • Find the maximum length of the subarray in increas...
      • Spiral Matrix
      • Find the maximum occurrence of a character in a st...
    • ►  August (3)
      • Basic Structure of a C Program
      • Get your hands dirty
      • Basics of C
    • ►  July (9)
      • Accept Array elements
      • Clear Screen without clrscr() function call
      • Find Perimeter of a Circle. Take the value of the ...
      • Use Macro to define the value of PI and Find the P...
      • Print Pattern 5 
      • Print Pattern 4 
      • Print Pattern 3 
      • Print Pattern 2 
      • Print Pattern 1 
    • ►  June (9)
      • List all the Prime Numbers
      • Solve Quadratic Equation
      • Find the greatest of two numbers using Conditional...
      • Generate the Fibonacci Series
      • Print 1-10 using do-while loop
      • Find the perimeter of a Circle
      • Check whether an alphabet is Vowel or not
      • Print the ASCII values of all the English Alphabets
      • Find Area and Circumference of a Circle
    • ►  May (20)
      • Print the Truth Table
      • Simple Interest
      • Print the Multiplication Table
      • Find the Greatest of Three Numbers
      • Find Greatest number using Conditional Operator
      • Print 1 to 10 using For Loop
      • Print 1 to 10 using While Loop
      • Swap two Numbers
      • Sum of two numbers
      • Print "Hello, World!" without semicolon
      • Print "Hello, World!"
      • Accept Name, Age from User and Print them on the S...
      • Normalization
    • ►  April (7)
    • ►  March (22)
    • ►  February (7)
    • ►  January (8)
  • ►  2014 (158)
    • ►  November (70)
    • ►  October (6)
    • ►  September (82)
Powered by Blogger.

Categories

C - Programming Java Programming Basic Technology 8085 Assembly Programming For Loop Numerical Methods WhatsApp Algorithm Shell Programming Programming Networking Windows Android C++ Programming CPP If Else Tricky Internet Microsoft Pattern Photography Socket Program News While Loop Array DBMS DS Macro Recursion User Defined Function Conditional Operator Data Structure Durga Puja Earthquake Google Mela Nokia SQL Share Yahoo Airtel Bio Command Prompt Confused Facebook Finance Firefox Ganges Graph HokKolorob Input Kolkata MCQ Math Matrix NetNeutrality Oracle Religion Search Sumit Kar Survey Switch Case Viral Virus Visual Studio do-while featured featured_slider

Popular Programs

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • Addition of two numbers on Server sent from Client [TCP] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • Concatenation of two strings sent from Client on the Server - [ TCP ] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Daily Hits

Copyright © Sumit Kar