You are on page 1of 21

Uni-

Exercise:

Check whether the required amount can be withdrawn based on


the available amount
#include < stdio.h>
unsigned long amount=1000, deposit, withdraw;
int ch, pin, I;
char transaction = 'y';
void main()

while (pin = 9090)

printf(ENTER YOUR SECRET PIN NUMBER:");


Scanf("od", &pin);
if (pin I= 9090)
printf("PLEASE ENTER VALID PASswORDIn");

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("%od", &ch);
switch (ch)

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("in PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");


else if (withdraw >(amount - 500))

printf("In INSUFFICENT BALANCE");

RMK
GROUP OF
INSTITUTONS
else
{
amount =
amount
- withdraw;
printf("In\n PLEASE COLLECT CASH");
printf("In YOUR CURRENT BALANCE
IS%lu", amount);
break;
case 3
printf("n ENTER THE AMOUNT TO
scanf("%lu", &deposit); DEPOSIT");
amount =
amount +
deposit;
printf("YOUR BALANCE IS %lu", amount);
break;
case 4:
printf("n THANK U USING
ATM");
break;
default:
printf("In INVALID CHOICE");
printf("\n\n\n DO U WANT TO
flush(stdin); CONTINUE?(Y/n): \n");
Scanf("%c", &transaction);
if (transaction
l= 1;
== 'n'|| transaction "'N')=

}
while ();
printf("n\n THANKS FOR USING OUT ATM
SERVICE");
Menu driven program to find the area of different shape
#include <stdio.h>
void main ()
{
int
ch,rad,length,width, breadth, height;
float area
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input your choice: ");
scanf("%d",&ch);

B GROUP
R.M.KOF
INSTITUTIONS
switch(ch)
case 1:
printf("Input radius of the circle: ");
scanf("%d",&rad);
area=3.14*rad*rad;
break;
case 2:
printf("Input length and width of the rectangle: ");
Scanf("%d%d",&length,&width);
area=length*width
break;
case 3:
printf("Input the base and height of the triangle: ");
scanf("%d%d",&breadth, &height);
area=.5*breadth*height;
break;

printf("The area is : %f\n",area);

Find the sum of even numbers


#include <stdio.h>
void main()

int i, X, sum=0;
/* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &x);
for(i=2; i=x; i+=2)
/* Add current even number to sum */
sum = sum+i;

printf("'Sum of all even number between 1 to %d =


%d", x, sum);

R.MK
GROUP OF
INSTITUTIONS
Onf+22

Operations
3) Insertion,
Insertion, 4)
selectiion, 3)
Traversal, 2)
Operation on array, includes: 1)
Deletion 5) Searching
Is
1) Traversal in an array,
element of a list, stored
operation in which each
Is an
aversal of the
lisl.
Visited.. element
element to the last
proceeds from the zeroth ve dd
he travel
Print the number of posiive
the list and
Exercise Program 1:
Traverse on
the array -as <0,=0,>0)
negative values present in
Algorithm
Step 1: get the elements
element.
oth element to the last
Step 2: visit all the elements from
if so do count of each criteria.
Step 3. chk for element is <0 =0 and >0, otn to
4: COunt of negative, zero and positive in which
travel proceeds from
Step
ast.
Step 5. print the count for each criteria.

#include< stdio.h>
void main(0

int list[10];
int n
int i, neg=0, zero=0, pos=0;
printf("n enter the size of the list\n");
Scanf("od",&n);
printf("Enter the elements one by one");
for(i=0;i<n;i+ +)
printf("\n Enter number %d number",i); OUTPUT:
scanf("%d", &list[i]); enter the aize of the l i s t
3
forfi=0;i<n;it+) Enter the elements one by one
Enter number 0 number
1
if(list[i]<0) Enter number 1 number
neg=neg+1; -2
else Enter number 2 number

if(list[i]==0)
No of Negative numbers in given list a r e
Zero=zerot1, No of 2eros in given l i s t are 1
1
else
No of Poaitive numbes i n given listt are 1
pos=pos+1,
printf("No of Negative numbers in given list are %d", neg);
printf("No of Zeros in given list are %d", zero);
printf("No of Positive numbers in given list are %d", pos);
bubble sort
kercise Program 2: Sort the numbers using

Bubbles up the highest

Sorted
Unsorted

10 54 54 54 54 54

47 10 47 47 47 47

12 47 10 23 23 23

54 12 23 10 19 19

19 23 12 19 10 12

23 19 19 12 12 10

Original After After After After After


List Pass 1 Pass 2 Pass 3 Pass 4 Pass 5

Bubble Sort (A[1, N) Complexity of Bubble Sort


Step 1: Repeat For P = 1 to N- 1 The complexity of sorting algorithm
Begin is depends upon the number of
Step 2 Repeat For J 1 to N- P comparisons that are made.
Begin Total comparisons in Bubble sort is
Step 3 If(A[J] <A[J-1]) n (n-1)/2 s n2-n
Swap (A [3], A[]-1] Complexity = 0 (n2)

End For
End For
Step 4 Exit
#include< stdio.h> Bubtle Sort

int main() {
int count,num[50],i;
to N 1
=1
-

For pass
printf ("How many elements to be sorted
:";
Scanf ("%d", &count);
For J 1 to N pass
printf("Enter the elements: \n");
for ( i = 0; i < count; i++) {

printf ("num[%d]: ", i ); scanf( "%d",


&num[iJ) A[]-11>A[J]

printf("n Array Before Sorting: \n"); A[J


Temp A[J-1J
1] = A [ J ] |

for (i=0;i<cOunt; i++) A[3] Temp


printf("%5d",num[i]);

int pass, Current, temp;

for
(pass=1;(pass<count); pass++) {
for (current=1;current<=count
pass current++{
Return
if (num[current-1]>num[current] )

temp = num|current-1]

num[current-1] = num[current];
How many elements to be sorted : 5
num[current] = temp; Enter the elements:
num [0] : 5

num [ 1] 9
num [2] 1
Dum[3] : 3
printf("nArray After Sorting: \n");
num[4] :7
for(i=0;i<count; i++)
printf("%5d",num[i]); Array Before Sorting :
5 9 1 3

Array After Sorting


1 3 7 9
Kercise Program 3: Find
iagonal or not. whether given is matrix is

/* Matrix
Diagonal Program to check whether a given matrix is diagonal matrix
-

/*A diagonal matrix is that square matrix whose *


to lower right are non-zero and all diagonal elements from upper left
other elements are zero. For example,
20 0
04 0
0 0 6
*
#include <stdio.h>
void main(){
int x[10][10], nr, nc, r, c, flag;
printr("Enter the number of rows and columns:
");
Scanf(%d %d", &nr, &nc);
if(nr==nc){ /* checking for square matrix */
printf("Enter elements of the matrix:\n");
for(r=0; r<nr; r++)
for(c=0 ;C<nc; c++)
scanf("%d", &x[r]Ic]);
flag=1
for(r=0;r<nr; r++)
for(c=0; C<nc; C++)
if(r==c)/*{true for diagonal elements */
if(x[r][c]=0)
flag=0; }
elsef
if(x[r][c]!=0)
flag=0;}
if(flag==1)
printf"The matrix is diagonal");
else
printf("The matrix is not diagonal");

else
printf("The matrix is not a sqaure matrix") ;}

Enter the number of rows and column3: 2

Enter elementa of the matrix:

The matrix is diagonal


UhH3
5 Exercise Programs:
character in a
Exercise program1 To find the frequency ofa
string
We will learn how to find occurrence of a particular character in a string
using C program?
with
Here, we are character array/string (character array is declaring
reading a
means maximum
the maximum number of character using a Macro MAX that
then we are
number of characters in a string should not more than MAX (100),
the characters which are
reading a character to find the occurrence and counting
equal to input character.

&For example:
If input string is "Hello world!" and we want to find occurrence of T in the

string, output will be T' found3 times in "Hello world!".

#include<stdio.h>
#define MAX 100
int main({
char str[MAX]={0};
char ch;
int count,i;

//input a string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces

getchar(); / get extra character (enter/return key)


/input character to check frequency
printf("Enter a character: ");
ch=getchar();
//calculate frequency of character Enter a
string: Hello World!
COunt=0; Enter a character: 1
for(i=0; str[i]!="\0'; i++) '1' found 3 times in "Hello
World!"

if(str[il==ch)
COunt++
printf("%c found %d times in
%s",ch,count,str);
return 0;}
s in a strin9
character
of all
To find the frequency which character is
i.e,
characters in a string, each of the
counts the frequency
of in the string "code
Sprogram example,
string. For alphabets
Only lower case
hoW many times in the
present occurredtime.
one ignored. You
'o' has are
characters'c, 'd,''e,'and and special
characters)
other characters (uppercase symbois.
handle uppercase and special
onsiaered,
Ldn modify this program to
so c is 0
suppose input string
begins with a
Explanation of "count[string[c]-a'l++", and we increment
count[O, i.e., a'
'a' =
0
nitially and string[0] 'a' and string[01
-

=
scanned.
nas occurred one time and repeat this until the complete string is

#include <stdio.h>
#include <string.h>
int main()

char string[100];
int c 0, count[26] =
{0}, x;
printf("Enter a string\n");
gets(string);
while (string[c] l= "\0)
** Considering characters from 'a' to 'z'
if (string[c]>= 'a' &&
only and ignoring others. *7
X= stringlc] -'a';
stringlc] 'z'){
<=

count[x]++;
C++

for (c 0; c< 26; c++)


=

printf("%c occurs %d times in the


return 0; string. \n", c +'a', count[c]);

OUTPUT
Enter a string
This
program
is easy 2 understand
a occurs
3 times in
the
b occurs 0 string.
times in the
occurs 0 times string.
in the
occurs 2 times in string.
the
occurs 2 times in string.
the
occurs 0 times in string.
the
g occurs 1 times in string.
the
string.
xercise program 2: To find the number of vowels, consonants
and white spaces in a given text
#inude stdio.h

nt main()

char linel 150];


int vowels, consonant, digit, space
vOwels Consonant = digit space = 0;

printf("Enter a line of string: ");

gets(line)
/tgets(line, sizeof(line), stdin);
for (int i-0; linelil t= "\O';
++i){
f (line[il ==
'a' || line[i] == 'e' || line[i] == 'i |||
ine[i] == 'o'|| linefi] == 'u' || line[i] = = "A' ||

inei== 'E' || line[i] == 'T || line[i] == 'O'||

line[i] == 'U') {

++VOwels,
else if (linei] >= 'a' && line[il<= "z2) || (line[i] >= 'A 8& linefi]<= "Z)) {
++Consonant;
else if (line[i] >= '0' && line[i] <=
'9') {
++digit,
else if (ine[i) == "') {
OUTPUT
++Space, aia.c: text-Uxl}: varning: the get:' ftuncicn is angezous and shoali ont te ced.
ater alite of sting This progran is easy 2wiecstan
swele:9
printf("Vowels: %d", vowels); ABONauts: 18

printf("nConsonants: %d", Consonant); igita:


ite spaces:
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
ters B1238 to exit console
return 0;
order */
CProgram to Sort array of nos ascending and descending

#include <stdio.h>
#include <string.h>

void main(0

int a[10], t;
int i, j, n;

printf("Enter the value of n \n");


scanf("%d", &n);
printf("Enter %d numbers: \n" , n);
for (= 0i<n;i++)
Scanf("%d', &a[i]);
for (i =0; i<n-1;i++)
for ( = i+ 1;j < n; j++)

if (a[il>a[jl)
t=a[il;
a[il=ajl;
a]=t;

printf("Ascending Order Nos\n") OUTPUT


for (i =
0; i <n; i++) Enter the value of na
5
printf("%d\t", a[i]); Enter 5 numbers:
7 3 5 4 1
printf("nDescending Order Nos\n");
for (i = n-1; i>= 0; i--)
Ascending Order Nos
3 5 7
Descending Order Nos
printf("%d\t", a[il); 3

Program finished with exit code 2


Press ENTER to exit
console.
Exercise program3: Sorting the names

CProgram to Sort Names in Alphabetical Order */


#include <stdio.h>
#include <string.h>

void main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;

printf("'Enter the value of n \n");


Scanf(%d", &n);
printf("Enter %d names: \n" , n);
for (i = 0; i < n; it+)

scanf("%s", name[i]);
strcpy(tname[i], namefi);

for(i =0; i<n-1;i++)


for (j = i + 1;j< n; jt+)
if (strcmp(name[i], name[jl) > 0)
{
strcpy(temp, namel0);
strcpy(namef), namejl);
strcpy(namej], temp}; OUTPUT
Enter the value of n

Enter 3 names:

printf("n ----n"); xerox


printf("Input NamestSorted names\n"); aPple

printf(-- -n"); Apple


for(i = 0; i< n; i++)
Tnput NamestSorted names
printf("%sNt%s\n", tname[i], name[il); -------=----*
xerox Apple
Pple aPple
printf"- --\n"); Apple xerox

-=--. ====-
Oni
Exercise Programs:
consumed by 'n' devices
of power
Calculate the total amount
(Passing an array to a function):
#include <stdio.h>

#include <stdlib.h>

int calc_Electricity();://function prototype

int devices(int n[], int size);


int main()

int size,i,n[50],s;

printf("enter the size of an array');


scanf("%d",&size);
Ss=devices(n ,size);
printf("the value is",s);
return 0;

int
calc_Electricity(int unit){//function definition
printf("Enter total units consumed\n");
scanf("%d",&unit);
double amount;

if((unit>=1)&&(unit<=50)//between 1 50 units

amount=unit"1.50;
if((unit>50)&&(unit<=150)//between 50 150 units

amount=((50*1.5)+(unit-50)*2.00);

elseif((unit>150)8&(unit<=250)K//between 150 250 units


amount=(50*1.5)+(150-50)*2.00)+(unit-150)*3.00;

else if(unit> 250){//above 250 units

amount=(S0*1.5)+(150-50)*2.00)+(250-150)*3.00)+(unit-250)*4;

else{
printf("No usage ");

amount=0;

l/printf"Electricity bill = Rs. %.2f",amount);

return amount;

int devices(int n[],int size)

int total=0;

int i

int unit=0;

int p;

for (i=0;i<size;i++)
p calc_Electricity(unit);

printf("the amount of one bill %d is",p);

n[il=p;
total=total+n[i];
printf("the total amount of n devices is %d", total);

Output:
Enter the number of devices: 3

Enter total unit consumed 200

The total amount of 1 device is 425, Total amount of n device is 425

Enter total unit consumed: 250

The total amount of 1 device is 575, Total amount of n device is 1000

Enter total unit consumed : 200


The total amount of 1 device is 425, Total amount of n device is 1425
kercise:
program to count the numbers which are divisible
Menu-driven
by 3, 5 and both (passing an array to a function)
#include<stdio.h»

int menudriven(int a[]);


int main()

int k,5,i;

int a[3]-{0,1,2};

menudriven(a);

int menudriven(int al])

int i,n,s,pi
printf("Enter the value of n");

scanf("%d",&n);

forj=0;j<3;j++){
switch(ai])

case 0:

for (i=1; i<=n; i++)

if(i%5==0)

p p+1;}}
printf("nthe total numbers divisible by 5 is %d",p);
case 1:
p=0;
for (i=1; i<=n, t*)

if (19%3-=0)

p-p+1,}}
printf("\nthe total numbers divisible by 3 is %d",p);
case 2:

p-0;
for (i=1; i<=n; i++)

if (i%3==0&&i%5-=0)

P=p+1)

printf("nthe total numbers divisible by both are %d".p);

Output:
Enter the value of n :100

The total numbers divisible by 5 is: 20


The total numbers divisible by 3 is: 33
The total numbers divisible by both is : 6
Uni+5
JIDED
ACTIVITY Here is the activity you on (compare datesS

zinclude < stdio.h>

struct date {

intdd, mm, yyi};


intdate_cmp(struct date d1, struct date d2);
void date_print(struct date d);

int main()K
struct date di = {7, 3, 2005)
struct date d2 = {24, 10, 2005};

date_print(d1);
int cmp = date_cmp(d1, d2);
if (cmp == 0)

printf(" is equal to");


else if (cmp > 0)
later than ");
printf(" is greater i.e.
earlier than");
else printf(" is smaller i.e.
date print(d2);
return 0;
and d2 */
/*Compare given dates d1
date di, struct date d2){ ==d2.yy)
int date_cmp(struct d2.mm && d1.yy
if (d1.dd
== d2.dd &8 d1.mm ==
==
d2.yy
return 0; && di.mm >
d2.mm || d1.yy
d2.yy
d2.yy || d1.yy
==

>
else if (d1.yy > d2.dd)
d2.mm &8 d1.dd
&& d1.mm
==

return 1;
else return -1;}

* print a given
date */
date d) {
void date_print(struct
printf(%d/%d/%d", d.dd, d.mm, d.yy);}
Utuitua lu TUUTT
(pdssily d s
Sll ULuIe duu
iuILIUIS
The logic is
the current age. Th benis
birth and print
oi d persu U>iy
t dye
c
will read your date of current date and print how old
Age Calculator: This program
given date with the
will compare
to implement this program Program
are you?
/Age Calculator (C program to calculate age)."1|
#include<stdio.h>
int date_diff(struct date dti, struct date dt2);

struct date {
intday, month, year;

intmain(){
struct date dtl = {05, 10, 2020};
struct date dt2 = {17, 05, 2004};

intcmp date_diff(dti, dt2);


return cmp;}

int date_diff(struct date dt1, struct date dt2){


int years,months,days;
if(dt2.year>dt1.year) {
years=0; months-0; days=0;
printf("n I can't travel in time");}
else if(dt2.year==dt1.year){
years=0;
if(dt2.month>dt1.monthX
months=0; days=0;
printf("n I can't travel in time");}
else if(dt2.month==dt1.month){ months=0;
if(dt2.day>dt1.day){
days=0;
printf("n I can't travel in time");}
else if(dt2.day==dt1.day){ days=0;
printf("n Welcome to Earth");}
else
days=dt1.day-dt2.day;}
else{
months=dti.month-dt2.month
if(dt2.day>dt1.day) { months
days-30-dt2.day+dti.day;}
else
days=dt1.day-dt2.day;}}
else {
years=dti.year-dt2.year;
if(dt2.month>dt1.month) {
years-
months=12-dt2.month+dt1.month;
days=30-dt2.day+dt1.day;}
else {
months=dtl.month-dt2.month;
if(dt2.day>dti.day) {
months--;
days=30-dt2.day+dti.day;}
else
days=dt1.day-dt2.day;}}
printf("In Your age is %d years, 9%d months, %d days",years,months,days);}

Your age is 16 years, 4 months, 18 days R.M.K


GROUP OF
INSTITUTIONS
cis behinc
IDED
ow old ACTIVITY -Here is the activity you on (Time in C)

#include <stdio.h>
#include <stdlib.h>>
#include <time.h>
/Print current date and time in C
int main(void){
/variables to store date and time components
int hours, minutes, seconds, day, month, year,
I/time_t is arithmetic time type
time_t now;
I/Obtain current time
time() returns the current time of the system as a time_t value
time(&now);
I/Convert to local time format and print to stdout
printf("Today is : %s", ctime(&now));
I/ localtime converts a time_t value to calendar time and
returns a pointer to a tm structure with its members
I/ filled with the corresponding values
struct tm *local =
localtime(8&now);
hours = local->tm_hour;
/get hours since midnight (0-23)
minutes = local->tm_min; get minutes passed after the hour (0-59)
seconds = local->tm_sec; / get seconds passed after minute (0-59)

day local->tm_mday; get day of month (1 to 31)


// get month of year (0 to 11)
month local->tm_mon+1; 1900
year local->tm_year +1900; get year since
/ print local time
if (hours < 12) // before midday
printf("Time is : %02d:%02d:%02d am\n", hours, minutes, seconds);
else / after midday
printf("Time is: %02d:%02d:%02d pm\n", hours 12, minutes, seconds);

/print current date


printf("Date is: %02d/%02d/%d\n", day, month, year);

return 0;

roday i s : Fri 0ct 9 04:37:08 2020


Time i s : 04:37:08 am
Date i s : 09/10/2020

. . Program finished with exit code 0


Press ENTER to exit console.
CUriStuery TIS
Uilie Dy
Ldliie idlt
tU Li lE
empuyee
dii
TufIIDEr ui udys
E and functioiis
of structures
arrval time for 30 days (Use array

#include <stdio.h>
#include <time.h>
struct student{
char lastName[100];
char firstName[100];
char *date
int age
int id;}
int main()
int n=1;
struct student s[n];
int>
do
printf(main menu :\n1.add\n2.delete\n3.diplayln4.exit\n");

scanf"%d",8x);
switch(x){
case 1:
for(int i-0;i<n;i++{
printf("Enter first name\n");
scanf("%s",s[i].firstName);
printf("Enter last name\n");
scanf("%s",S[i].lastName);
printf("Enter your id\n");
scanf("%d",&s[i].time);
printf("Enter your ageln");
scanf("%d"8s[i].age);
time_t timer;
timer=time(NULL);
s[i].date asctime(localtime(&timer)
I/si].time=time(&now);

for(int i-0;i<n;i++X
printf("id tfirstName\tlastNameltageltdaten%d\t%s\t%s\%d\t%s",si].id,s[i].firstNa
me,s[i]lastName,s[i].age,s[i].date);

break;
case 2:
break;
case 3:
break;
case 4:
break,
default:
printf('wrong choice");
break;

while(xl=4)
return 0;

Note: time_t t R.MK


time(&t); GROUP OF
printf("n current time is : %s",ctime(&t); INSTHTUTIONS

You might also like