programs

17
Kuldeep bhartiya PROGRAMS / LOGIC FACTORIAL (JavaScript) <SCRIPT> function fact (n) { var i,f=1; document.write("<H3>FACTORIAL JAVASCRIPT PROGRAM OUTPUT</H3><HR><BR>"); document.write("Facorial of " + n + " is "); for(i=1;i<=n;i++) { f=f*i; } document.write(" " +f); } </SCRIPT> MULTIPLICATION (JavaScript) <SCRIPT> function mul (n) { var i,ans=0; document.write("<H3>MULTIPLICATION JAVASCRIPT PROGRAM OUTPUT</H3><HR><BR>"); for(i=1;i<=10;i++) { ans=n*i; document.write(n+" * "+i+" = "+ans+"<BR>"); } } </SCRIPT> ODD NOS (JavaScript) <SCRIPT TYPE="Text/Javascript"> document.write("<H3>JAVASCRIPT PROGRAM PRINTING ODD NOS ( 1 - 50 )</H3><HR>") function odd () { var i; for(i=1;i<=50;i++) { if(i%2!=0) document.write(i+"<BR>"); } } odd(); <!-- Calling function in script itself --> </SCRIPT> 1

Upload: vijay-barai

Post on 26-Nov-2014

47 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programs

Kuldeep bhartiya

PROGRAMS / LOGICFACTORIAL (JavaScript)<SCRIPT>function fact (n){

var i,f=1;document.write("<H3>FACTORIAL JAVASCRIPT PROGRAM OUTPUT</H3><HR><BR>");document.write("Facorial of " + n + " is ");for(i=1;i<=n;i++){

f=f*i;}document.write(" " +f);

}</SCRIPT>

MULTIPLICATION (JavaScript)<SCRIPT>function mul (n){

var i,ans=0;document.write("<H3>MULTIPLICATION JAVASCRIPT PROGRAM OUTPUT</H3><HR><BR>");for(i=1;i<=10;i++){

ans=n*i;document.write(n+" * "+i+" = "+ans+"<BR>");

}

}</SCRIPT>

ODD NOS (JavaScript)<SCRIPT TYPE="Text/Javascript">document.write("<H3>JAVASCRIPT PROGRAM PRINTING ODD NOS ( 1 - 50 )</H3><HR>")function odd (){

var i;for(i=1;i<=50;i++){

if(i%2!=0)document.write(i+"<BR>");

}}odd(); <!-- Calling function in script itself --></SCRIPT>

VOWEL CHECKING (JavaScript)<SCRIPT>function vowel (c){

var c;if((c=="a")||(c=="e")||(c=="i")||(c=="o")||(c=="u")){

alert(c+" is a vowel");}

1

Page 2: Programs

Kuldeep bhartiyaelse{

alert(c+" is not a vowel");}

}</SCRIPT>

NUMBER SERIES (JavaScript)<SCRIPT>document.write("A NUMBER SERIES JAVASCRIPT PROGRAM <BR><HR><BR>");var i,no=0;for(i=0;i<=10;i++){

no=3*i+no;document.write(no+" ");

}</SCRIPT>Output :0,3,9,18,30,45…….

NUMBER SERIES (JavaScript)<SCRIPT>document.write("A JAVASCRIPT PROGRAM TO PRINT DESIRED OUTPUT<BR><HR><BR>");var i,j=1,c=0;for(i=1;i<=6;i++){

c=1;document.write("<BR>");while(c<=i && j<=10){document.write(j+" ");c++;j++;}

}</SCRIPT>Output : Floyd’s triangle 1

2 3

4 5 6

7 8 9 10

PRIME NUMBER SERIES (JavaScript)<SCRIPT>document.write("A PRIME NUMBER SERIES JAVASCRIPT PROGRAM <BR><HR><BR>");var i,j,n,f=0;function prime(n){

for(i=2;i<=n;i++) { for(j=2;j<i;j++) { if(i%j==0) { f=1; break;

2

Page 3: Programs

Kuldeep bhartiya } f=0; }

if(f==0) document.write(" "+i);

}}</SCRIPT>

PALINDROME (JavaScript)<SCRIPT>document.write("A JAVASCRIPT PROGRAM TO CHECK WHETHER NO IS PALINDROME<BR><HR><BR>");function palindrome(str){

var rev = "";var len = str.length;

for (var i = len ; i > 0 ; i--){

rev += str.charAt(i-1) } if(str == rev)

{alert("palindrome")

}else{

alert("not a palindrome")}

}

