Wednesday, August 29, 2007

ArmStrong




# include <stdio.h>

int main(){


int no, te, r, sum=0;


printf("No :");

scanf("%d",&no);

te=no;


while(no>0){

r=no%10;

sum=sum+(r*r*r);

no=no/10;


}


if(sum==te){

printf("IT'S an Armstrong No");

}

else{

printf("IT'S NOT A Armstrong No");

}


}


---------------------------------------------------------------------------------

Normal Encryption Decryption


#include <stdio.h>

void main(){

char a[2]="AA";

int aa;

int conv,i;

char name[10],encr[10];

int actConv,aCode=3,uCode;

clrscr();

printf("%s",a);

printf("n%d",a[0]);

aa=a[0];

printf("%d",aa);

printf("%c",aa);

a[1]=aa;

// printf("%c n",a[1]);

//scanf("%s",name);

scanf("%[^n]",name);

for(i=0;name[i]!='0';i++){

conv=name[i];

actConv=conv+aCode;

//encr[i]=conv;

encr[i]=actConv;

// printf("%s t",conv);

// printf("%d t ",conv);

}

printf("nENCRYPTED TEXT n");

for(i=0;name[i]!='0';i++){

//printf("%s t",conv);

printf("%2c ",encr[i]);

}

a:

printf("n Give the decription KEY in NO'S :");

scanf("%d",&uCode);

if(uCode!=aCode){

printf("Oops! Key Error. n TRY AGAIN : ");

goto a;

}

printf("nDECRYPTED TEXT n");

for(i=0;name[i]!='0';i++){

//printf("%s t",conv);

conv=(encr[i])-uCode;

printf("%2c ",conv);

}

getch();

}


---------------------------------------------------------------------------------

Command Line Arguments



# include <stdio.h>

int main(int n,char args[]){

int i;

clrscr();

printf("HI");

printf("n Total %d arguments n ",n);

for(i=0;args[i]!=0;i++){


printf("%s n ",args[i]);


}

getch();

}

---------------------------------------------------------------------------------

Fibonacci



# include <stdio.h>


void main(){

int a,b,c,i;

a=1;

b=2;

c=a+b;

clrscr();


printf ("THe NACCI SEries%d , %d ",a,b);

for (i=0;i<100;i++){

printf(", %d",c);

a=b;

b=c;

c=a+b;

}

getch();

}


---------------------------------------------------------------------------------

Using Unary Operators


# include <stdio.h>


void main(){

int a,b,c;

clrscr();

a=1;

b=++a;

printf("n b=++a %d a=1 NOW %d",b,a);

//a--;

b=a--;

printf("n b=a-- %d a-- %d",b,a);

--a;

b++;

printf("n b++ %d --a %d a++ %d",++b,a,a++);

/* HERE the above a++ wil 1st assign 0 to a++ and now increme n

displa --a value as 1 to

*/

printf("nNEXT b++ %d --a %d a++ %d",++b,a,a++);


getch();

}



---------------------------------------------------------------------------------

LCM



# include <stdio.h>

void main(){

int f,s,i,add=1;

clrscr();

i=2;

f=6;

s=12;

a:

if((f%i==0)&&(s%i==0)){

// if(f%i==0){


f=(int)f/i;

s=(int)s/i;

add=add*i;


}


else if(f%i==0){

f=(int)f/i;

add=add*i;

}

else if(s%i==0) {

s=(int)s/i;

add=add*i;

}


if((f!=1)||(s!=1)){

i++;

//goto a;

}


printf("n THE LCM IS :%d ",add);

getch();


}

---------------------------------------------------------------------------------

LCM


# include <stdio.h>

# include <conio.h>

void main(){

int f,s,i,add=1;

clrscr();

f=6;

s=12;

i=2;

a:

if(f%i==0){

if(s%i==0){

s=(int)s/i;

}

else {

}

add=add*i;

printf("%d n",i);

printf("Total fFFF IS %d",add);

f=(int)f/i;

printf("n %d the divided value of I %d valn",f,i);

if ((f!=1)||(s!=1)){

i++;

goto a;

}

}

else{

i++;

goto a;

}

/*

i=2;

printf("n %d",i);

b:

if(s%i==0){

printf("n %d IIIII %d",i,add);

add=add*i;

printf("nnnTotal IS SSS %d",add);

s=s/i;

//printf("n %d 2 n",s);

if(s!=1){

i++;

goto b;

}

}

else{

i++;

goto b;

}

printf("The LCM is %d",add);

*/

getch();

}


---------------------------------------------------------------------------------

Using Malloc



# include <stdio.h>


