Itp Aasign

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

The British College

Assignments

By

Seema Shrestha

January 17, 2019

Saroj Regmi and Introduction to Programming, Section A

1
1. Write a currency converter program. You can find the current conversion rates on
www.nrb.org.np. Display menu to convert at least 5 different foreign currency to
equivalent Nepalese amount. Make use of validators to print error if user enters
some negative amount.
Solution:
#include <stdio.h>
int main(){
int choice; // choice is the option selected from the menu
float rupees;
printf("\n MENU\n"); // displays the menu to the user
printf("1. U.S DOLLAR \n");
printf("2. Brazilian Real \n");
printf("3. U.K POUND STERLING \n");
printf("4. SWISS FRANK \n");
printf("5. Korean Won \n");
printf("\n Choose the currency : "); // asks the user to select the option from the
above menu
scanf("%d",&choice);
printf("\n Enter the amount that you want convert to Rupees : "); //asks the user to enter
the smount that they want to convert
scanf("%f",&rupees);
if (rupees < 0){ // checks wether the entered amount is negative and displays the same
printf("\n ERROR! \n");
}
else{
switch(choice){
case 1: // converts US dollars into Nepalese rupees and displays the result
printf("\n Total amount : %f \n",rupees * 112.44);
break;

2
case 2:// converts brazilian real into Nepalese rupees and displays the result
printf("\n Total amount: %f \n",rupees * 0.032600);
break;
case 3:// converts uk pound sterling into Nepalese rupees and displays the result
printf("\n Total amount : %f \n",rupees * 143.06);
break;
case 4:// converts swiss frank into Nepalese rupees and displays the result
printf("\n Total amount : %f \n",rupees * 114.63);
break;
case 5:// converts korean won into Nepalese rupees and displays the result
printf("\n Total amount : %f \n",rupees * 9.8493);
break;
default :// displays error message if the user enters the option other than 1 to 5
printf("\n* ---- INVALID ENTRY ----PLEASE CHECK THE MENU NUMBER AND
TRY AGAIN---------- *\n");
}
}
return 0;
}

Output:
MENU
1. U.S DOLLAR
2. Brazilian Real
3. U.K POUND STERLING
4. SWISS FRANK
5. Korean Won

3
Choose the currency: 5
Enter the amount that you want convert to Rupees: 110
Total amount: 1083.423000
--------------------------------

2. You are assisting a researcher in collecting sample data and producing a report on
the frequency of repetition on the sample data. You collect data three times a day
for a weeks’ time and check the repetition of a pattern. All pattern data are stored in
integer counts. Write a program to store the sample data in a two-dimensional array
where one dimension is used to store the number of samples taken and the other to
store the pattern count. After entering the data, print the output in a horizontal
graph using “*”’s to determine individual count frequency. For example: If the
pattern 6 is seen 5 times then print “Pattern - 6: *****”.
Solution:
#include <stdio.h>
int main()
{
int data[7][3],onedata[21];
int
row,column,i=0,zero=0,one=0,two=0,three=0,four=0,five=0,six=0,seven=0,eight=0,nine
=0;
printf("Enter the data:\n");
for (row=0;row<7;row++)
{
for (column=0;column<3;column++)
{
scanf("%d",&data[row][column]);

4
}
}
for (row=0;row<7;row++)
{
for (column=0;column<3;column++)
{
onedata[i]=data[row][column];
i++;}
}
for (i=0;i<21;i++)
{
if (onedata[i]==0)
{
zero++;
}
else if (onedata[i]==1)
{
one++;
}
else if (onedata[i]==2)
{
two++;
}
else if (onedata[i]==3)
{
three++;
}

5
else if (onedata[i]==4)
{
four++;
}
else if (onedata[i]==5)
{
five++;
}
else if (onedata[i]==6)
{
six++;
}
else if (onedata[i]==7)
{
seven++;
}
else if (onedata[i]==8)
{
eight++;
}
else if (onedata[i]==9)
{
nine++;
}
}
if (zero!=0)
{

6
printf("Pattern-0: ");
while (zero!=0)
{
printf("*");
zero--;
}
}
if (one!=0)
{
printf("\nPattern-1: ");
while (one!=0)
{
printf("*");
one--;
}
}
if (two!=0)
{
printf("\nPattern-2: ");
while (two!=0)
{
printf("*");
two--;
}
}
if (three!=0)
{

7
printf("\nPattern-3: ");
while (three!=0)
{
printf("*");
three--;
}
}
if (four!=0)
{
printf("\nPattern-4: ");
while (four!=0)
{
printf("*");
four--;
}
}
if (five!=0)
{
printf("\nPattern-5: ");
while (five!=0)
{
printf("*");
five--;
}
}
if (six!=0)
{

8
printf("\nPattern-6: ");
while (six!=0)
{
printf("*");
six--;
}
}
if (seven!=0)
{
printf("\nPattern-7: ");
while (seven!=0)
{
printf("*");
seven--;
}
}
if (eight!=0)
{
printf("\nPattern-8: ");
while (eight!=0)
{
printf("*");
eight--;
}
}
if (nine!=0)
{

9
printf("\nPattern-9: ");
while (nine!=0)
{
printf("*");
nine--;
}
}
return 0;
}

Output:
Enter the data:
147
258
369
123
456
789
159
Pattern-1: ***
Pattern-2: **
Pattern-3: **
Pattern-4: **
Pattern-5: ***
Pattern-6: **
Pattern-7: **
Pattern-8: **

10
Pattern-9: ***

Another solution:
#include <stdio.h>
#define size 21

int main()
{
int array[100], frequency[100];
int i, j, count;

// Input in array
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &array[i]);

// Initially initialize frequencies to 1


frequency[i] = 1;
}

for(i=0; i<size; i++)


