_________________________________________________________________________________________________
C For Swimmers
______________
If u don't know how to swim, learn it now. Otherwise participate in the competition.
_________________________________________________________________________________________________
Topic : Programming Problems (Part I)
_____ ____________________
*************************************************************************************************
NOTE : * Use the necessary header files.
* Use comments to explain the underlying logic of various program features.
* Use of spacing, indentation, etc. is strongly encouraged as a matter of good programming
practice.
* Enter, compile and execute the C programs. Also verify that they run correctly with your
particular version of C. (If any of the programs do not run, try to determine why.)
*************************************************************************************************
[Q001]. Write a program (W.A.P.) in C to SWAP the contents of 3 variables without using the
temporary (or extra) variables.
Ans.
/* 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);
}
void 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);
} /* 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.
Ans.
/* 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>
void main(void)
{
long i,oddnum=1,sqrnum,sum=0;
clrscr();
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)));
} /* End of Main */
_________________________________________________________________________________________________
[Q003]. W.A.P. in C to multiply any two numbers without using * (asterisk) and other arithmetic
operators like +, -, / and %. [Hint : Use BITWISE OPERATORS]
Ans.
/* Multiplication of two numbers using BITWISE OPERATORS ONLY */
#include<stdio.h>
void main()
{
long int i,n,mul,mul2,count,temp,a,b,sum,carry,res,tot;
clrscr();
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();
} /* 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]
Ans.
/* 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>
void main(void)
{
long x;
clrscr();
printf("Enter a number : ");
scanf("%ld",&x);
if ((x & 0x1) == 0x0)
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();
} /* 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.
Ans.
/* 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]);
}
}
void main()
{
int item,ch;
clrscr();
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");
}
}
} /* 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.
Ans.
/* 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 randomly generated number.");
printf("\n3. Hints will be provided during the PLAY.");
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. ***");
} /* 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)
Ans.
/* To determine how much money in a piggy bank */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void 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();
} /* 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])
Ans.
/* Denominations */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void 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();
} /* End of Main */
_________________________________________________________________________________________________
[Q009]. 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.]
Ans.
/* Counting vowels and consonants in a given line of text */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void 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);
} /* 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.
Ans.
/* 4-letter word unscrambling */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void 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 4-letters only ");
getch();
} /* End of Main */
_________________________________________________________________________________________________
No comments:
Post a Comment
Your comments are welcome!