FIBONACCI SERIES (JavaScript)<SCRIPT>function Fibonacci(n){

var i,a=1,b=1,c=0;document.write("A JAVASCRIPT PROGRAM TO PRINT FIBONACCI SERIES<HR><BR>");document.write(c+"<BR>");for(i=0;i<=n;i++){

a=b;b=c;c=a+b;document.write(c+"<BR>");

}}</SCRIPT>

CONVERT FARENHEIT / CELSIUS(JavaScript)<SCRIPT>function convert(f){

var celsius=(5/9)*(parseInt(f)-32);return celsius;

}</SCRIPT>

DATE CHECKING(JavaScript)<SCRIPT>

3

Page 4: Programs

Kuldeep bhartiyavar d1=new Date();var d2=new Date();d1.setDate(d1.getDate());d2.setFullYear(2010,1,1);if(d1>d2){

document.write(d2 + "<BR>Date is after<BR>" + d1);}else{

document.write(d1 + "<BR>Date is before <BR>" + d2);}</SCRIPT>

PYRAMID(JavaScript)<SCRIPT>function pyramid(val){

var i,j;for(i=val;i>=1;i--){

for(j=val;j>=i;j--){

document.write(j);}

document.write("<br>");}

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

for(j=val;j>=i;j--){

document.write(j);}document.write("<br>");

}}

</SCRIPT>

Output : VAL=55

5 4

5 4 3

5 4 3 2

5 4 3 2 1

5 4 3 2

5 4 3

5 4

5

DIGITAL CLOCK (C)

4

Page 5: Programs

Kuldeep bhartiya/* A simple program to simulate a digital clock */#include<stdio.h>#include<conio.h>struct time{

int h,m,s;};void main(){

struct time t;t.h=11;t.s=0;t.m=0;while(!kbhit()){t.s++;clrscr();if(t.s>=60){

t.s=0;t.m++;

}if(t.m>=60){

t.s=0;t.m=0;t.h++;

}if(t.h>=24){

t.s=0;t.m=0;t.h=0;

}printf("DIGITAL CLOCK -\t");printf("HH : %d MM : %d SS : %d",t.h,t.m,t.s);delay(1000);}getch();

}

FACTORS OF A NO (C)/* to find factors of a given no*/#include<stdio.h>#include<conio.h>void main(){

int n,i;clrscr();printf("Enter a no : ");scanf("%d",&n);printf("The factors are");for(i=1;i<=n;i++){

if(n%i==0){

printf("\n%d",i);}

5

Page 6: Programs

Kuldeep bhartiya}getch();

}

GREATEST AMONG THREE (C)/* to find greatest among three */#include<stdio.h>#include<conio.h>void main(){

int a,b,c,temp;clrscr();printf("Enter 3 different nos :\t");scanf("%d%d%d",&a,&b,&c);if(a>b){

temp=a;}else{

temp=b;}if(temp>c)printf("\n%d is greater",temp);elseprintf("\n%d is greater",c);getch();

}

SORTING AN ARRAY 1-D(C)/* sorting an array */#include<stdio.h>#include<conio.h>void main(){

int a[10],i=0,j=0,temp;clrscr();printf("Sorting Single Dimensional Array\n\n\n");printf("Enter 10 elements in the array\n");for(i=0;i<10;i++){

scanf("%d",&a[i]);}// sortingfor(i=0;i<10;i++){

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

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

temp=a[i];a[i]=a[j];a[j]=temp;

}}

6

Page 7: Programs

Kuldeep bhartiya}// display sorted elementsprintf("\n\nSorted Array Elements Ascending Order\n");for(i=0;i<10;i++){

printf("\n%d",a[i]);}getch();

}

SEARCHING IN AN ARRAY(C)/* to search an element in an array */#include<stdio.h>#include<conio.h>void main(){

int a[10],i=0,x;clrscr();printf("Search Element in Array\n\n");printf("Enter 10 elements in the array1\n");for(i=0;i<10;i++){

scanf("%d",&a[i]);}printf("\nEnter an element to be searched : ");scanf("%d",&x);// searchingfor(i=0;i<10;i++){