{
count = 1;
for(j=i+1; j<size; j++)

11
{
/* If duplicate element is found */
if(array[i]==array[j])
{
count++;

/* Make sure not to count frequencyuency of same element again */


frequency[j] = 0;
}
}
/* If frequency of current element is not counted */
if(frequency[i] != 0)
{
frequency[i] = count;
}
}
/*
* Print frequency of each element
*/
for(i=0; i<size; i++)
{
if(frequency[i] != 0)
{
printf("\nPattern of elements of array %d occurs : %d times ", array[i],
frequency[i]);
}

12
for(j = 1; j<= frequency[i]; j++)
printf("* \n");
}
return 0;
}
Output:
Enter elements in array: 1 2 3
456
7 18 14
15 14 16
18 16 3
52 35 54
35 54 52

Pattern of elements of array 1 occurs : 1 times *


Pattern of elements of array 2 occurs : 1 times *
Pattern of elements of array 3 occurs : 2 times **
Pattern of elements of array 4 occurs : 1 times *
Pattern of elements of array 5 occurs : 1 times *
Pattern of elements of array 6 occurs : 1 times *
Pattern of elements of array 7 occurs : 1 times *
Pattern of elements of array 18 occurs : 2 times **
Pattern of elements of array 14 occurs : 2 times **
Pattern of elements of array 15 occurs : 1 times *
Pattern of elements of array 16 occurs : 2 times **
Pattern of elements of array 52 occurs : 2 times **
Pattern of elements of array 35 occurs : 2 times **

13
Pattern of elements of array 54 occurs : 2 times **
--------------------------------

3. Caesar cipher is a type of substitution cipher in letter in a plain text(normal text that
we can understand) is replaced by a letter that is some fixed number of positions
down the alphabet. For example, with a right shift of 4, a letter D will be replaced by
H, and A will be replaced by E. So if the plain text is HELLO the cipher text will be
(with a right shift of 4) LIPPS. [Caesar cipher:
https://en.wikipedia.org/wiki/Caesar_cipher]. Write a program that asks user to
enter key (shift position/ left or right) and generate a cipher text of plaintext work
entered by user.
Solution:
#include<stdio.h>
int main()
{
char txt[100], chr;
int i, shift_key;
// Input
printf("Enter a txt to encrypt: ");
gets(txt);
printf("Enter shift_key: ");
scanf("%d", &shift_key);
// check /0
for(i = 0; txt[i] != '\0'; ++i){
chr = txt[i];
// check character and encrypt
if(chr >= 'a' && chr <= 'z'){

14
chr = chr + shift_key;
if(chr > 'z'){
chr = chr - 'z' + 'a' - 1;
}
txt[i] = chr;
}
else if(chr >= 'A' && chr <= 'Z'){
chr = chr + shift_key;
if(chr > 'Z'){
chr = chr - 'Z' + 'A' - 1;
}
txt[i] = chr;
}
}
printf("Encrypted txt: %s", txt);
return 0;
}
Output:
Enter a txt to encrypt: seema
Enter shift_key: 3
Encrypted txt: vhhpd
------------------------------

Write another program that asks for cipher text and key and print the decoded
plain text of that word.

15
Solution:
#include<stdio.h>

int main()
{
char txt[100], chr;
int i, shift_key;

// Input
printf("Enter a message to decrypt: ");
gets(txt);
printf("Enter shift_key: ");
scanf("%d", &shift_key);

// check \0
for(i = 0; txt[i] != '\0'; ++i){
chr = txt[i];

// check character
if(chr >= 'a' && chr <= 'z'){
chr = chr - shift_key;

if(chr < 'a'){


chr = chr + 'z' - 'a' + 1;
}

txt[i] = chr;
}
else if(chr >= 'A' && chr <= 'Z'){
chr = chr - shift_key;

if(chr < 'A'){


chr = chr + 'Z' - 'A' + 1;
}

txt[i] = chr;
}
}

printf("Decrypted txt: %s", txt);

return 0;
}
Output:
Enter a message to decrypt: vhhrf
Enter shift_key: 5

16
Decrypted txt: qccma
--------------------------------

4. Write a number guessing game where computer generates a random number


between 0 and 100. The program then asks user to enter his guess and prints out
“&lt;”, “&gt;” or “=” to define whether the number generated by computer is less than,
greater than or equal to the number entered by user respectively. The program ends
when the number entered by user is equal to the number generated by computer.
The program further prints the number of guesses user has made before they made
the correct guess. [Hint: a random number can be generated and stored in a integer
using
rand(time(NULL));//generate a different seed on each run
int a = rand()%101; //generate a random number between 0 and 100]
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
srand(time(NULL)); // generates a different number every time the program is run
int r = rand() % 101; // generates a random value and stores it in r
int correct = 0; // sets the initial value of correction to 0
int counter = 0; // sets the initial value of counter to 0
int guess; // stores the guesses made by the user

printf("Guess my number! ");

do {

17
scanf("%d", &guess);
if (guess == r) { // checks if the guess is equal to the number if true prints the
following messsage
counter++; // increases the value of counter by one
printf("You guessed correctly in %d tries! Congratulations!\n", counter);
correct = 1; // sets the value of correct to 1
}

if (guess < r) {//check if the guess is loiwer than the number and if true prints <
counter++;
printf("guess a greater number : ");
}

if (guess > r) { //check if the guess is higher than the number and if true prints >
counter++;
printf("\n guess a smaller number : ");
}
} while (correct == 0); //repeats the loop if the value of correct is 0 and the loop ends
only when the value of correct is 1

return 0;
}
Output:
Guess my number! 50
guess a greater number : 55
guess a greater number : 60
You guessed correctly in 3 tries! Congratulations!
--------------------------------

18

You might also like