...

[Q001] Write a program (W.A.P.) in C to SWAP the... the temporary (or extra) variables.

by user

on
Category: Documents
11

views

Report

Comments

Transcript

[Q001] Write a program (W.A.P.) in C to SWAP the... the temporary (or extra) variables.
[Q001] Write a program (W.A.P.) in C to SWAP the contents of 3 variables without using
the temporary (or extra) variables.
/* Swapping 3 numbers without using extra variable */
#include< stdio.h >
#include< conio.h >
void Swap(int *a,int *b,int *c)
{
*a = *a + *b + *c;
*b = *a - (*b + *c);
*c = *a - (*b + *c);
*a = *a - (*b + *c);
}
int main()
{
int x=1,y=2,z=3;
clrscr();
printf("BEFORE SWAPPING : %d %d %d\n",x,y,z);
Swap(&x,&y,&z);
printf("AFTER SWAPPING : %d %d %d",x,y,z);
return(0);
} /* End of Main */
[Q002] W.A.P. in C to find the Fifth root of the sum of the squares of the first 100 ODD
numbers only.
/* To find the Fifth root of the sum of the squares of the first 100 ODD numbers
ONLY */
#include< stdio.h >
#include< conio.h >
#include< math.h >
int main(void)
{
long i,oddnum=1,sqrnum,sum=0;
for (i=1; i<=100; i++)
{
sqrnum=oddnum * oddnum; // Square the ODD number
sum+=sqrnum; // Add Square value to the sum
oddnum+=2; // Get the next ODD number
}
printf("\nThe result is : %ld,%.2f",sum,pow((double)sum,(1.0/5.0)));
return(0);
} /* End of Main */
[Q003] W.A.P. in C to multiply any two numbers without using * (asterisk)
[Hint : Use BITWISE OPERATORS]
/* Multiplication of two numbers using BITWISE OPERATORS ONLY */
#include< stdio.h >
int main()
{
long int i,n,mul,mul2,count,temp,a,b,sum,carry,res,tot;
printf("\nEnter any 2 numbers : ");
scanf("%ld %ld",&mul,&n);
mul2=temp=mul;
for (i=2; i<=n; i++)
{
temp=mul;
count=32;
res=1;
tot=sum=carry=0;
while (count--)
{
a=temp & 0x1;
b=mul2 & 0x1;
if ((a^b==1) && (carry==1))
{
sum=(a^b)^carry;
carry=(a^b)&carry;
}
else
{
sum=a^b|carry;
carry=a&b;
}
temp=temp>>1;
mul2=mul2>>1;
tot+=res*sum;
res=res*2;
}
mul2=tot;
}
printf("\n%3ld * %3ld = %3ld",mul,i-1,tot);
getch();
return(0);
} /* End of Main */
[Q004] W.A.P. in C to check whether given number x is equal to the value 2 POWER i or
something, where i>=0 using BITWISE operators ONLY. [Hint : Check whether the given
number x is equal to the value 2 POWER i or something using BITWISE operators
ONLY]
/* Check whether the given number x is equal to the value 2 power i or not using
BITWISE operators ONLY */
#include< stdio.h >
#include< conio.h >
int main(void)
{
long x;
printf("Enter a number : ");
scanf("%ld",&x);
if ((x & 0x1) == 0x0) // Checking the Least significant bit
printf("The given number %ld is EQUAL to the value 2 POWER
something",x);
else
printf("The given number %ld is NOT EQUAL to the value 2 POWER
something",x);
getch();
return(0);
} /* End of Main */
[Q005] W.A.P. in C to maintain 2 STACKS within a SINGLE ARRAY and the values of one
stack should not overwrite the values of another stack.
/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include< stdio.h >
#include< conio.h >
#define MAX 10
int stack[MAX],top1,top2;
void init()
{
top1=-1;
top2=10;
}
void Push1(int item)
{
stack[++top1]=item;
}
void Push2(int item)
{
stack[--top2]=item;
}
int Pop1()
{
return stack[top1--];
}
int Pop2()
{
return stack[top2++];
}
void Display()
{
int i;
if(top1==-1)
printf("\nStack1 : Empty");
else
{
printf("\nContent of Stack1 :\n");
for(i=0;i<=top1;i++)
printf("%d\t",stack[i]);
}
if(top2==10)
printf("\nStack2 : Empty");
else
{
printf("\nStack2 contains:\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",stack[i]);
}
}
int main()
{
int item,ch;
init();
while(1)
{
printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.Exit
");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if ((top1 + 1) < top2)
{
printf("\nEnter item to Push into Stack1:");
scanf("%d",&item);
Push1(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 2 : if ((top2 - 1) > top1)
{
printf("\nEnter item to Push into Stack2:");
scanf("%d",&item);
Push2(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 3 : if(top1 <= -1)
printf("\nError : Underflow on pop1");
else
printf("\nPopped item from stack1 is :
%d",Pop1());
break;
case 4 : if(top2 >= 10)
printf("\nError : Underflow on pop2");
else
printf("\nPopped item from stack2 is :
%d",Pop2());
break;
case 5 : Display();
break;
case 6 : exit(0);
}
default: printf("\nInvalid Choice");
}
return(0);
} /* End of Main */
[Q006] W.A.P. in C that act as a guessing game in which the user has eight tries to
guess a
randomly generated number. The program will tell the user each time whether he
guessed high or
low. The user WINS the game when the number guessed is same as randomly
generated number.
/* Guessing Game Solution */
#include< stdlib.h >
#include< stdio.h >
#include< time.h >
int main(void)
{
int i=8,rval,val,flag=1;
randomize(); // Initialize the random number generator
rval=random (100); // Generates a random number in the range 0 to
99
printf("Welcome to Guessing Game.\n");
printf("RULES:\n1. Only 8 chances to guess the randomly generated
number.");
printf("\n2. You can WIN the game when the number guessed is same
as the");
printf("randomly generated number.\n3. Follow the hints.");
printf("\n\n$$$ Good Luck. Start Guessing $$$");
for (i=1; i<=8; i++)
{
printf("\n\nGUESS %d ? ",i);
scanf("%d",&val);
if (val > rval)
printf("Your value is GREATER THAN the randomly generated
number");
else if (val < rval)
printf("Your value is LESSER THAN the randomly generated
number");
else
{
flag=1;
break;
}
}
if (flag)
printf("\n\n*** You are the WINNER. No. of tries = %d
***",i);
else
printf("\n\n*** You are the LOSER. ***");
return(0);
} /* End of Main */
[Q007] W.A.P. to determine how much money is in a piggy bank that contains several
50 paise coins, 25 paise coins, 20 paise coins, 10 paise coins and 5 paise coins. Use
the following values to test your program : Five 50 paise coins, Three 25 paise coins,
Two 20 paise coins, One 10 paise coin and Fifteen 5 paise coins. (Answer : Rs. 4.50)
/* To determine how much money in a piggy bank */
#include< stdio.h >
#include< string.h >
#include< conio.h >
int main(void)
{
float
coin1=0.50,coin2=0.25,coin3=0.20,coin4=0.10,coin5=0.05,total=0.0;
int ncoins;
clrscr();
printf("How many 50 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin1);
printf("** %.2f **",total);
printf("\nHow many 25 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin2);
printf("** %.2f **",total);
printf("\nHow many 20 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin3);
printf("** %.2f **",total);
printf("\nHow many 10 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin4);
printf("** %.2f **",total);
printf("\nHow many 5 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin5);
printf("\n\nThe total amount is Rs.%.2f",total);
getch();
return(0);
} /* End of Main */
[Q008] Modify the program given in [Q007] to accept total amount (in rupees) and
convert them into paise.(Vice-versa of [Q007])
/* Denominations */
#include< stdio.h >
#include< string.h >
#include< conio.h >
int main(void)
{
int nc1,nc2,nc3,nc4,nc5,temp;
float total;
clrscr();
printf("Enter the amount : ");
scanf("%f",&total);
temp = total * 100;
nc1 = temp / 50;
temp = temp % 50;
nc2 = temp / 25;
temp = temp % 25;
nc3 = temp / 20;
temp = temp % 20;
nc4 = temp / 10;
temp = temp % 10;
nc5=temp;
printf("\n\nNo. of 50 paise coins = %d",nc1);
printf("\nNo. of 25 paise coins = %d",nc2);
printf("\nNo. of 20 paise coins = %d",nc3);
printf("\nNo. of 10 paise coins = %d",nc4);
printf("\nNo. of 5 paise coins = %d",nc5);
getch();
return(0);
} /* End of Main */
[Q09] W.A.P. in C to determine how many of the characters are vowels and how many
are consonants in a given line of text. Also terminate the string when the input
character encountered is other than the alphabets(a-z or A-Z) and Blank spaces.
[Hint:(a) When the input string is 'C FOR SWIMMERS, TEST YOUR C PROGRAMMING
STRENGTHS'. Consider the string 'C FOR SWIMMERS' only Because ',' is encountered.
(b) When the input string is 'Y2K PROBLEM'. Consider the character 'Y' only Because
the '2' is encountered.]
/* Counting vowels and consonants in a given line of text */
#include< stdio.h >
#include< string.h >
#include< conio.h >
int main(void)
{
char *str;
int i,vow=0,cons=0;
clrscr();
printf("Enter a string : ");
scanf("%[
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",str);
for (i = 0; i < strlen(str); i++)
if (str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U' ||
str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' ||
str[i]=='u')
vow++;
else if (str[i] != ' ') // Ignore BLANK characters
cons++;
printf("\n\nThe given string is %s",str);
printf("\n\nThe total number of VOWELS in a given string is
%d",vow);
printf("\nThe total number of CONSONANTS in a given string is
%d",cons);
return(0);
} /* End of Main */
[Q010] W.A.P. in C to perform 4-letter WORD UNSCRAMBLING i.e. List all possible
combinations of 4-letters in a word. Ex: The word 'TEST' can be unscrambled as
TEST,TETS,TSET,TSTE,TTSE,TTES,etc.
/* 4-letter word unscrambling */
#include< stdio.h >
#include< conio.h >
#include< string.h >
int main()
{
int i,j,k,l,sum=6;
char *str;
clrscr();
printf("Enter a 4-letter word or string : ");
scanf("%s",str);
if (strlen(str) == 4)
{
printf("The possible combinations of the given 4-letter word is
shown.");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (i != j)
{
for (k = 0; k < 4; k++)
if ((k != i) && (k != j))
{
l = sum - (i + j + k);
printf("\n%c%c%c%c",str[i],str[j],str[k],str[l]);
}
}
printf("\nTotal combinations = %d",4*3*2*1);
}
else
printf("\nInvalid string. Length of the string must be 4letters only ");
getch();
return(0);
} /* End of Main */
To check whether the entered string is palindrome
or not
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{clrscr();
char str1[20],str2[20];
int m,x,i,j;
printf("\n Enter any string - ");
scanf("%s",&str1);
m=strlen(str1);
for(i=m-1,j=0;i>=0;i--,j++)
{
str2[j]=str1[i];
}
str2[j]='\0';
x=strcmp(str1,str2);
if(x!=0)
printf("\n The entered string is not palindrome");
if(x==0)
printf("\n The entered string is palindrome");
getch();
}
To concatenate two strings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{clrscr();
char a[20],b[20];
printf("Enter the first string = ");
scanf("%s",&a);
printf("Enter the second string = ");
scanf("%s",&b);
strcat(a,b);
printf("The concatenated string is = ");
printf("%s",a);
getch();
}
To find the reverse of a string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{clrscr();
char a[20];
printf("Enter the first string = ");
scanf("%s",&a);
strrev(a);
printf("The reverse string is = ");
printf("%s",a);
getch();
}
To multiply two given Matrices of orders (2*3) &
(3*3) respectively
#include<stdio.h>
#include<conio.h>
void main ()
{clrscr();
int m1[2][3],m2[3][3],r[2][3];
int i,j;
puts("Values for 1st Matrix of 2*3");
for(i=0;i<2;i++)
{
printf("\n%d row",i+1);
for(j=0;j<3;j++)
{
printf("\nEnter the value of column : ");
scanf("%d",&m1[i][j]);
}
}
puts("\n\n\nEnter values for 2nd Matrix of 3*3 ");
for(i=0;i<3;i++)
{
printf("\n%d row",i+1);
for(j=0;j<3;j++)
{
printf("\nEnter the value of column : ");
scanf("%d",&m2[i][j]);
}
}
(Matrix multiplication program continued………….)
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
r[i][j]=0;
r[i][j]=m1[i][j]+m2[i][j];
}
}
puts("\nResultant Matrix : ");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d",r[i][j]);
}
}
getch();
}
To find the largest & second largest numbers out
of 10 entered numbers
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{clrscr();
int a[10],i,j,x;
for(i=0;i<10;i++)
{
printf("\nEnter the no., %d = ",i+1 );
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<=10;j++)
{
if(a[i]<a[j])
{
x=a[j];
a[j]=a[i];
a[i]=x;
}
}
}
printf("\nThe largest no. :%d",a[0]);
printf("\nThe 2nd largest no.is :%d",a[1]);
getch();
}
To find the reverse of a number
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
int num,i;
int num1=0;
printf("Enter a number = ");
scanf("%d",&num);
while(num!=0)
{
i=num%10;
num=num/10;
num1=num1*10+i;
}
printf("Reversed No. is = %d",num1);
getch();
}
To find the average height of 5 Males & 5
Females
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
float mavg,mtot,favg,ftot,mh[5],fh[5];
int i;
puts("Enter the Male heights");
for(i=0;i<5;i++)
{
printf("\nHeight, %d = ",i+1);
scanf("%f",&mh[i]);
mtot=mh[i]+mtot;
}
mavg=mtot/5;
printf("\nThe average Male height is = %f",mavg);
puts("\n\n\n\nEnter the Female heights");
for(i=0;i<5;i++)
{
printf("\nHeight, %d = ",i+1);
scanf("%f",&fh[i]);
ftot=fh[i]+ftot;
}
favg=ftot/5;
printf("\nThe average Female height is = %f",favg);
getch();
}
To find greatest number out of three numbers
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
int a,b,c;
printf("Enter any integer,a");
scanf("%d",&a);
printf("Enter any integer,b");
scanf("%d",&b);
printf("Enter any integer,c");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{printf("a is greatest");
printf("%d",a);}
else
{
printf("c is greatest");
printf("%d",c);
}
}
(Greatest out of three numbers continued…………)
else
{
if(b>c)
{printf("b is greatest");
printf("%d",b);}
else
{
printf("c is greatest");
printf("%d",c);
}
}
getch();
}
To find the roots of a Quadratic Equations
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
float a,b,c,p,q,r,x,conjug_X;
printf("Enter coefficient, a = ");
scanf("%f",&a);
printf("Enter coefficient, b = ");
scanf("%f",&b);
printf("Enter coefficient, c = ");
scanf("%f",&c);
r=2*a;
p=-b/r;
q=(sqrt((b*b)-(4*a*c)))/r;
x=p+q;
conjug_X=p-q;
printf("x = ");
printf("%f",x);
printf("\nConjugate x = ");
printf("%f",conjug_X);
getch();
}
COPYING THE CONTENTS OF ONE ARRAY TO ANOTHER
(STRING COPY)
Aim:
To Copying the contents of one array to another (String Copy).
Program:
/*copying the contents of one array to another*/
#include<stdio.h>
main()
{
char a[100],b[100];
int i,j,n;
clrscr();
printf("enter the string ");
for(i=0;(a[i]=getchar())!='$';i++)
;
a[i]='\0';
printf("the entered string is %s\n",a);
for(i=0,j=0;a[i];j++,i++)
b[j]=a[i];
b[j]='\0';
printf("the copied str ing is %s",b);
getch();
}
Sample Run:
enter the string bioinformatics$
the entered string is bioinformatics
the copied str ing is bioinformatics
COPY THE UPPERCASE CHARACTER OF ONE ARRAY AS
LOWERCASE CHARACTER TO ANOTHER ARRAY
Aim:
To copy the uppercase character of one array as lowercase character to
another array
Program:
/*copy the upper case charecter*/
#include<stdio.h>
main()
{
int i,j;
char a[100],b[100];
clrscr();
printf("enter the string\n");
for(i=0;(a[i]=getchar())!='$';i++)
;
a[i]='\0';
printf("the entered string is %s\n",a);
for(i=0;a[i];i++)
if(a[i]>=65&&a[i]<=90)
a[i]=a[i]+32;
for(i=0,j=0;a[i];j++,i++)
b[j]=a[i];
b[j]='\0';
printf("the letters in small letler is %s",b);
getch();
}
Sample Run:
enter the string
BIOINformatics$
the entered string is BIOINformatics
the letters in small letler is bioinformatics
CHECKING WHETHER A STRING IS A PALINDROME OR NOT
Aim:
To check whether a string is a palindrome or not.
Program:
/*Checking whether a string is a palindrome or not*/
#include<stdio.h>
main()
{
char a[100];
int i,j,k,flag=1;
clrscr();
printf("Enter the string:");
for(i=0;(a[i]=getchar())!='$';i++);
a[i]='\0';
if(i%2==0)
printf("The string entered is not a valid one;");
for(i=i/2,j=i-1,k=i+1;a[k]&&j>=0;k++,j--,i++)
if(a[k]!=a[j])
{
flag=0;
printf("The string %s is not a palindrome",a);
break;
}
if(flag)
printf("The string %s is a palindrome",a);
getch();
}
Sample run:
Enter the string:malayalam$
The string malayalam is a palindrome
COPY THE LEFT ‘n’ CHARACTERS OF ONE ARRAY TO
ANOTHER
Aim:
To copy the left ‘n’ characters of one array to another.
Program:
/*copy left end of the arrays*/
#include<stdio.h>
main()
{
int i,j,m,n;
char a[100],b[100];
clrscr();
printf("enter the string ");
for(i=0;(a[i]=getchar())!='$';i++)
;
a[i]='\0';
printf("the entered string is %s\n",a);
printf("enter the range of charecter");
scanf("%d",&n);
for(i=0,j=0;i<n;j++,i++)
b[j]=a[i];
b[j]='\0';
printf("the extracted string is %s",b);
getch();
}
Sample Run:
enter the string selvaraj$
the entered string is selvaraj
enter the range of charecter5
the extracted string is selva
COPY THE LAST ‘N’ CHARACTERS OF ONE ARRAY TO ANOTHER
Aim:
To Copy the last ‘n’ characters of one array to another.
Program:
/*Copying the last'n' characters of one array to another*/
#include<stdio.h>
main()
{
char a[100],b[100];
int i,j,n;
clrscr();
printf("Enter the string:");
for(i=0;(a[i]=getchar())!='$';i++);
a[i]='\0';
printf("Enter the number of characters to copy from the right:");
scanf("%d",&n);
for(i=i-n,j=0;a[i];j++,i++)
b[j]=a[i];
b[j]='\0';
printf("The copied string from right to another array is:%s",b);
getch();
}
Sample run:
Enter the string:selvaraj$
Enter the number of characters to copy from the right:3
The copied string from right to another array is:raj
COPY THE MIDDLE ‘n’ CHARACTERS OF ONE ARRAY TO
ANOTHER
Aim:
To Copy the middle ‘n’ characters of one array to another.
Program:
/*Copying the middle 'n' characters of one array to another*/
#include<stdio.h>
main()
{
char a[100],b[100];
int i,j,m,n;
clrscr();
printf("Enter the string:");
for(i=0;(a[i]=getchar())!='$';i++);
a[i]='\0';
printf("Enter the range of characters to be copied:");
scanf("%d%d",&m,&n);
for(i=m-1,j=0;i<n;j++,i++)
b[j]=a[i];
b[j]='\0';
printf("The copied characters in another array are:%s",b);
getch();
}
Sample run:
Enter the string:bioinformatics$
Enter the range of characters to be copied:4 9
The copied characters in another array are:inform
CONCATENATE TWO CHARACTER ARRAYS (STRING
CONCATENATION)
Aim:
To Concatenate two character arrays (String Concatenation).
Program:
/*Concatenate 2 character arrays*/
#include<stdio.h>
main()
{
char a[100],b[100],c[100];
int i,j,k;
clrscr();
printf("Enter the 1st string:");
for(i=0;(a[i]=getchar())!='$';i++);
a[i]='\0';
fflush(stdin);
printf("Enter the 2nd string:");
for(j=0;(b[j]=getchar())!='$';j++);
b[j]='\0';
for(i=0,k=0;a[i];k++,i++)
c[k]=a[i];
c[k++]=' ';
for(j=0;b[j];k++,j++)
c[k]=b[j];
c[k]='\0';
printf("The concatenated string is:%s",c);
getch();
}
Sample run:
Enter the 1st string:selva$
Enter the 2nd string:raj$
The concatenated string is:selva raj
COUNTING THE NUMBERS OF WORDS, LINES AND
CHARACTERS IN AN ARRAY
Aim:
To Counting the numbers of Words, Lines and Characters in an array.
Program:
/*Count the number of words,lines & Characters*/
#include<stdio.h>
main()
{
char a[100];
int i,w=1,l=1,c=0;
clrscr();
printf("Enter the text:");
for(i=0;(a[i]=getchar())!='$';i++);
a[i]='\0';
for(i=0;a[i];i++)
{
if((a[i]==' '&&a[i+1]!=' ')||(a[i]=='\n'&&a[i+1]!='\n')||
(a[i]=='\t'&&a[i+1]!='\t'))
w++;
if(a[i]=='\n'&&a[i+1]!='\n')
l++;
if(a[i]!=' '&&a[i]!='\n'&&a[i]!='\t')
c++;
}
printf("The number of words in the text is %d\n",w);
printf("The number of lines in the text is %d\n",l);
printf("The number of characters in the text is %d",c);
getch();
}
Sample run:
Enter the text: the combination of computer studies in biological science
is bioinformatics$
The number of words in the text is 12
The number of lines in the text is 2
The number of characters in the text is 66
COUNTING THE NUMBER OF UPPERCASE AND LOWERCASE
ALPHABETS, DIGITS AND
SPECIAL CHARACTERS IN AN ARRAY
Aim:
To Counting the number of Uppercase and Lowercase Alphabets, Digits and
Special Characters in an array.
Program:
/*Counting number of lower & upper case alphabets,digits & spl characters*/
#include<stdio.h>
main()
{
char a[100];
int i,alp=0,dig=0,spc=0;
clrscr();
printf("Enter the string:");
for(i=0;(a[i]=getchar())!='$';i++);
a[i]='\0';
for(i=0;a[i];i++)
{
if(a[i]==' '||a[i]=='\t'||a[i]=='\n')
continue;
if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z'))
alp++;
else
if(a[i]>='0'&&a[i]<='9')
dig++;
else
spc++;
}
printf("No. of lower & upper case alpahets is %d\n",alp);
printf("No. of digits is %d\n",dig);
printf("No. of special characters is %d",spc);
getch();
}
Sample run:
Enter the string:BIOINFORMATICSfield&&$
No. of lower & upper case alpahets is 19
No. of digits is 0
No. of special characters is 2
CHECK THE NUMBER OF OCCURRENCES OF A PATTERN
Aim:
To check the number of occurrences of a pattern.
Program:
*check the number of occurance of pattern*/
#include<stdio.h>
main()
{
char a[100],b[100];
int i,j,k,count=0;
clrscr();
printf("enter the charecters:\n");
for(i=0;(a[i]=getchar())!='$';i++)
;
a[i]='\0';
fflush(stdin);
printf("enter the patern for matching\n");
for(j=0;(b[j]=getchar())!='$';j++)
;
b[j]='\0';
for(i=0;a[i];i++)
{
for(k=i,j=0;b[j]==a[k]&&b[j];k++,j++)
;
if(b[j]=='\0')
count++;
}
printf("the number of times %s is in %s is %d",b,a,count);
getch();
}
Sample run:
enter the charecters:
selva raj$
enter the patern for matching
raj$
the number of times raj is in selva raj is 1
CHECK THE OCCURRENCE OF A PATTERN AND SKIP THE
SAME
Aim:
To Check the occurrence of a pattern and skip the same.
Program:
/*match and skip the pattern if it occurs*/
#include<stdio.h>
main()
{
char a[100],b[100],c[100];
int i,j,k,l=0;
clrscr();
printf("enter the charecters:\n");
for(i=0;(a[i]=getchar())!='$';i++)
;
a[i]='\0';
fflush(stdin);
printf("enter the charecter for matches:\n");
for(j=0;(b[j]=getchar())!='$';j++)
;
b[j]='\0';
for(i=0;a[i];i++)
{
for(k=i,j=0;b[j]==a[k]&&b[j];k++,j++)
;
if(b[i]=='\0')
i=k;
else
c[l++]=a[k];
}
c[l]='\0';
printf("the array in which %s is skipped is %s",b,c);
getch();
}
Sample run:
enter the charecters:
bio tech$
enter the charecter for matches:
tech$
the array in which tech is skipped is bio
READ AND DISPLAY AN ARRAY OF 10 INTEGERS
Aim:
To read and display an array of 10 integers.
Program:
/*read and display array in 10 integer*/
#include<stdio.h>
main()
{
int a[100],n,i,sum;
clrscr();
printf("how many values you want to enter\n\n");
scanf("%d",&n);
if(n>100)
{
printf("your n value is exceeding");
return;
}
printf("enter the elements\n\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("this is the array you had entered\n\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n\nthe array in reverse order\n\n");
for(i=i-1;i>=0;i--)
printf("%d\t",a[i]);
for(i=sum;i+=sum;i++)
scanf("%d",&sum);
sum+=i;
printf("the sum of the digits are %d",sum);
getch();
}
Sample run:
enter the elements of the array
2134535646
the arrays are displayed
this is the array which is entered
2
1
3
4
5
3
5
6
4
6
REVERSE PRINT AN ARRAY OF 10 INTEGERS
Aim:
To reverse an array of 10 integers.
Program:
/*to reverse the array of 10 integers*/
#include<stdio.h>
main()
{
int a[10],i;
clrscr();
printf("enter the elements\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n this is the array numbers");
for(i=0;i<10;i++)
printf("%d\t",a[i]);
printf("\nthe array in reverse order is shown");
for(i=i-1;i>=0;i--)
printf("%d\t",a[i]);
getch();
}
Sample Run:
enter the elements
12 23 11 10 2 3 43 25 27 43
this is the array numbers
12
23
11
10
2
3
43
25
27
43
the array in reverse order is shown
43 27
25
43
3
2
10
11
23
12
SUM OF THE ELEMENTS OF THE ARRAY
Aim:
To print the sum of the elements of the array.
Program:
/*sum of elements in array*/
#include<stdio.h>
main()
{
int a[100],n,i,sum=0;
clrscr();
printf("enter how many values you want to enter\n");
scanf("%d",&n);
printf("enter the elements of a array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
printf("sum of %d elements is %d",n,sum);
getch();
}
Sample Run:
enter how many values you want to enter
4
enter the elements of a array
23 24 3 25
sum of 4 elements is 75
ARRANGE THE ELEMENTS IN ASCENDING ORDER
Aim:
To Arrange the elements in ascending order.
Program:
/*ascending order*/
#include<stdio.h>
main()
{
int a[100],n,i,j,temp=0;
clrscr();
printf("enter how many values you want to have\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
printf("the array in ascending order format is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Sample Run:
enter how many values you want to have
5
enter the elements of array
34275
the array elements are
3
4
2
7
5
the array in ascending order format is
2
3
4
5
7
ARRANGE THE ELEMENTS IN DESCENDING ORDER
Aim:
To Arrange the elements in descending order.
Program:
/*arrange the array in decending order*/
#include<stdio.h>
main()
{
int a[100],n,i,j,temp=0;
clrscr();
printf("enter the values you want to enter\n");
scanf("%d",&n);
printf("\nenter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nthe elements of the array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
printf("\nthe array in decending order is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Sample run:
enter the values you want to enter
5
enter the array elements
34265
the elements of the array is
3
4
2
6
5
the array in decending order is
6
5
4
3
2
FIND THE BIGGEST AND SMALLEST OF 10 INTEGERS
Aim:
To Find the biggest and smallest of 10 integers.
Program:
/*to find smallest and biggest in 10 integers*/
#include<stdio.h>
main()
{
int a[10],i,big,small;
clrscr();
printf("enter the elements of the array\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nthe array elements are\n");
for(i=0;i<10;i++)
printf("%d\t",a[i]);
big=a[0];
for(i=1;i<10;i++)
if(a[i]>big)
big=a[i];
printf("\n the biggest of 10 integer is %d\n",big);
small=a[0];
for(i=1;i<10;i++)
if(a[i]<small)
small=a[i];
printf("\n the smallest of 10 integer is %d\n",small);
getch();
}
Sample run:
enter the elements of the array
2354769891
the array elements are
2
3
5
4
7
6
the biggest of 10 integer is 9
9
8
9
1
the smallest of 10 integer is 1
INTERCHANGE THE BIGGEST AND SMALLEST OF 10
INTEGERS
Aim:
To Interchange the biggest and smallest of 10 integers.
Program:
/*to inter change big and small num in array*/
#include<stdio.h>
main()
{
int a[10],i,big,small,x,y;
clrscr();
printf("enter the array elements\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nthe array elements are\n");
for(i=0;i<10;i++)
printf("%d\t",a[i]);
big=a[0];
small=a[0];
for(i=1;i<10;i++)
{
if(a[i]>big)
{
big=a[i];
x=i;
}
}
printf("\nthe biggest of array is %d\n",big);
for(i=1;i<10;i++)
{
if(a[i]<small);
{
small=a[i];
y=i;
}
}
printf("the smallest number is %d\n",small);
a[x]=small;
a[y]=big;
printf("\n the inter changed array is \n");
for(i=0;i<10;i++)
printf("%d\t",a[i]);
getch();
}
Sample Run:
enter the array elements
3241576980
the array elements are
3
2
4
1
5
7
6
9
8
0
7
6
0
8
9
the biggest of array is 9
the smallest number is 0
the inter changed array is
3
2
4
1
5
MENU DRIVEN PROGRAM
Aim:
To write a menu driven program.
Program:
/* menu driven program*/
#include<stdio.h>
main()
{
int a,b,c,option,i,fac=1;
unsigned long int d,sum;
clrscr();
printf("pls wait for some minutes the menu is loading......\n\n");
sleep(3);
do
{
printf("******menu******\n\n");
printf("1.addition\n ");
printf("2 .subraction\n ");
printf("3.multiplication\n ");
printf("4.division\n ");
printf("5.modulation\n ");
printf("6.reverse the four digit\n ");
printf("7.factorial value\n ");
printf("8.odd or even\n ");
printf("9.leap year\n ");
printf("10.sum of the four digits\n ");
printf("11.ascii value of coding\n ");
printf("12.prime or composite\n ");
printf("13.exit\n ");
printf("\n\nenter the option code of program\n ");
scanf("%d",&option);
switch(option)
{
case 1:
clrscr();
printf("enter the value of a,b");
scanf("%d%d",&a,&b);
c=a+b;
printf("the value of %d and %d is %d",a,b,c);
getch();
clrscr();
break;
case 2:
clrscr();
printf("enter the value of a,b");
scanf("%d%d",&a,&b);
c=a-b;
printf("the value of %d and %d is %d",a,b,c);
getch();
clrscr();
break;
case 3:
clrscr();
printf("enter the value of a,b");
scanf("%d%d",&a,&b);
c=a*b;
printf("the value of %d and %d is %d",a,b,c);
getch();
clrscr();
break;
case 4:
clrscr();
printf("enter the value of a,b");
scanf("%d%d",&a,&b);
c=a/b;
printf("the value of %d and %d is %d",a,b,c);
getch();
clrscr();
break;
case 5:
clrscr();
printf("enter the value of a,b");
scanf("%d%d",&a,&b);
c=a%b;
printf("the value of %d and %d is %d",a,b,c);
getch();
clrscr();
break;
case 6:
clrscr();
i=1;
printf("enter the number a to be reversed");
scanf("%d",&a);
while(i<=4)
{
b=a%10;
printf("%d",b);
a=a/10;
i++;
}
getch();
clrscr();
break;
case 7:
clrscr();
printf("enter the a value");
scanf("%d",&a);
while(a!=0)
{
b=a%10;
fac=fac*b;
a-=1;
}
printf("the factorial value is %d",fac);
getch();
clrscr();
break;
case 8:
clrscr();
printf("enter the number to check");
scanf("%d",&a);
(a%2==0)?printf("the %d is the even number",a):printf("the %d
is the odd number",a);
break;
case 9:
clrscr();
printf("enter the year");
scanf("%d",&a);
(a%4==0)?printf("the %d is the leap year",a):printf("the %d is
a
ordinary year",a);
getch();
clrscr();
break;
case 10:
clrscr();
sum=0;
printf("enter the number");
sum=0;
scanf("%d",&a);
d=a;
while(a!=0)
{
b=a%10;
sum+=b;
a/=10;
}
printf("the sum of %lu is %d ",d,sum);
getch();
clrscr();
break;
case 11:
clrscr();
printf("enter the charecter");
scanf("%c",&a);
printf("the ascii code of %c is %d",a,a);
getch();
clrscr();
break;
case 12:
clrscr();
printf("enter the number");
scanf("%ld",&a);
while(fac<a)
{
b=a%fac;
if(b==0)
{
sum=sum+2;
}
fac++;
}
(sum==2||sum==0)?printf("%d is a prime
number",a):printf("%d is
a composite number",a);
getch();
clrscr();
break;
case 13:
clrscr();
exit();
clrscr();
break;
default:
printf("enter the correct case number\n\n");
break;
}
}while(option!=13);
getch();
}
Sample run:
******menu******
1. addition
2. subraction
3. multiplication
4. division
5. modulation
6. reverse the four digits
7. factorial value
8. odd or even
9. leap year
10. sum of the four digits
11. ascii value of coding
12. prime or composite
13. exit
enter the option code of program 6
enter the number a to be reversed 3456
6543
TERNARY OPERATORS
Aim:
To write program using ternary operator, to identify bigger of two numbers
Program:
/* ternary operators*/
#include<stdio.h>
main()
{
int a,b;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
a>b?printf("%d is bigger",a):printf("%d is bigger",b);
getch();
}
Sample run:
enter the value of a 6
enter the value of b 9
9 is bigger
TERNARY OPERATORS
Aim:
To write program using ternary operators to identify biggest among three numbers.
Program:
/*ternary operators for three numbers*/
#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf("enter the three numbers");
scanf("%d%d%d",&a,&b,&c);
(a>b&&a>c)?printf("%d is biggest",a):(b>c)?printf("%d is
biggest",b):printf("%d is biggest",c);
getch();
}
Sample run:
enter the three numbers
5
7
3
7 is biggest
CASE CONTROL STRUCTURE
Aim
To write the program to print the single digit numbers in words
Program:
/*print all single digit numbers*/
#include<stdio.h>
main()
{
int num;
clrscr();
printf("enter the number option that you want in words");
scanf("%d",&num);
fflush(stdin);
switch(num)
{
Case 0:
Clrscr();
printf("%d -zero",num);
break;
case 1:
clrscr();
printf("%d - one",num);
break;
case 2:
clrscr();
printf("%d - two",num);
break;
case 3:
clrscr();
printf("%d - three",num);
break;
case 4:
clrscr();
printf("%d - four",num);
break;
case 5:
clrscr();
printf("%d - five",num);
break;
case 6:
clrscr();
printf("%d -six",num);
break;
case 7:
clrscr();
printf("%d - seven",num);
break;
case 8:
clrscr();
printf("%d - eight",num);
break;
case 9:
clrscr();
printf("%d - nine",num);
break;
default:
clrscr();
printf("the %d is invalid enter the single digit value",num);
}
getch();
}
Sample run:
enter the number option that you want in words
7 - seven
CASE CONTROL STRUCTURES
Aim:
To write the program to print the tens in words
Program:
/* tens in words*/
#include<stdio.h>
main()
{
int num;
clrscr();
printf("\n\n\enter the tens value in numbers");
scanf("%d",&num);
switch(num)
{
case 10:
clrscr();
printf("%d - ten\n\n",num);
break;
case 20:
clrscr();
printf("%d - twenty\n\n",num);
break;
case 30:
clrscr();
printf(" %d - thirty\n\n",num);
break;
case 40:
clrscr();
printf("%d - fourty\n\n",num);
break;
case 50:
clrscr();
printf("%d - fifty\n\n",num);
break;
case 60:
clrscr();
printf("%d - sixty\n\n",num);
break;
case 70:
clrscr();
printf(" %d - seventy\n\n",num);
break;
case 80:
clrscr();
printf("%d - eighty\n \n",num);
break;
case 90:
clrscr();
printf("%d -ninety\n\n",num);
break;
default:
printf("%d is the invalid number\n\n",num);
break;
}
while(num);
getch();
}
Sample run:
enter the tens value in numbers
the 30 is the thirty
CASE CONTROL STRUCTURE
Aim:
To write the program in C language to print the number in words
Program:
/*for the teen values*/
#include<stdio.h>
main()
{
int num;
clrscr();
printf("enter the num that you want in words");
scanf("%d",&num);
fflush(stdin);
switch(num)
{
case 11:
clrscr();
printf("%d - eleven",num);
break;
case 12:
clrscr();
printf("%d -twelve",num);
break;
case 13:
clrscr();
printf("%d - thirteen",num);
break;
case 14:
clrscr();
printf("%d - fourteen",num);
break;
case 15:
clrscr();
printf("%d - fifteen",num);
break;
case 16:
clrscr();
printf("%d - sixteen",num);
break;
case 17:
clrscr();
printf("%d - seventeen",num);
break;
case 18:
clrscr();
printf("%d - eighteen",num);
break;
case 19:
clrscr();
printf("%d - nineteen",num);
break;
default:
clrscr();
printf("enter the valid num");
break;
}
getch();
}
Sample run:
enter the num that you want in words
17 - seventeen
Fly UP