if(x==a[i]){

printf("\nElement %d found at %d location",x,i+1);}

}getch();

}

MERGING ARRAYS(C)/* Merging two Arrays */#include<stdio.h>#include<conio.h>void main(){

int a[5],b[5],c[10],i=0,count=0;clrscr();printf("Merging two Arrays\n\n");printf("Enter 5 elements in the array1\n");for(i=0;i<5;i++){

scanf("%d",&a[i]);c[i]=a[i];count++;

}printf("Enter 5 elements in the array2\n");for(i=0;i<5;i++){

scanf("%d",&b[i]);c[count]=b[i];

7

Page 8: Programs

Kuldeep bhartiyacount++;}printf("\nDisplay Merged Arrays\n");for(i=0;i<count;i++){

printf("\nc[%d] = %d",i,c[i]);}getch();

}

ADDTION OF ARRAYS(C)/* Addition of two Arrays */#include<stdio.h>#include<conio.h>void main(){

int a[2][2],b[2][2],c[2][2],i=0,j=0;clrscr();printf("Addition of 2D - 2 x 2 Arrays\n\n");printf("Enter elements in the array1\n");for(i=0;i<2;i++){

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

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

}}printf("\nEnter elements in the array2\n");for(i=0;i<2;i++){

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

printf("b[%d][%d] : ",i,j);scanf("%d",&b[i][j]);

}}printf("\nDisplay Added Arrays\n");for(i=0;i<2;i++){

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

c[i][j]=a[i][j]+b[i][j];printf("\nc[%d][%d] = %d",i,j,c[i][j]);

}}getch();

}

ASCII VALUES(C)/* Ascii code of ur name */#include<stdio.h>#include<string.h>#include<conio.h>void main(){

char str[10];int i=0,ascii;

8

Page 9: Programs

Kuldeep bhartiyaclrscr();printf("Ascii code of your name\n\n");printf("Enter ur name\n");scanf("%s",str);while(str[i]!='\0'){ ascii=(int)str[i]; printf("%c = %d\n",str[i],ascii); i++;}getch();

}

STRING OPERATIONS (c)/* String Operations Without Using Functions */#include<stdio.h>#include<string.h>#include<conio.h>void main(){

char str1[30],str2[30],str3[20],var;int i=0,l1=0,l2=0,flag;clrscr();printf("/*Performing Different String Operations*/\n");printf("a.Finds length of string\n");printf("b.Copies one string into another\n");printf("c.Compare two strings\n");printf("d.Concatenate two strings\n\n");printf("Enter string1\n");scanf("%s",str1);printf("Enter string2\n");scanf("%s",str2);printf("Enter case character\n\n");scanf("%s",&var);switch(var){

case 'a':printf("a.Finds length of string1\n"); while(str1[i]!='\0') {

i++; } printf("Length of string = %d\n",i); break;

case 'b':printf("b.Copies one string into another\n"); while(str1[i]!='\0') {

str2[i]=str1[i];i++;

} str2[i]='\0'; printf("Copied string str2 = %s\n",str2); break;

case 'c':printf("c.Compare two strings\n"); i=0; while(str1[i]!='\0') {

if(str1[i]==str2[i])flag=0;

9

Page 10: Programs

Kuldeep bhartiyaelseflag=1;i++;

} if(flag==0) printf("strings are equal\n"); else printf("strings are not equal\n"); break;

case 'd':printf("d.Concatenate two strings\n"); l1=strlen(str1); l2=strlen(str2); printf("%d%d\n",l1,l2); for(i=0;i<=l2;i++) {

str1[l1+i]=str2[i]; } printf("Joined string = %s\n",str1); break;

default: printf("Invalid Case");}getch();

}

SWAP VALUES (C)/* a program to swap variables */#include<stdio.h>#include<conio.h>void swap(int a,int b){

a=a+b;b=a-b;a=a-b;printf("\n\nInterchanged values : a=%d b=%d",a,b);

}void main(){

int a,b;clrscr();printf("Enter 2 nos :\n");scanf("%d%d",&a,&b);printf("a=%d b=%d",a,b);swap(a,b);getch();

}

FIBONACCI SERIES - RECURSION(C)#include<stdio.h>#include<conio.h>unsigned int fibonacci(unsigned int n) // recursive function{ if(n==0 || n==1) { return 1; } else {

10

Page 11: Programs

