Professional Documents
Culture Documents
Itp Aasign
Itp Aasign
Itp Aasign
Assignments
By
Seema Shrestha
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]);
11
{
/* If duplicate element is found */
if(array[i]==array[j])
{
count++;
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
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;
txt[i] = chr;
}
else if(chr >= 'A' && chr <= 'Z'){
chr = chr - shift_key;
txt[i] = chr;
}
}
return 0;
}
Output:
Enter a message to decrypt: vhhrf
Enter shift_key: 5
16
Decrypted txt: qccma
--------------------------------
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
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