ASSIGNMENT-1
NAME: M PUNEETH SLOT: L43+L44
REG NO: 19MID0069 ADVANCED C PROGRAMMING
1. Write a C program to print your First name using printf statement .
Code:
#include<stdio.h>
int main()
{
printf("PPPPP\tU U\tN N\t E E E E E E E E T T T T T H H\n");
printf("P P\tU U\tN N N\t E\t E T H H \n");
printf("PPPPP\tU U\tN N N\t E E E E E E E E T H H H H H \n");
printf("P\tU U\tN N N\t E\t E T H H \n");
printf("P\tU U U U U U\tN N\t E E E E E E E E T H H ");
return 0;
}
Output:
2. There are three batsmen from a state. The state government likes to present
the best batsman award. Given the name and runs of each person, write a C
program to find the best batsman using ternary operator. (1 mark)
#include<stdio.h>
int main() {
int bat1,bat2,bat3,best;
char name1[20];
printf("\n*****The state government likes to present the best batsman award.*****");
printf("\nThese are the three players");
printf("\nEnter name of the player: ");
gets(name1);
printf("Runs scored by the player:" );
scanf("%d", &bat1);
while (getchar() != '\n');
char name2[20];
printf("Enter name of the player:");
gets(name2);
printf("Runs scored by the player:");
scanf("%d", &bat2);
while (getchar() != '\n');
char name3[20];
printf("Enter name of the player: ");
gets(name3);
printf("Runs scored by the player: ");
scanf("%d", &bat3);
best = (bat1 > bat2) ? ((bat1 > bat3) ? bat1 : bat3) : ((bat2 > bat3) ? bat2 : bat3) ;
printf("\nThe Highest runs among three players : %d", best) ;//printing highest runs of the
player among three batman's.
if (best == bat1)
printf("\nBest batsman award goes to %s",name1);
if (best == bat2)
printf("\nBest batsman award goes to: %s", name2);
if (best == bat3)
printf("\nBest batsman award goes to: %s", name3);
getch() ;
return 0;
}
Output:
3. Let a=0, b=2, c=-2 , Find the output of the following expression and also
state the reason (1 mark) --a*(10-b)/2-c++*b+(a+b+c)
#include<stdio.h>
int main() {
printf("NAME : M PUNEETH\nREG NO-19MID0069");
int a=0,b=2,c=-2,result;
result=(--a*(10-b)/2-c++*b+(a+b+c));
printf("\n\nResult for the expression is %d", result);
return 0;
}
According to associativity and precedence rules it takes the expression
like this
First checks the increment/decrement operators and then (* and /) then
(+ and -).
So here, --a=-1 and c++=-2
--a*(10-b)/2-c++*b+(a+b+c) = -1*(10-2)/2-(-2)*2+(0+2-2)
= -1*(8)/2-(-4)
= -1*4+4
= -4+4= 0 .
Output: 0
4. Write a C program to count the number of vowels and consonants in
your name irrespective of their case (upper or lower case). The letters
b,h,s,p and empty spaces must be considered as special characters not as
consonant. (2 marks)
#include <stdio.h>
int main()
{
char s[20];
int i, nc=0, nv=0, ns=0, na=0;
printf("NAME : M PUNEETH\nREG NO-19MID0069");
printf("\nEnter name:");
gets(s);
for(i=0; s[i] != '\0'; i++) {
nc++;
if(s[i]==' ' || s[i]=='b' || s[i]=='h' || s[i]=='s' || s[i]=='p' || s[i]=='B' || s[i]=='H' ||
s[i]=='S' || s[i]=='P')
ns++;
// Here the letters b,h,s,p and white space are considering as special characters
not as consonants.
else if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E'
|| s[i]=='I' || s[i]=='O' || s[i]=='U')
na++;
else if(s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z')
nv++;
}
printf("\nNumber of special characters:%d",ns);
printf("\nNumber of vowels:%d", na);
printf("\nNumber of consonants:%d", nv);
return 0;
}
Output:
5. Write a C program to perform ATM transaction. The transactions are Balance
checking, Cash withdrawal and Cash deposition. Firstly, initialize the ATM pin as
your registration number. Take the ATM pin and the balance amount as input. If
the input pin is equal to the initialized pin, then do the further operations or send
an error message “Enter the correct PIN”. Use switch statement to do the
operations like Balance checking (1), Cash withdrawal (2), Cash deposition (3) and
Quit (4). Use while loop to terminate or restart the process. (3 marks)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned long amount=1000, deposit, withdraw;
int choice, k;
char pin[9];
char transaction ='y';
void main()
printf("NAME : M PUNEETH\nREG NO-19MID0069");
int flag=0;
while (flag==0)
printf("\nENTER YOUR SECRET PIN NUMBER:");
scanf("%s", pin);
if (strcmp(pin,"19MID0069")==0)
flag=1;
else
printf("PLEASE ENTER VALID PASSWORD\n");
do
printf("*Welcome to ATM Service*\n");
printf("1. Check Balance\n");
printf("2. Withdraw Cash\n");
printf("3. Deposit Cash\n");
printf("4. Quit\n");
printf("*?*?\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
case 1:
printf("\n YOUR BALANCE IN Rs : %lu ", amount);
break;
case 2:
printf("\n ENTER THE AMOUNT TO WITHDRAW: ");
scanf("%lu", &withdraw);
if (withdraw % 100 != 0)
printf("\n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");
}
else if (withdraw >(amount - 500))
printf("\n INSUFFICENT BALANCE");
else
amount = amount - withdraw;
printf("\n\n PLEASE COLLECT CASH");
printf("\n YOUR CURRENT BALANCE IS%lu", amount);
break;
case 3:
printf("\n ENTER THE AMOUNT TO DEPOSIT");
scanf("%lu", &deposit);
amount = amount + deposit;
printf("YOUR BALANCE IS %lu", amount);
break;
case 4:
printf("\n THANK U USING ATM");
exit(0);
break;
default:
printf("\n INVALID CHOICE");
printf("\n\n\n DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): \n");
fflush(stdin);
scanf("%c", &transaction);
if (transaction == 'n'|| transaction == 'N')
k = 1;
} while (!k);
printf("\n\n THANKS FOR USING OUT ATM SERVICE");
OUTPUT:
6. Tell me about a time when you came up with a new approach to the
above problems. (1 mark to show up your individuality, answer should
be sound and sensible – Answers repeated no marks, so don’t share
your answers)
1. I saw the question for the first time because my previous
faculty didn’t ask this type of questions before…later realised
it is the simple printf statement and given the code.
2. In the process of writing code, I understand that there are
some issues in executing code like input buffer.
I cleared the input buffer using y=the statement
while (getchar() != '\n');
and for printing the name of the best batsman I used if statements in the
end of the code after printing the highest runs.
3. Simple code but their lot to understand behind working. Got to know
about the precedence and associativity rules and working of
complier.
4. To restrict some consonants and white space to come under special
characters, and to know the number of vowels and remaining
consonants, I used “for loop” and “if statements” in an ordered way
so, I got the clear output.
5. To get pin as my registration number and to process the code, I used
this statement if (strcmp(pin,"19MID0069")==0) so, the compiler
didn’t confused and will take the input. And used the do while
condition and switch case inside it to process the remaining
questions.