void main(){

int *a;

int b=10,i;

;;;;;;

;

;clrscr();

// a=(int*) malloc(sizeof (int) * b);


a[0]=10;

a[1]=20;

a[100]=90;

for(i=0;i<50;i++){

printf("%4d",i+10);

a[i]=i+10;

}

printf("n");

for(i=0;i<50;i++){

printf("%4d",a[i]);

};

printf("%d",sizeof(*a));

printf("n%d",sizeof(b));

printf("n%d",a);

printf("n value %d",a[0]);

printf("n value %d",a[1]);

printf("n value %d",a[100]);

getch();

}


---------------------------------------------------------------------------------

Matrix


# include <stdio.h>

void main()

{

int i,j,sum=0,gT=0,mul=1,gAvg=0;

int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};

int b[3][3];

clrscr();

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("%d t",a[i][j]);

sum=sum+a[i][j];

mul=mul+a[i][j];

}

printf("t ** SUM =%d",sum);

printf("t ** AVG =%d",(sum/j));

printf("t ** mul =%d",(mul));

gT=gT+sum;

gAvg=gAvg+(sum/j);

sum=0;

printf("n");

}

printf("tt Grand Total :%d",gT);

printf("t Grand Average :%d",(gAvg/3));

// Matrix Transpose

sum=0;

gT=0;

mul=0;

printf("n t t *** Matrix Transpose *** n");


for(i=0;i<3;i++){

for(j=0;j<3;j++){

// printf("%d t",a[j][i]);

b[i][j]=a[j][i];

sum=sum+a[j][i];

mul=mul+a[i][j];

}

/*

printf("t ** SUM =%d",sum);

printf("t ** AVG =%d",(sum/j));

printf("t ** MUL =%d",mul);

*/

gT=gT+sum;

sum=0;

printf("n");

}

printf("tt Grand Total :t%d",gT);

printf("n tt ** Stored in another MATRIX **");

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("%d t",b[i][j]);

}

printf("n");

}

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("%d * %d = %d n",a[i][j],b[i][j],(a[i][j]*a[i][j]));

}

printf("n");

}


getch();

}


---------------------------------------------------------------------------------

Guess what prog is this

# include <stdio.h>

void main(){

int i,j,a[3]={7,6,5},prv=0;

clrscr();

for(i=0;i<3;i++){

printf("%3d",a[i]);

}

for(i=0;i<2;i++){

for(j=0;j<3;j++){

if(a[j]>a[(j+1)]){

printf("nn");

printf("%d ",a[j]);

prv=a[(j+1)];

a[j]=a[(j+1)];

printf("%d ",a[j]);

a[(j+1)]=prv;

}

}

}

printf("nheREn");

for(i=0;i<3;i++){

printf("%3d",a[i]);

}

getch();

}


---------------------------------------------------------------------------------


Using Preprocessor Directives



# include <stdio.h>

// # include "CALLH.H"

# define a 100

# define b

# define vinS ;

# define trushal ;


void main(){

#if defined b

int v;

clrscr();

v=10;

printf("nv VALUE is YO! %dn",v)vinS

#else

printf("NOT WORKNG");

#endif

printf("nn v %d n",v);

printf("(A)%d n",((a)+1));

printf("(A)%d n",(1+(a*2)));

#if defined a

printf("YO ! %d",a);

#endif

#undef a

# define a

printf("n int!: %d , %%s! :%s, int! %d, U %u"a a a a)vinS

#undef a

# define a 100

printf("n int!: %d , %%s! :%s, int! %d, U %u",a,a,a,a)vinS

printf("n t hi truS n")trushal

# if defined a

/*

int v;

v=10;


printf("nYO! %d",v);

*/

#endif

getch();

}


---------------------------------------------------------------------------------

PRIME


# include <stdio.h>


void main() {

int no,inc,flag,i;

clrscr();

// printf("PLZ enter a Integer No , Elso dont blame me for error,n Ur No here :");

// scanf("%d",&no);

for(no=1;no<50;no++){

flag=1;

inc=2;

for(i=2;i<no;i++){

if(no%inc==0){

flag=0;

break;

}

else {

// printf("n %d",inc);

inc++;

flag=1;

}

}

if(flag==1){

printf("tPrime No %d:",no);

}else{

printf("tNT Prime No %d:",no);

}

}

getch();

}


---------------------------------------------------------------------------------

Returning for Main()





# include <stdio.h>

char main(){

printf("WORKING1");

return '0';

getch();

}


---------------------------------------------------------------------------------

Reversing




# include <stdio.h>


void main(){

int i,n,r,t,rev=0;

clrscr();

a:

scanf("%d",&i);

n=i;

while(i>0){

t=i%10;

rev=rev*10+t;

i=i/10;

}

printf("n Reverse is %d",rev);

if(rev==n){

printf("n No is Palindrome %d :",n);

}

else{

printf("n No is Not Palindrome %d :",n);

}

rev=0;

getch();

goto a;

}


