Professional Documents
Culture Documents
Exercise:
do
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)
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;
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;
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
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
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++) {
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
/* Matrix
Diagonal Program to check whether a given matrix is diagonal matrix
-
else
printf("The matrix is not a sqaure matrix") ;}
&For example:
If input string is "Hello world!" and we want to find occurrence of T in the
#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
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++
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()
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' ||
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
#include <stdio.h>
#include <string.h>
void main(0
int a[10], t;
int i, j, n;
if (a[il>a[jl)
t=a[il;
a[il=ajl;
a]=t;
void main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;
scanf("%s", name[i]);
strcpy(tname[i], namefi);
Enter 3 names:
-=--. ====-
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 size,i,n[50],s;
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);
amount=(S0*1.5)+(150-50)*2.00)+(250-150)*3.00)+(unit-250)*4;
else{
printf("No usage ");
amount=0;
return amount;
int total=0;
int i
int unit=0;
int p;
for (i=0;i<size;i++)
p calc_Electricity(unit);
n[il=p;
total=total+n[i];
printf("the total amount of n devices is %d", total);
Output:
Enter the number of devices: 3
int k,5,i;
int a[3]-{0,1,2};
menudriven(a);
int i,n,s,pi
printf("Enter the value of n");
scanf("%d",&n);
forj=0;j<3;j++){
switch(ai])
case 0:
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)
Output:
Enter the value of n :100
struct date {
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)
>
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};
#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)
return 0;
#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;