Kuldeep bhartiya return fibonacci(n-1) + fibonacci(n-2);}

}void main(){ unsigned int i,j=0; clrscr(); printf("\nEnter the fibonnaci number : "); scanf("%d",&i); printf("%d",j); for(j=0;j<=i;j++) printf(" ""%d",fibonacci(j)); // recursive call getch();}

SORTING ON STRING VALUES(C)/* a program to sort as per name,age,salary*/#include<stdio.h>#include<conio.h>

struct person{

char name[20];int age,salary;

};void getdata(struct person []);void putdata(struct person []);void sortbyage(struct person []);void sortbyname(struct person []);void sortbysalary(struct person []);

void main(){

struct person p[3],temp;int i,j,c;clrscr();getdata(p);// switch case to choose optionsprintf("\n *** Select option to sort ***");printf("\n\n1.Sort by age\n2.Sort by name\n3.Sort by salary\n");printf("\nEnter ur option : ");scanf("%d",&c);switch(c){ case 1:printf("\n\nSort by age\n");

sortbyage(p);putdata(p);break;

case 2:printf("\n\nSort by name\n");sortbyname(p);putdata(p);break;

case 3:printf("\n\nSort by salary\n");sortbysalary(p);putdata(p);break;

default:printf("Invalid case\n");}

11

Page 12: Programs

Kuldeep bhartiyagetch();}

void getdata(struct person p[3]){

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

printf("\nEnter name , age & salary of person %d :",i+1);scanf("\n%s%d%d",p[i].name,&p[i].age,&p[i].salary);

}} void putdata(struct person p[3]) {

int i;printf("Name\tAge\tSalary\n");for(i=0;i<3;i++){

printf("\n%s\t%d\t%d",p[i].name,p[i].age,p[i].salary);}

}

void sortbyage(struct person p[3]){

int i,j;struct person temp;for(i=0;i<3;i++){

for(j=i+1;j<3;j++){

if(p[i].age>p[j].age){

temp=p[i];p[i]=p[j];p[j]=temp;

}}

}}void sortbyname(struct person p[3]){

struct person temp;int i,j;for(i=0;i<3;i++){ for(j=i+1;j<3;j++) if(strcmp(p[i].name,p[j].name)>0) {

temp=p[i];p[i]=p[j];p[j]=temp;

}}

}void sortbysalary(struct person p[3]){

int i,j;struct person temp;

12

Page 13: Programs

Kuldeep bhartiyafor(i=0;i<3;i++){

for(j=i+1;j<3;j++){

if(p[i].salary>p[j].salary){

temp=p[i];p[i]=p[j];p[j]=temp;

}}

}}

ADDITION OF FEETS / INCHES(C)/* a program to add feets,inches using structures */#include<stdio.h>#include<conio.h>struct convert{

int feet,inches;};typedef struct convert covt;covt add(covt,covt);

void main(){

covt c1,c2,c3;clrscr();printf("*** Measurement Addition ***\n");printf("\nEnter feet1 and inches1 : ");scanf("%d%d",&c1.feet,&c1.inches);printf("\nEnter feet2 and inches2 : ");scanf("%d%d",&c2.feet,&c2.inches);c3=add(c1,c2);printf("\nAdded values are : Feets = %d'\tInches = %d''",c3);getch();

}

covt add(covt p1,covt p2){

covt p3;int val=30;if(p1.inches+p2.inches>=30){

p3.feet=p1.feet+p2.feet+1;val=(p1.inches+p2.inches)-val;while(val>=30){

p3.feet++;val=val-30;

}p3.inches=val;

}else{

p3.feet=p1.feet+p2.feet;p3.inches=p1.inches+p2.inches;

13

Page 14: Programs

Kuldeep bhartiya}return p3;

}

EVALUATE SERIES(C)/* Evaluate series ex=1+x1+x2/2!+3/3!+....+xn/n!*/#include <stdio.h>#include <conio.h>long int factorial(int n);void main(){

int n,i,x=2;float s;clrscr();printf("Enter the value of n : ");scanf("%d",&n);s=1;for (i=0;i<=n-1;i++){

s=s+((float)i*(x)/(float)factorial(i));}printf("The sum of %d terms is %f",n,s);getch();

}long int factorial(int n){

if (n<=1){

return(1);}else{

n=n*factorial(n-1);}return(n);

}

14