Friday, 23 September 2016

ASCII Character Codes


DecHexOctCharDescription
00000null
11001start of heading
22002start of text
33003end of text
44004end of transmission
55005enquiry
66006acknowledge
77007bell
88010backspace
99011horizontal tab
10A012new line
11B013vertical tab
12C014new page
13D015carriage return
14E016shift out
15F017shift in
1610020data link escape
1711021device control 1
1812022device control 2
1913023device control 3
2014024device control 4
2115025negative acknowledge
2216026synchronous idle
2317027end of trans. block
2418030cancel
2519031end of medium
261A032substitute
271B033escape
281C034file separator
291D035group separator
301E036record separator
311F037unit separator
3220040space
3321041!
3422042"
3523043#
3624044$
3725045%
3826046&
3927047'
4028050(
4129051)
422A052*
432B053+
442C054,
452D055-
462E056.
472F057/
48300600
49310611
50320622
51330633
52340644
53350655
54360666
55370677
56380708
57390719
583A072:
593B073;
603C074<
613D075=
623E076>
633F077?
6440100@
6541101A
6642102B
6743103C
6844104D
6945105E
7046106F
7147107G
7248110H
7349111I
744A112J
754B113K
764C114L
774D115M
784E116N
794F117O
8050120P
8151121Q
8252122R
8353123S
8454124T
8555125U
8656126V
8757127W
8858130X
8959131Y
905A132Z
915B133[
925C134\
935D135]
945E136^
955F137_
9660140`
9761141a
9862142b
9963143c
10064144d
10165145e
10266146f
10367147g
10468150h
10569151i
1066A152j
1076B153k
1086C154l
1096D155m
1106E156n
1116F157o
11270160p
11371161q
11472162r
11573163s
11674164t
11775165u
11876166v
11977167w
12078170x
12179171y
1227A172z
1237B173{
1247C174|
1257D175}
1267E176~
1277F177DEL

Matrix Transpose

/* Transpose the matrix */
#include<stdio.h>

int main()
{
int a[4][4],i,j,b;

for(i=0;i<4;i++)
{
printf("\nEnter elements of %d row of Matrix: ",i+1);
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}

for(i=0;i<4;i++)
{
for(j=i+1;j<4;j++)
{
b=a[i][j];
a[i][j]=a[j][i];
a[j][i]=b;
}
}

printf("\nTransposed Matrix:\n\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%4d",a[i][j]);
printf("\n");
}
return 0;
}

Sum of two Matrix

/* Sum of two Matrix */
#include<stdio.h>

int main()
{
int a[5][5],b[5][5],c[5][5];
int i,j;

for(i=0;i<5;i++)
{
printf("\nEnter elements of %d row of first Matrix: ",i+1);
for(j=0;j<5;j++)
scanf("%d",&a[i][j]);
}

for(i=0;i<5;i++)
{
printf("\nEnter elements of %d row of 2nd Matrix: ",i+1);
for(j=0;j<5;j++)
scanf("%d",&b[i][j]);
}

printf("\nSum of Matrix:\n\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%3d ",c[i][j]);
}
printf("\n");
}
return 0;
}

Matrix Multiplication

/* Matrix multiplication */
#include<stdio.h>

int main()
{
int arr1[3][3],arr2[3][3],arr3[3][3]={0},i,j,k;

printf("Type a matrix of 3 rows & 3 colomns :\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&arr1[i][j]);

printf("Type another matrix of 3 rows & 3 colomns :\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&arr2[i][j]);

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
arr3[i][j]=arr3[i][j]+arr1[i][k]*arr2[k][j];
}

}

printf("\n\nOutput:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",arr3[i][j]);
printf("\n\n");
}

return 0;
}

Sort array of 10 strings

/* Sort array of 10 strings. */

#include<string.h>
#include<stdio.h>

int main()
{
int i,j;
char str[10][10],temp[10];
printf("Type 10 names :\n");
for(i=0;i<10;i++)
{
// gets(str[i]);
// fgets is a better option over gets to read multiword string .
fgets(str[i], 10, stdin);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-1-i;j++)
{
if(strcmpi(str[j],str[j+1])>0)
{
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}
}

printf("\nSorted Names :\n");
for(i=0;i<10;i++)
puts(str[i]);
return 0;
}

Insertion Sort

/* Insertion Sort */

#include<stdio.h>

int main()
{
int arr[10],i,j,new;
printf("Please enter 10 values:\n");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);

for(i=1;i<10;i++)
{
new=a[i];
for(j=i-1;j>=0&&new<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=new;
}

printf("Sorted Array is:\n");
for(i=0;i<10;i++)
printf("%d\n",arr[i]);

return 0;
}

Selection Sort

/* Selection Sort */

#include<stdio.h>

int main()
{
int arr[10],i,j,k,t;
printf("Please enter 10 values:\n");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);

for(i=0;i<10;i++)
{
k=i;
for(j=i;j<10;j++)
{
if(a[k]>a[j])
k=j;
}
if(k!=i)
{
t=a[k];
a[k]=a[i];
a[i]=t;
}
}

printf("Sorted Array is:\n");
for(i=0;i<10;i++)
printf("%d\n",arr[i]);

return 0;
}

Bubble Sort

/* Bubble Sort */

#include<stdio.h>

int main()
{
int arr[10],i,j,t;
printf("Please enter 10 values:\n");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);

for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if (arr[j]>arr[j+1])
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}

printf("Sorted Array is:\n");
for(i=0;i<10;i++)
printf("%d\n",arr[i]);

return 0;
}

Binary Search

/* Binary Search */

#include<stdio.h>

int main()
{
int arr[10],i,max,min,mid,val,index;

printf("Please enter 10 values in ascending order:\n");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);

printf("\nEnter a value to be searched: ");
scanf("%d",&val);

max=9;
min=0;
index=-1;
while(min<=max)
{
mid=(max+min)/2;
if(val==arr[mid])
{
index=mid;
break;
}
if(arr[mid]>val)
max=mid-1;
else
min=mid+1;
}

if(index>=0)
printf("Value found in Array at %d location",index);
else
printf("Value not found in Array");
return 0;
}

File Copy using command line arguments

/* File Copy using command line arguments */

#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fs,*ft;
int ch;
if(argc!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
printf("Can't find the source file.");
return 1;
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
printf("Can't open target file.");
fclose(fs);
return 1;
}

while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}

fclose(fs);
fclose(ft);
return 0;
}

File to upper case using command line arguments

/* File to upper case using command line arguments */

#include<stdio.h>
int main(int n,char *a[])
{
int c;
FILE *fr,*fw;

if(n!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}

if((fr=fopen(a[1],"r"))==NULL)
{
printf("File can't be open.");
return 1;
}
if((fw=fopen(a[2],"r+"))==NULL)
{
printf("File can't be open.");
fclose(fr);
return 1;
}
while(1)
{
c=fgetc(fr);
if(feof(fr)) break;
c=~c;
fputc(c,fw);
}
fclose(fr);
fclose(fw);
return 0;
}

Count characters of file

/* Count characters of file */

#include<stdio.h>
int main()
{
FILE *fp;
char a[10];
long cnt=0;
int c;

printf("Type a file name : ");
gets(a);

if((fp=fopen(a,"r"))==NULL)
printf("File dosen't exist.");
else
{
while(1)
{
c=fgetc(fp);
if(feof(fp)) break;
cnt++;
}
printf("\nfile have %ld characters",cnt);
}
fclose(fp);
return 0;
}

Write to text file

/* Write to text file */

#include<stdio.h>
int main()
{
FILE *fp;
char mystr[100];

fp=fopen("mytext.txt","w");
if(fp==NULL)
{
puts("Some error occured.");
return 1;
}

printf("\nEnter some lines:\n");
while(strlen(gets(mystr))>0)
{
fputs(mystr,fp);
fputs("\n",fp);
}
fclose(fp);
return 0;
}

Read text file

/* Read text file */

#include<stdio.h>
int main()
{
FILE *fp;
int ch;

fp=fopen("myfile.txt","r");
if(fp==NULL)
{
printf("Can't find the source file.");
return 1;
}
while(1)
{
ch=fgetc(fp);
if(feof(fp)) break;
printf("%c",ch);
}
fclose(fp);
return 0;
}

Towers of Hanoi by recursion

/* Towers of Hanoi by recursion */

#include<stdio.h>
void toh(int,char,char,char);

int main()
{
int n=3;
toh(n,'A','B','C');
return 0;
}

void toh(int n,char a,char b,char c)
{
if(n==1)
printf("\nMoved from %c to %c",a,c);
else
{
toh(n-1,a,c,b);
toh(1,a,' ',c);
toh(n-1,b,a,c);
}
}

Binary by recursion

/* Binary by recursion */

#include<stdio.h>
void binary(long);
int main()
{
long n;
printf("Type a value : ");
scanf("%ld",&n);
binary(n);
return 0;
}

void binary(long n)
{
if(n>1)
binary(n/2);
printf("%ld",n%2);
}

GCD by Recursion

/* greatest common divisor by recursion */
#include<stdio.h>
int gcd(int,int);

int main()
{
int a,b;
printf("Type 2 values to find GCD :\n");
scanf("%d %d",&a,&b);
printf("GCD : %d",gcd(a,b));
return 0;
}

int gcd(int m,int n)
{
if(n>m) return gcd(n,m);
if(n==0) return m;
return gcd(n,m%n);
}

 

Sum of digit by Recursion

/* Sum of digit by recursion */

#include<stdio.h>
int sod(int);

int main()
{
int i;
printf(" Type any value : ");
scanf("%d",&i);
printf("Sum of digit : %d",sod(i));
return 0;
}

int sod(int n)
{
if(n<1)
return 0;
return(n%10+sod(n/10));
}

Reverse by Recursion

/* Reverse by Recursion */

#include<stdio.h>
int rev(int,int);

int main()
{
int a;
printf("Type a value : ");
scanf("%d",&a);
printf("Reverse: %d",rev(a,0));
return 0;
}

int rev(int i,int r)
{
if(i > 0)
return rev(i/10,(r*10)+(i%10));
return r;
}

Fibonacci by Recursion

/* Fibonacci by Recursion */

#include<stdio.h>
int fib(int);

int main()
{
printf("Type any value : ");
printf("\nNth value: %d",fib(getche()-'0'));
return 0;
}

int fib(int n)
{
if(n<=1)
return n;
return(fib(n-1)+fib(n-2));
}

Power by Recursion

/* Power by Recursion */

#include<stdio.h>
int pow(int,int);

int main()
{
int i,j;
printf("Type two values : \n");
scanf("%d %d",&i,&j);

printf("i pow j = %d",pow(i,j));
return 0;
}

int pow(int i,int j)
{
if(j==1)
return i;
return (i*pow(i,j-1));
}

Factorial by Recursion

/* Factorial by Recursion */

#include<stdio.h>
int fact(int);

int main()
{
int n;
printf("Type any value : ");
scanf("%d",&n);
n=fact(n);
printf("\nFactorial : %d ",n);
return 0;
}

int fact(int x)
{
if(x==1)
return(x);
else
x=x*fact(x-1);
}

Series Program : 1 3 8 15 27 50 92 169 311

1 3 8 15 27 50 92 169 311
void main()
{
int a=1, b=3, c=4, n=10, sum, i;
printf("%d %d ",a,b,c);
for(i=4; i<=n; i++)
{
sum = a+b+c;
printf("%d ",sum);
a = b;
b = c;
c = sum;
}
}

Series Program : 2 15 41 80 132 197 275 366 470 587

2 15 41 80 132 197 275 366 470 587

 
int main()
{
int a=2,i,n=10;
for(i=1;i<=n;i++)
{
printf("%d ",a);
a+=13*i;
}
return 0;
}

Series Program : 1 2 3 6 9 18 27 54...

1 2 3 6 9 18 27 54...
int main()
{
int a=1,b=2,i,n=10;
printf("%d %d ",a, b);
for(i=3;i<=n;i++)
{
if(i%2==1)
{
a=a*3;
printf("%d ",a);
}
else
{
b=b*3;
printf("%d ",b);
}
}
return 0;
}

Series Program : 1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n

1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n
int main()
{
double i, n,sum=0;
n=10;
for(i=1;i<=n;i++)
{
if ((int)i%2==1)
sum+=i/(i+1);
else
sum-=i/(i+1);
}
printf("Sum: %lf",sum);
return 0;
}

Series Program : [(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n]

[(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n]
long power(int a, int b)
{
long i, p=1;
for(i=1;i<=b;i++)
{
p=p*a;
}
return p;
}

int main()
{
long i,n;
double sum=0;
n=5;
for(i=1;i<=n;i++)
{
sum=sum+(power(i,i)/i);
}
printf("Sum: %lf",sum);
return 0;
}

Series Program : (1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)

(1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)
long fact(int n)
{
long i, f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

int main()
{
long i,n;
double sum=0;
n=5;
for(i=1;i<=n;i++)
{
sum=sum+(fact(i)/i);
}
printf("Sum: %lf",sum);
return 0;
}

Series Program : (1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)

(1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)
long power(int a, int b)
{
long i, p=1;
for(i=1;i<=b;i++)
{
p=p*a;
}
return p;
}

int main()
{
long i,n,sum=0;
n=5;
for(i=1;i<=n;i++)
{
sum=sum+power(i,i);
}
printf("Sum: %d",sum);
return 0;
}

Series Program : 1! + 2! + 3! + 4! + 5! + ... + n!

1! + 2! + 3! + 4! + 5! + ... + n!
long fact(int n)
{
long i, f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

int main()
{
long i,n,sum=0;
n=5;
for(i=1;i<=n;i++)
{
sum=sum+fact(i);
}
printf("Sum: %d",sum);
return 0;
}

Series Program : (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)
int main()
{
int i,j,n,sum=0;
n=10;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
sum+=j;
}
}
printf("Sum: %d",sum);
return 0;
}

Series Program : (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)

(1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
int main()
{
int i,n,sum=0;
n=10;
for(i=1;i<=n;i++)
{
sum+=i*i;
}
printf("Sum: %d",sum);
return 0;
}

series program : 1 + 2 + 3 + 4 + 5 + ... + n

1 + 2 + 3 + 4 + 5 + ... + n
int main()
{
int i,n,sum=0;
n=10;
for(i=1;i<=n;i++)
{
sum+=i;
}
printf("Sum: %d",sum);
return 0;
}

Alphabet pattern : ABCDE ABCD ABC AB A

ABCDE
ABCD
ABC
AB
A


#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%c",'A' + j-1);
}
printf("\n");
}

return 0;
}

Alphabet pattern : ABCDE BCDE CDE DE E

ABCDE
BCDE
CDE
DE
E


#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
printf("%c", 'A'-1 + j);
}
printf("\n");
}

return 0;
}

Alphabet pattern : EDCBA DCBA CBA BA A

EDCBA
DCBA
CBA
BA
A


#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%c",'A'-1 + j);
}
printf("\n");
}

return 0;
}

Alphabet pattern : EDCBA EDCB EDC ED E

EDCBA
EDCB
EDC
ED
E


#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%c",'A'-1 + j);
}
printf("\n");
}

return 0;
}

Alphabet pattern : A BB CCC DDDD EEEEE

A
BB
CCC
DDDD
EEEEE


#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}

return 0;
}

Alphabet pattern : E DD CCC BBBB AAAAA

E
DD
CCC
BBBB
AAAAA


#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}

return 0;
}

Alphabet pattern : EEEEE DDDD CCC BB A

EEEEE
DDDD
CCC
BB
A


#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}

return 0;
}

Alphabet pattern : AAAAA BBBB CCC DD E

AAAAA
BBBB
CCC
DD
E


#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}

return 0;
}

Alphabet pattern : A AB ABC ABCD ABCDE

A
AB
ABC
ABCD
ABCDE


#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",'A' + j-1);
}
printf("\n");
}

return 0;
}

Alphabet pattern : E DE CDE BCDE ABCDE

E
DE
CDE
BCDE
ABCDE


#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=i;j<=5;j++)
{
printf("%c",'A' + j-1);
}
printf("\n");
}

return 0;
}

Alphabet Pattern : A BA CBA DCBA EDCBA

A
BA
CBA
DCBA
EDCBA


#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=i;j>=1;j--)
{
printf("%c",'A' + j-1);
}
printf("\n");
}

return 0;
}

Alphabet Pattern E ED EDC EDCB EDCBA

E
ED
EDC
EDCB
EDCBA


#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%c",'A' + j-1);
}
printf("\n");
}

return 0;
}

Tuesday, 20 September 2016

Star Pattern : Pure Diamond

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

CODE : Awesome Logic :

int main()
{
int i, j;

for(i=1; i<=5; i++)
{
for(j=5; j>i; j--)
{
printf(" ");
}
printf("*");
for(j=1; j<(i-1)*2; j++)
{
printf(" ");
}
if(i==1) printf("\n");
else printf("*\n");
}

for(i=4; i>=1; i--)
{
for(j=5; j>i; j--)
{
printf(" ");
}
printf("*");
for(j=1; j<(i-1)*2; j++)
{
printf(" ");
}
if(i==1) printf("\n");
else printf("*\n");
}

return 0;
}

Star Pattern : top left edge diamond

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

#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}

return 0;
}

Star pattern : Top right edge diamond

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


#include <stdio.h>

int 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");
}

return 0;
}

Star Pattern : bottom left edge diamond

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


#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}

return 0;
}

Star Pattern : bottom right edge diamond

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


#include <stdio.h>

int main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}

return 0;
}

Star Pattern : Half Diamond

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

#include <stdio.h>

int main()
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}

return 0;
}

Star Pattern : Bottom half Diamond

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

#include <stdio.h>

int main()
{
    int i, j, k;
    for(i=5;i>=1;i--)
    {
        for(j=5;j>i;j--)
        {
                printf(" ");
        }
        for(k=1;k<(i*2);k++)
        {
                printf("*");
        }
        printf("\n");
    }

    return 0;
}

Star Pattern : Diamond

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

CODE :
#include <stdio.h>

int main()
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}

return 0;


Star Pattern : Hollow Diamond

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

CODE:

#include <stdio.h>

int main()
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=1;j<=6-i;j++)
{
printf("*");
}
for(k=1;k<i;k++)
{
printf(" ");
}
for(j=1;j<=6-i;j++)
{
printf("*");
}
printf("\n");
}
for(i=2;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
for(k=1;k<=5-i;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}

return 0;
}

Sunday, 18 September 2016

Number pattern : Pyramid of square of 1 to 25

                  1  
4 9 16
25 36 49 64 81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625

#include<stdio.h>

int main()
{
int i, j, k=1;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(j=1;j<(i*2);j++)
{
printf("%3d ",k*k);
k++;
}
printf("\n");
}
return 0;
}

Number pattern : rectangle of 1

11111
1 1
1 1
1 1
11111



#include<stdio.h>

int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j==5 || j==1 || i==1 || i==5)
printf("1");
else
printf(" ");
}
printf("\n");
}
return 0;
}

Number pattern : Stuff Diamond

    1
123
12345
1234567
123456789
1234567
12345
123
1

CODE:
#include<stdio.h>

int main()
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
return 0;
}

middle Pyramid number pattern

    1 
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<stdio.h>

int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}

Pyramid top view using 1 to 5

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5

int main()
{
int i, j, n=5;

for(i=n; i>1; i--)
{
for(j=n;j>=1;j--)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
for(j=2;j<=n;j++)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
printf("\n");
}
for(i=1; i<=n; i++)
{
for(j=n;j>=1;j--)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
for(j=2;j<=n;j++)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
printf("\n");
}

return 0;
}

1 to 100 : Left edge pyramid : number pattern

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100

int main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",i*j);
}
printf("\n");
}
return 0;
}

Number pattern : X

1     1
2 2
3 3
4
3 3
2 2
1 1

CODE

int main()
{
int i,j,k=1;
int m[7][7]={0};
for(i=1;i<=7;i++)
{
for(j=1;j<=7;j++)
if(j==i || 8-i==j)
m[i-1][j-1]=k;
if(i<4) k++;
else --k;

}
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(m[i][j]==0)
printf(" ");
else
printf("%d",m[i][j]);
}
printf("\n");
}
return 0;
}

number pattern : diamond

      1
2 2
3 3
4 4
3 3
2 2
1


Code:

int main()
{
int i,j,k;
for(i=1;i<=4;i++)
{
for(j=4;j>=(i-1)*2-1;j--)
printf(" ");
printf("%d",i);
for(j=2;j<=(i-1)*4;j++)
printf(" ");
if(i>1)
printf("%d",i);
printf("\n");
}
for(i=3;i>=1;i--)
{
for(j=4;j>=(i-1)*2-1;j--)
printf(" ");
printf("%d",i);
for(j=2;j<=(i-1)*4;j++)
printf(" ");
if(i>1)
printf("%d",i);
printf("\n");
}
return 0;
}

Square number pattern 1 to 16

1  2  3  4  5  
16 6
15 7
14 8
13 12 11 10 9

CODE

int main()
{
int i,j,k=6,l=13,m=16;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(i==1)
printf("%-3d",j);
else if(j==5)
printf("%-3d",k++);
else if(i==5)
printf("%-3d",l--);
else if(j==1)
printf("%-3d",m--);
else
printf(" ");
}
printf("\n");
}
return 0;
}



Number pattern N=39714

N=39714

3 9 1 7 4
9 1 7 4
1 7 4
7 4
4

code:

#include <stdio.h>

int main()
{
long n = 39714, i=1;
for(i=10;i<n;i*=10);

for (i=i/10; n>0; i/=10)
{
printf("%d\n", n);
n%=i;
}

return 0;
}



Number pattern :- 1, 23, 456, 78910

1
23
456
78910

#include<stdio.h>
int main()
{
int i,j,k;
k=1;
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",k++);
}
printf("\n");
}
return 0;
}

Number pattern 1 10 101 1010 10101

1
10
101
1010
10101

#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j%2);
}
printf("\n");
}
return 0;
}

left edge pyramid number pattern

1
123
12345
1234567

#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=7;i+=2)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}

Number pattern : left edge pyramid

1 
2 6
3 7 10
4 8 11 13
5 9 12 14 15

#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
k = i;
for(j=1;j<=i;j++)
{
printf("%d ", k);
k += 5-j;
}
printf("\n");
}
return 0;
}

Number pattern : Pyramid star and Numbers

12344321
123**321
12****21
1******1

#include<stdio.h>
int main()
{
int i,j,k;
for(i=4;i>=1;i--)
{
for(j=1;j<=4;j++)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
for(j=4;j>=1;j--)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
printf("\n");
}
return 0;
}

Number pattern pyramid

    1
2 3 4
5 6 7 8 9

#include<stdio.h>
int main()
{
int i,j,k;
k=1;
for(i=1;i<=5;i+=2)
{
for(j=5;j>=1;j--)
{
if(j>i)
printf(" ");
else
printf("%d ",k++);
}
printf("\n");
}
return 0;
}

number pattern : a perfect square

1   2   3   4   5   6   7   8   9   10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63 84 85 86 87 88 71 46 13
33 62 83 96 97 98 89 72 47 14
32 61 82 95 100 99 90 73 48 15
31 60 81 94 93 92 91 74 49 16
30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19

 
#include<stdio.h>

int main()
{
int a[10][10]={0},i,j,low=0,top=9,n=1;
for(i=0;i<5;i++,low++,top--)
{
for(j=low;j<=top;j++,n++)
a[i][j]=n;
for(j=low+1;j<=top;j++,n++)
a[j][top]=n;
for(j=top-1;j>=low;j--,n++)
a[top][j]=n;
for(j=top-1;j>low;j--,n++)
a[j][low]=n;
}
printf("\t\t\t\tPerfect Square\n");
for(i=0;i<10;i++)
{
printf("\n\n\t");
for(j=0;j<10;j++)
{
printf("%6d",a[i][j]);
delay(300);
}
}
return 0;
}

 

Number pattern double verticle pyramids

1        1
12 21
123 321
1234 4321
1234554321

#include<stdio.h>

int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
for(j=5;j>=1;j--)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
printf("\n");
}
return 0;
}

Number pattern horizontal pyramid

1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d*",i);
else
printf("%d",i);
}
printf(" \n");
}
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d*",i);
else
printf("%d",i);
}
printf(" \n");
}
return 0;
}

Number pattern 1 22 333 4444 55555

1
22
333
4444
55555

#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}

return 0;
}

Number pattern 5 44 333 2222 11111

5
44
333
2222
11111

#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}

return 0;
}

Number pattern 55555 4444 333 22 1

55555
4444
333
22
1

#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}

return 0;
}

Number Pattern 11111 2222 333 44 5

11111
2222
333
44
5

#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}

return 0;
}

Number Pattern 12345 4321 123 21 1

12345
4321
123
21
1

int main()
{
int i,j,k;
for(i=5;i>=1;i--)
{
if(i%2==1) k=1;
else k=i;
for(j=1;j<=i;j++)
{
printf("%d",k);
if(i%2==1) k++;
else k--;
}
printf("\n");
}
return 0;
}

Number Pattern 1234567 12345 123 1

1234567
12345
123
1

int main()
{
int i,j;
for(i=7;i>=1;i-=2)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}

Number Pattern 1 01 101 0101

1
01
101
0101

#include <stdio.h>
int main()
{
int i, j;
for(i=1;i<=4;i++)
{
for(j=i;j>=1;j--)
{
printf("%d",j%2);
}
printf("\n");
}
return 0;
}

Number Pattern 13579 3579 579 79 9

13579
3579
579
79
9

#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=9;i+=2)
{
for(j=i;j<=9;j+=2)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}

Number Pattern 1, 2 4,1 3 5, 2 4 6 8, 1 3 5 7 9

1
2 4
1 3 5
2 4 6 8
1 3 5 7 9

#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
if(i%2==0)
{
k=2;
}
else
{
k=1;
}
for(j=1;j<=i;j++,k+=2)
{
printf("%d ", k);
}
printf("\n");
}
return 0;
}

Number Pattern 55555 45555 34555 23455 12345

55555
45555
34555
23455
12345

int main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
k = i;
for(j=1;j<=5;j++)
{
if(k <= 5)
{
printf("%d",k);
}
else
{
printf("5");
}
k++;
}
printf("\n");
}
return 0;
}

Number Pattern 12345 2345 345 45 5

12345
2345
345
45
5

code:

#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}

Number Pattern 54321 4321 321 21 1

54321
4321
321
21
1

CODE:
#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}

Number Pattern 54321 5432 543 54 5

54321
5432
543
54
5

CODE:

#include <stdio.h>

int main()
{
    int i, j;
    for(i=1;i<=5;i++)
    {
        for(j=5;j>=i;j--)
        {
            printf("%d",j);
        }
        printf("\n");
    }

    return 0;
}

Number Pattern 1 12 123 1234 12345

1
12
123
1234
12345

Code;

#include <stdio.h>

int main()
{
    int i, j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }

    return 0;
}

Number Pattern 5 45 345 2345 12345

5
45
345
2345
12345

CODE:
#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=i;j<=5;j++)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}

Number Pattern 1 21 321 4321 54321

1
21
321
4321
54321

CODE:
#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}

Number Pattern 5 54 543 5432 54321

5
54
543
5432
54321

CODE : 
#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}

Number Pattern - 12345 1234 123 12 1

12345
1234
123
12
1

Code : 
#include <stdio.h>

int main()
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}

Saturday, 17 September 2016

File or image store into database by gwt

GreetingService.java


package com.client;


import com.google.gwt.user.client.rpc.RemoteService;


importcom.google.gwt.user.client.rpc.RemoteServiceRelativePath;


/**


 * The client side stub for the RPC service.


 */


@RemoteServiceRelativePath("greet")


public interface GreetingService extends RemoteService {


      String upload(String topic, String stream, String Tag, String plink, String fpath );


}


               


GreetinServiceAsync.java


package com.client;


import com.google.gwt.user.client.rpc.AsyncCallback;


/**


 * The async counterpart of <code>GreetingService</code>.


 */


public interface GreetingServiceAsync 
{


  void upload(String topic, String stream, String Tag, String plink, String fpath, AsyncCallback<String> callback );



}


TestExample.java


package com.client;


import com.google.gwt.core.client.EntryPoint;


import com.google.gwt.core.client.GWT;


import com.google.gwt.event.dom.client.ClickEvent;


import com.google.gwt.event.dom.client.ClickHandler;


import com.google.gwt.user.client.Window;


import com.google.gwt.user.client.rpc.AsyncCallback;


import com.google.gwt.user.client.ui.AbsolutePanel;


import com.google.gwt.user.client.ui.Button;


import com.google.gwt.user.client.ui.FileUpload;


import com.google.gwt.user.client.ui.Label;


import com.google.gwt.user.client.ui.ListBox;


import com.google.gwt.user.client.ui.RootPanel;


import com.google.gwt.user.client.ui.TextBox;


/**


 * Entry point classes define <code>onModuleLoad()</code>.


 */


public class TestExample implements EntryPoint


{


private final GreetingServiceAsync a1 = GWT.create(GreetingService.class);


        RootPanel rt;


        AbsolutePanel absolutePanel;


        TextBox textBox;


        ListBox comboBox;


        TextBox textBox_1;


        TextBox textBox_2;


        FileUpload fileUpload;


        Button button;


        String s1,s2,s3,s4,s5;


      public void onModuleLoad()


      {



              rt=RootPanel.get();



              absolutePanel = new AbsolutePanel();


              rt.add(absolutePanel, 218, 24);


              absolutePanel.setSize("445px", "434px");



              textBox = new TextBox();


              absolutePanel.add(textBox, 143, 39);



              Label lblTopic = new Label("Topic");


              absolutePanel.add(lblTopic, 86, 42);



              comboBox = new ListBox();


              comboBox.addItem("Computer Science");


              comboBox.addItem("Environment");


              comboBox.addItem("Physics");


              comboBox.addItem("Chemistry");


              comboBox.addItem("Biology");


              absolutePanel.add(comboBox, 143, 88);


              comboBox.setSize("148px", "24px");



              Label lblStreams = new Label("Streams");


              absolutePanel.add(lblStreams, 69, 88);



              textBox_1 = new TextBox();


              absolutePanel.add(textBox_1, 143, 139);



              Label lblTags = new Label("Tags");


              absolutePanel.add(lblTags, 87, 139);



              textBox_2 = new TextBox();


              absolutePanel.add(textBox_2, 143, 194);



              Label lblPublicLink = new Label("Public Link");


              absolutePanel.add(lblPublicLink, 49, 194);



              fileUpload = new FileUpload();


              absolutePanel.add(fileUpload, 143, 254);


              button = new Button("New button");


              absolutePanel.add(button, 156, 311);



              button.addClickHandler(new ClickHandler()


              {


                  public void onClick(ClickEvent event)


                  {


                        Window.alert("onclick");


                        String topic=textBox.getText();


                   String strm=comboBox.getItemText(comboBox.getSelectedIndex());


                        String tag=textBox_1.getText();


                        String plink=textBox_2.getText();


                        String fpath=fileUpload.getFilename();



        a1.upload(topic, strm, tag, plink, fpath, newAsyncCallback<String>() 
            {



              @Override


             public void onSuccess(String result)


             {


                 // TODO Auto-generated method stub


                  String f="yes";


                  System.out.println(result);


                  // TODO Auto-generated method stub


                  if(result.equals(f))


                  {


                     System.out.println("onsuccess");


                     Window.alert("File Upload");



                  }


                  else


                  {


                     Window.alert("Filis not uploaded");


                  }



           }



           @Override


           public void onFailure(Throwable caught) 
            {


                  // TODO Auto-generated method stub


                  Window.alert("Failure");


            }


        });


      }


   });


              button.setText("Uplod lctr notes");


              button.setSize("133px", "28px");



              Label lblFile = new Label("File");


              absolutePanel.add(lblFile, 98, 254);


              lblFile.setSize("27px", "21px");


    }


}


GreetingServiceimp.java


package com.server;


import java.io.File;


import java.io.FileInputStream;


import java.io.FileOutputStream;


import java.io.IOException;


import java.io.InputStream;


import java.sql.Connection;


import java.sql.DriverManager;


import java.sql.PreparedStatement;


import java.sql.ResultSet;


import java.sql.SQLException;


import java.sql.Statement;


import com.client.GreetingService;


importcom.google.gwt.user.server.rpc.RemoteServiceServlet;


/**


 * The server side implementation of the RPC service.


 */


@SuppressWarnings("serial")


public class GreetingServiceImpl extendsRemoteServiceServlet implements


    GreetingService


{



      Connection con=null;


      Statement st=null;


      ResultSet rs=null;


      String query;



      String url="jdbc:mysql://localhost:3306/gwtlogin";



 public void call()


 {


       try


       {


             Class.forName("com.mysql.jdbc.Driver");


       }


       catch(ClassNotFoundException e)


       {


             System.out.print(e.getMessage());


       }


       try


       {


             con=DriverManager.getConnection(url, "root","");


             st=con.createStatement();


       }


       catch(SQLException e)


       {


             System.out.println(e.getMessage());


       }


 }


      @Override


public String upload(String topic, String stream, String Tag, String plink,


                  String fpath) {


            // TODO Auto-generated method stub


            call();


            String ss="no";


            int k=0;


          FileInputStream fis = null;


          PreparedStatement pstmt = null;



          System.out.println(fpath);


          try


          {


            System.out.println("Hello try");


            con.setAutoCommit(false);


            File file = new File(fpath);


            fis = new FileInputStream(file);


            pstmt = con.prepareStatement("insert into uploadfile(Topic, Stream, Tag, PublicLink, FileBody ) values ( ?, ?, ?, ?, ?)");


            //pstmt.setString(1, id);


            pstmt.setString(1, topic);


            pstmt.setString(2, stream);


            pstmt.setString(3, Tag);


            pstmt.setString(4, plink);


            pstmt.setAsciiStream(5, fis, (int) file.length());


            k=pstmt.executeUpdate();


            System.out.println("Bye");


            if(k==1)


                  {


                        ss="yes";



                  }


            con.commit();


          }


          catch (Exception e)


          {


            System.err.println("Error: " + e.getMessage());


            e.printStackTrace();


          }


          finally {


              try {


                        pstmt.close();


                  } catch (SQLException e) {


                        // TODO Auto-generated catch block


                        e.printStackTrace();


                  }


              try {


                        fis.close();


                  } catch (IOException e) {


                        // TODO Auto-generated catch block


                        e.printStackTrace();


                  }


              try {


                        con.close();


                  } catch (SQLException e) {


                        // TODO Auto-generated catch block


                        e.printStackTrace();


                  }


            }



          return ss;


      }


}


I hope you will able to store file into database. If any error has occurred or any question comes in your mind. Freely ask your problem by comment we just solve your problem within 1 hour.