---------------------------------------------------------------------------------


Reverse String Palindrome


/* Reverse of string & check it for palindrome */

#include <stdio.h>

#include <conio.h>

main()

{

char s[30],s1[30];

int i,j,c,c1=0;

clrscr();

printf("n Enter the string= ");

gets(s);

for(i=0;s[i]!='0';i++);

c=0;

for(j=(i-1);j>=0;j--)

{

s1[c]=s[j];

c=c+1;

}

s1[c+1]='0';

for(i=0;s1[i]!='0';i++)

{

if(s1[i]!=s[i])

c1=1;

}

printf("n Original string= %s",s);

printf("n Reverse string= %s",s1);

if(c==1)

printf("%s is palindrome",s);

else

printf("%s is not palindrome",s);

getch();

}



---------------------------------------------------------------------------------

ShellSort



void main()

{

int a[20],t,i,j,n;

clrscr();

scanf("%d",&n); //N o of values to scaN;

for(i=0;i<n;i++){

scanf("%d",&a[i]);

}

for(i=0;i<n;i++){

for(j=1+1;j<n-1;j++){

if(a[i]>a[j]){

t=a[i];

a[i]=a[j];

a[j]=t;

}

}

}

printf("After Sorting Element arE");

for(i=0;i<n;i++){

printf("%5d",a[i]);

}

getch();

}


---------------------------------------------------------------------------------


SimpleInterest


# include <stdio.h>


void main()

{

float p, m, r, si;

clrscr();

printf("n Enter the Pr. Amount:- ");

scanf("%f",&p);

printf("n Enter the no. of months:- ");

scanf("%f",&m);

printf("n Enter the rate of interest:- ");

scanf("%f",&r);

si = (p*m*r)/100.00;

printf("n Simple Interest:- %.2f",si);

getch();

}


---------------------------------------------------------------------------------

String Comparision



# include <stdio.h>


void strcomp(char *f,char *s){

int flag=0;

while(*f!='0'){

if(*f!=*s){

printf("Sorry THE STRINGS R NOT EQUAL n");

flag=1;

break;

}

else{

*f++;

*s++;

}

}

if(flag==0){

printf("BOTH R SAME n");

}

else{

// printf("NOT SAME n");

}

}


void strCopy(char *f,char *s){

while(*s!='0'){

*f++=*s++;

}

}


int main() {

//char a[10],b[10];

system("clear");

char a[10]="HIDUDE";

char b[10]="BI";

strcomp(a,b);

strCopy(a,b);


printf("THE COPY PROCESS IS : %s >> %s END n ",a,b);

//system("tput");


}


---------------------------------------------------------------------------------

String Cpy (strcpy)




#include <stdio.h>


void strcopy(char *,char*);

main(){

char str1[20],str2[20];

clrscr();

printf("Enter 2 Strings");

scanf("%s %s",str1,str2);

printf("Strin b4 cop");

printf("Str 1 : %s t Str2 2 : %sn",str1,str2);

strcopy(str1,str2);

printf("Str aftr copy");

printf("Str 1 : %s t Str2 2 : %sn",str1,str2);

getch();

}


void strcopy(char *s,char *d){

while (*d!='0'){

*s++=*d++;

}

*s='0';

}

---------------------------------------------------------------------------------


Structures


#include <stdio.h>


struct Emp{

int eNo,age;

char nam[20];

}e;


struct EmpCop{

int eNo,age;

char nam[20];

}eC;


typedef struct{

int eNo,age;

char nam[20];

}Empl;

struct anot{

char name[20];

};


struct asgn{

int za;

//za=10; //U cant initialize

};

typedef struct{

int a;

union uni{

int a;

char b[5];

}un;

}stUni;


typedef union{

int a;

struct str{

int a;

char b[5];

}st;

}UniSt;

typedef struct N{

int nest;

}Na;



void main(){

struct Emp e,ex;

struct EmpCop ec;

struct N Nit,vr,*pVr;

Na aaa;

Empl e1;

Empl e2;

stUni su1;

UniSt un1;

clrscr();


vr.nest=10;


printf("THE SIZE OF STR %d N UNI IS %d n",sizeof(stUni),sizeof(su1.un));

printf("THE SIZE OF UNI %d N UNI IS %d n",sizeof(stUni),sizeof(su1.un));

e.eNo=20;

e.age=30;

strcpy(e.nam,"a");

printf("n %d %2d",e.eNo,e.age);

e1.eNo=50;

printf("n E1 %d %2d",e1.eNo,e1.age);

ex=e;

// e1=(Empl)ex;

//ec=ex;

printf("n STRUCTU COPY : %d %2d",ex.eNo,ex.age);

printf("n STRUCTU : %d %2d",e1.eNo,e1.age);

getch();

}