0% found this document useful (0 votes)
3 views

CS Programming _Dr.Sanjeev

The document contains a collection of programming examples and exercises primarily in C language, covering various topics such as directory commands, basic arithmetic operations, float operations, conditional statements, and clean programming practices. It includes multiple programming tasks that require the reader to write code for specific problems, such as calculating areas, finding roots of equations, and manipulating numbers. The content serves as a guide for learning programming concepts and improving coding skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CS Programming _Dr.Sanjeev

The document contains a collection of programming examples and exercises primarily in C language, covering various topics such as directory commands, basic arithmetic operations, float operations, conditional statements, and clean programming practices. It includes multiple programming tasks that require the reader to write code for specific problems, such as calculating areas, finding roots of equations, and manipulating numbers. The content serves as a guide for learning programming concepts and improving coding skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

mkdir hari To make directory

Simple Programs
#include<stdio.h>
ed hari To change directory main()
vi t.c To enter into editor
int a,b.c,d;
To enter into insert mode printf("give two numbers");
Esc To leave insert mode scanf("%d %d",&a,&b);
ld To delete a line ca/b
:W To save d-a%b;
To quit from editor printf(%d od",c.d)
cc t.c To compile file t.c
/a.out To execute Here/ is quotient and % is remainder.
87/7 is 12. 87%7 is 5.
#include<stdio.h>
main() #include<stdio.h>
printf("Rajiv \n Kumar Gupta"); main()
printf("Ajay \n chandra"); int a,b,c;
printf("nRam prakash"); printf'give a number");
scanf%d",&a);
b-a%10;
#include<stdio.h> ca/10;
main() printf(%d %d",b,c);
int a,b;
printf("Give side of square"); a%10 finds last digit of a number. a/100
scanf("%d",&a); deletes the last digit.
b=a*a,
printf("The area is %d",b); #include stdio.h>
main()
{int a,b,c;
a-42315;
b-9;
ca*10+b
printf(*%d",c);
1. Write program, which reads as input sides of a rectangle and prints its area.
2. Write program, which reads 2 numbers and prints the sum of square of the first and
cube of the second. e.g. input 5 3 output 5+3 =52.
3 Write program, which reads 4 numbers a, b, c and p. Let fx)=ax-+bxte be a function.
The program outputs the value of flp). e.g input 4 3-1 2 output 4(2)+3(2)-1=21.
4. Write program to delete the last digit. input 13613 output 1361. input 324 output 32.
5. Write program to deletelast two digits. input13613output 136. input 324 output 4.
6. Write program to print the second last digit. Input 23617 output 1.
7. Program to find the sum of last two digits. For aboveinputs output 1+3=4 and 2+4-6.
8. Write program to double the last digit. e.g. input 23613 output 23616. input 324 output
328. (assume that last digit is less than 5)
9 Write program to delete the second last digit. e.g. input 23617 output 2367,
10. Write program to exchange last two digits. e.g. input 23617 output 23671.
Float Operations
#include<stdio.h>
#include stdio.h>
#includemath.h> #include<math.h>
main() main()
{float x,y float x,y,Z,
printf("nGive a number"); printf("\nGive a number");
scanf("%f",&x); printf(" between -1 and 1");
ycos(x); printf("%f".y); scanf("%f"&x);
=acos(x);
The above program finds the cosine of an z=y*180/3.14;
angle. However the angle must be printf("cos inverse in radians %f",y);
specified in radians. If input is 1.05 then printf(" and in degrees %f",z)
output is 0.5. It is because 1.05° is
approximately 600 To compile the Here acos(x) means cos"(x)
program use cc t.c -lm Input 0.5 output 60". Input .87 output 30"
1. Write program, which reads a, b and c as sides of a triangle and prints area. Hint: area
ys(s-a)s-b)(s-c) .[Hint: s is(a+b+c/2] [sqrt(x) will find square root|. Input 5
7 10 output 16.24.
2. Write program, which reads a, b, c and d and finds distance between points (a,b) and
(c,d). input 3, 7, 11, 13 output 10.
3. Write program, which reads 6 numbers a, b, c, d, e and f. The program outputs the area
of the triangle whose end points are (a,b), (c,d) and (e,f). [Hint: use above two
questions]. Input 7 3 11 3 76output 6.
Write program, which reads a, b, and c. Let ax + by + c = 0 be equation of a line. The
4
program outputs the slope. Input 3 5 8 output -0.6.
5. Write program, which reads a, b, c, d and e and prints the distance between point (a,b)
and line cx+dyte=0. [Hint: (ac+bd+e)/*+d)i"] input 6 7 3 4 2 output 9.6.
6. Write program, which reads a, b, and c. Let x +y + ax + by + c = 0 be equation of a

circle. Print its center and radius. Input 10, -6 and -2 then output is center (-5,3) and
radius 6. Here circle is x+y2+10x-6y-2=0.
7. Write program, which reads a, b, c, p, q and r. Let ax + by + c =0 and px + qy + r= 0
be equations oflines. Print their point ofintersection. Input 4 8 12273 output (-5,1).
8. Write program, which reads a, b and c as sides of a triangle and prints the angle A in
degree and radian. Hint: a? = b+ c*- 2bcCOS(A). [Hint: use acos. Example: input
13, 12, 5 output 90. input 10, 20, 17.32 output 30. input 7, 7, 7 output 601
9. Write program, which reads h, k, r, and s. Let a circle has center (h.k) and radius r. Let
line x=s intersects the circle. The program, the chord length. Input 2 4 13 7 output 24.

[Hint: find distance of the line from the center].


10. Modify above program to find the area of the triangle formed by the points of
intersection and the center of the circle. Input 2 4 13 7 output 12x13=60.
11. Modify above program to output the points of intersection of the circle and the line.

Input 2 4 13 7 output (7,16) and (7,-8).


d. e f g. and h. Let axtbytcztd=0 be a plane and
12. Read a, b, c,
xty2+2+ex+fy+gzth=0 be a sphere. Find the area of circle of intersection of the
Computer Programming
Dr. Sanjeev Kumar
The 1CFAI University, Dehradun
If else
#include<stdio.h> #include<stdio.h>
main() #include math.h>
float a,b.m: main)
printf("\nGive two numbers"); float a,b.c.det.rl,r2;
scanf("%f %f".&a,&b): printf("inGive a b c");
if (a>b) m-a; else m=b; scanf("%f %f %f".&a,&b,&c);
printf"%f is bigger".m);
det-b*b-4*a*c;
if (det<0) printf("Imaginary roots");
else {
rl--b+sqt(det)/(2*a):
2-(-b-sqrt(det)) (2*a);
printf("roots %f %f",rl,r2);

1. Write program, which reads


three numbers and prints the biggest.
2. Write program, which reads
four numbers and prints the biggest.
3. Write program, which reads 2
numbers and prints both of them. The bigger number is
printed first.
4.Write program, which reads a, b and c as sides ofa
is 90 or not. [Hint: if (a2
triangle and prints whether angle A
b+c)] [Do
= =
not use
5. Write program, which reads a, b, and c. Let ax +
cos etc]
by + c =0 be equation of a line.
Print its slope. The program also prints whether the line is vertical or not.
6. Write program, which reads a, b and c. Let ax*+ bx + c =0 be a quadratic equation. If
roots are real and distinct then both roots are printed. If roots are equal then only one
root is printed. If roots are imaginary then real part and complex parts of both roots are
printed. e.g. if input is a=1 b-8 c=25 then output is-4, 3 and-3. if input a-2 b-8
c=8 output 2. Input 2 10 12 output -3 and -2.
7. Modify above program to print roots. e.g. for above input output -4+3i and -4-3i.
8. Write program, which reads three numbers. Two of these are same and one of them is
different. The program outputs the diferent number. e.g. input 5 52 output 2. Input 4
3 4 output 3. Input 5 22 output 5.
9. Write program, which reads 5 numbers a, b, c, d, and x. Here a, b, c, and d are distinct
and x is equal to exactly one of a or b or c or d. The program output which is equal to
x. e.g. input 5 7967 output x is equal to b. input 8 7 1 31 output x is equal to c.
10. Write program, which reads 5 numbers a, b, c, d, and x. The program output how
many among a, b, c and d are equal to x. e.g. input 5 7977output 2. input 5 3 8 7 2
output 0. input 5 2 225 output 1.
11. Write program, which reads three numbers. The program outputs the middle of these.
5 62 output 5.
e.g. input 524 output 4. Input
12. Read p, q, r, a, b, c. Let ax+bytc=0 be a line. Let (p,q) be the center of a circle and r
be its radius. The program finds whether the circle and the line intersect or not. If they
intersect let A and B be the points of intersection of the circle and the line. Find the
area of the triangle formed by A, B and the center of the circle. [Hint: Find the
distance of the line from the center. If it is more than the radius then circle and the
Clean programming

Ordinary Program Clean program program when brackets of


main() main) else are removed
int x. a=S: int x, a=5; main)
printf("Give x"): printf"Give x"); int x.a-5
scanf(%d".&x): scanf(%d",&x); printf("Give x");
if(x>10) if(x>10) scanf(%d",&x);
a-a+2:
else
asa+2; if(x>10)
else a=a+2;
a-a+10: a=at10; else
aa+x:} aatx; a-a+10;
printf%d".a); a=atX,
printf(%d",a); printf(%d".a);
For each of above case observe the output when
Write clean programs for each of the
input is x=1 and x=20.
following. Also observe the output on the given inputs.
1. scanf(%d".&x); x=3,14,16,25 6. scanf("%d",&x);
if(x>10) 4. scanf(%d",&x) if (x10) 9. Xx-5;
a-30; a=0; aa+2; while (x<80)
else if (x>4) if (x>10) if(x%2==1) xX+9;
a40: a=a+2; aatx, x=X+10;
else a=50;
if(x%2-=1) a=at10;}} printf%d",x);
printf%d"a); aa+x; a=at100;
Input x=3,6,20 aa+10; printf"%d",a); 10. x=5;
a=at100; x=3,8,13,14 while (x<80)
2. scanf"%d",&x); printf(%d",a); x=x+9;
if (x>10) x-3,8,13,14 7. scanf("%d",&x); x=x+10:}
if (x>20) a-6; printf(%d".x);
a-30; 5. scanf "%d",&x) if(x>10)
else a=40; a-0; if (x>20) 11.x-5:y=5;
else a-50; if(x>10) a-30; while (x<80)
printf(%d",a); a-a+2; else a=40; N=x+9;
Input x=3,16,25 if(x%2==1) printf(%d",a); if (x<60)
a-atx; Input x=3,15,25 y=y+2;
3. scanf%d",&x); a-a+10;} printt(%d.od".x.y):
8. scanf("%d".&x):
if(x>10) a=at100;}
if (x>20) printf(%d",a); a-6; 12.x-5:y-5:
x=3,8,13,14 if(x>10) while (x<80)
a-30;
else if (x>15) if(x>20) x N+9;
a-30;} if (x<60)
a=40;
else a-50; elsea-40; =y+2:3
else a=60; printf(%d",a); printf(%d,%d".x.y):
printf(%d",a); Inputx-3,15,25
Boolean connectives

main()
{int x;
for (x=0:x<100:x++) The given program prints numbers
if (((x>50) && (x<70)) || (x%2--0)) between 0 and 100, which are either even
printf("%d.".x); or between 50 and 70.

main()
int x: The given program prints
for (x=0:x<100:x++) numbers which are
between 20 and 70 but not
if((x>20)&&(x<70)&:& (x>40)&&(x<60)
printf("%d,".x); between 40 and 60.

Write following programs by modifying the line containing if only.

1. Write a program, which prints all even numbers between 20 and 70.
2. Write program, which prints all numbers between 20 and 40, and all even numbers
between 50 and 80.
3. Write program, which will print all even numbers less then 50 and all odd numbers
more than 50.
4. Write program, which prints all even numbers between 20 and 40, and all odd
numbers between 50 and 80.
5. Write program, which will print all numbers which are multiple of either 3 or 7.
6. Write program, which will print all numbers which are either between 50 and 70, or
less than 20, or more than 90.
7. Write program, which will print all numbers, which are even but not a multiple of
8 14 1622..
either 3 or 5. e.g. 2 4
8. Write program, which will print all numbers, which are either a multiple of 3 or 5 but
not both.
those numbers whose last digit is multiple of 3. e.g. 0.
9. Write program, which will print
3, 6,9, 10, 13, 16, 19, 20, 23,...
those numbers whose last digit is between 5 and 8.
10. Write program, which will print
5, 6, 7, 8, 15, 16, 17, 18, 25, 26, numbers whose sum
. . .

e.g.
11. Write program, which will print
those of both digits is multiple of
7. e.g. 0,7,16, 25, 34, 43, 52, 59, 61,
. .

will print all numbers between 10 and 19, 30 and 39, 50 and 59,
12. Write program, which
90 and 99. [Hint: check
condition ((x/10)%62) 1]. ==

,
those numbers whose first digit leaves remainder 1
which will print
13. Write program, 49, 70, 71, 79
11, 19,40, 41,
when divided by 3. e.g. 10,
..., ..,
. ,

which will print all odd numbers between O and 9, 20 and 29, 40 and
14. Write program, I0 and 19, 30 and 39,... . 90 and 99.
and all even numbers between
49,.. 80 and 89
Switch and Condition

1. Write program, which reads an integer X and prints an integer Y. Y is X+10 if X is


between 10 and 30. Y is 3*X if X is between 50 and 70. Otherwise Y is X-2.

2. A student is awarded Ex grade if he gets more than 90 marks. He is awarded A grade


if marks are between 80 and 89. Similarly range for B, C, D and P are 70-79, 60-69,
50-59, and 35-49 respectively. The student is awarded F grade if he gets less then 35
marks. Write a program, which reads marks of a student and prints his grade.

3. No income tax is to be paid if income is less than 5000. If income is between 5000 and
6000 then tax is 10% of the amount by which the income exceeds 5000. If income is
between 6000 and 15000 then the tax is 100 + 20% of the amount by which the
income
exceeds 6000. If income is more than 15000 then the tax is 1900 +30% of the
amount by which the income exceeds 15000. e.g. if income is 10000 then the tax will
be 100+ (10000-6000)*20/100 900. Write a program, which reads income and
calculates the income tax.

4. Write a program, which reads a number X and prints a number Y. Y=X+10 ifX is 6.
Y is X*X if X is 7. Y is 2*X+4 if X is 12. Otherwise Y is X*6-1.

5. Write a program, which reads three integers X, Y and Z and prints Y+Z if X is 0. If X
is 1 then Y-Z is printed. If X is 2 then Y*Z is
printed. If X is 3 then Y/Z is printed.

(
Series
double x,sum.term.fact; int i.n; the sum of
finds
printf("Give x and n"): The given program

scanf("%lf %d".&x.&n):
term=1; sum=1; fact=1: 2! 3 n!
for (i=1:i<=n:i++) become ex.
When n is large the above
term-term*x: x-2 output e-7.39
input
factfact*i
sum-sumtterm/fact: of following
Write program to find sum

series. Do not use more than one loop.


printf "The answer is %lf".sum); not be used.
Function 'pow' should
printf("nThe exp(x) is %lf".exp(x);:
result print the value of the function.
To verify the correctness of the
|x<l input 0.3 output 2.04
1. 1+2x+3x+4x*+ . . .
+ (n+1)x" It is for large n and
(1-x)
1 n=100 output 0.769
2. 1-x+x*-x* +... +(-1)" x" It is for x1 input x=0.3
(1+x)
n=100 output 0.642
3. 1-x - x*+x*+ x* - x' -

x° +x+ . . . n terms input x= 0.3


0.356
4 4 r++ + Itis-log(1- x) for |x|K1 input x=0.3 n=100 output
n

x=1.05 output 0.5


5.
5. 1 It is cos(x) input x=0.3n=10 output 0.95
It is sin(x) input x=0.3 n=10 output 0.29 x=1.05 output 0.866
. +
X
15x.. It is for x1 input x= 0.3 n=100 output 0.874
7. x3x +6x* + 10x + -
(1- x)
+x' +x* +x0 +x!l+ x!3 + For x-2 and n-5 the answer is
8. x +x2 +x + x 7. Do it for both
182 or 54. For x=1 and n=10 the answer is 10 or answers.

reads x and n and finds f(n).


For followings, write program which
9. f0)=x f(1)=x+1f2)-2(x+1)+1 f3)=3(2(x+1)+1)+1... for x=1 f{5)=326

10. fO)=x
f0)=x 1)-x+1 f2)-2(x+1)+1 f3)=4(2(x+1)+1)+1 f(4)=8(4(2(x+1)+1)+1)+1
f5)-16(8(4(2(x+1)+1)+1)+1)+1 f6)-32(16(8(4(2(x+1)+1)+1)+1)+1)+1
for x=1 f(5)=2705, f(7)=5539905

11.fO)=x f1)=x2 f(2)=x*+x f(3)-x(x t x*)+x* fi)-xf(i-1)+1(i-2) tor x=2 t5)=140

12. f(1)=1+ f2)-1+- f3)-1+ f(4)-1+ f(7)1.616


1+ +
2 1+

4
Picture Printing
#include<stdio.h> #include<stdio.h> #include<stdio.h>
main ()
main () main ()
int X; char c int i
printf ("Give number") ; Cgetchar (); printf ("Give a number") ;
scanf (" 8d", &x) printf ("Bd", c) ; scanf (" td", &x);
printf ("ram8com \n", x); printf("gopal") ;

It outputs ASCII code of a fflush (stdout) ;sleep (2);


It outputs the character, whose printf ("%c", x) ;
character. fflush (stdout) ;sleep (2)
ASCIl code is given.
printf("hari\n");
Give input 8 and 13
main ()
int i; The give program outputs BCDEF.
for (i=l; i<=5; i=i+1)
Modify "65+i" to print followings:
printt ("8c", 65+i); FEDCB ABBCC
1. ABCDE CEGIK

#include<stdio.h>
main () The give program outputs ABCDEF.
int i; Modify "for (i=65;i<=70; i++)
"
to print followings:
for (i-65; i<=70; i=i+1)
2. BCDEFGHIJ ACEGI FEDCBA4
printf ("sc", i);

The given program prints following picture.


for (1ine=1;line<=5;line++)
ABCDE
f o r (i=1;i<=6-line;itt)
BCDE printf("%c", 63+linet+i);
CDE printf("\n") ;
DE
E
In the give program replace "63+line+1" by suitable expression to print following pictures.
3. 5. 7. 9.
ABCDE AAAAA EFGHI ABCDE
ABCD BBBB DEFG CDEF
ABC CCC CDE EFG
AB DD BC GH
A E A I
4. 6. 8. 10.
EDCBA EEEEE EDCBA ABCDE
DCBA DDDD FEDC ACEG
CBA CCC GFE ADG
BA BB HG AE
A A A
The given program prints following picture.
AAAAA for (line=l;1ine<=5; linet+)
AAAA f o r (i=1; i<=6-line; i++)
AAA printf ("A") ;
AA print f ("\n");
A
#include<stdio.h> In the
main () previous problem the inner loop
int i; can be replaced as follows
for for (i=1; i<=7-1ine; it+)
(i=65; i<=90; i++)
printf ("%c", 63+i+line);
printf ("%c", i)
for (1ine=1;1ine<=6; linett)
output ABCD
f o r (i=1;i<=6-1ine; itt)
int i,gr; printf(" ") ;
for (gr=1;gr<=5;gr++)
for (i=65; i<=64+1line;it+)
for (i=1; i<=6-gr; it+) printf("%c", i);
printf ("&c", 64 +gr); printf("\n")
A
AB
output AAAAABBBBCCCDDE ABC
What output will be produced if
64+gr is ABCD
replaced by (A)64+i (B)70-gr ABCDE
(C)70-i (D)63+i+gr ABCDEF

for (gr=1;gr<=5; gr++) for (1ine=1;line<=5; line++)


f o r (i=1;i<=gr; i++) f o r (i=1;i<=line; i++)
printf ( "A"); printf ("Gc", 96+i) ;
for (i=5-gr; i>=1;i--) for (i=70-line; i>=65; i--)
printf ("X") ; printf("%c", i);
printf("\n");
AXXXXAAXXXAAAXXAAAAXAAAAA
aEDCBA
int i,gr: abDCBA
for (gr=1;gr<=4;gr++) abcCBA
for (i=65; i<=69; i++) abcdBA
printf("$c",i); abcdeA
printf ( "X");
int i,line; charp;
ABCDEXABCDEXABCDEXABCDEX for (1ine=1;1ine<=3;linet+)
for (i=97;i<=100;i++)
int i, line; { p= (char) i;
for (1ine=1;1ine<=6; line++)
printf ("%c", p);
for (i=64+line;i<=70;i++)
printf ("%c", i); for (i=65; i<=71+line; i++)
printf ("\n") ; printf("%c", (char) i):
for (i=line-1; i<=4;it+)
ABCDEF
printf ("%d", i);
BCDEF printf ("\n"):
CDEF
DEF
abcdABCDE FGHO1234
EF abcdABCDEFGHI 1234
F abcdABCDEFGHIJ234
Write program to print 14. A
following pictures AA 20. K
AAA
1. AAAAAA AAAA IHG
2. AAABBBCCCDDD AAAAA HGFE
AAAAAA GFEDC
3. ABCABCABCABC
FEDCBA
4. ABBCCCDDDD [Solve following two
5. AAAABBBCCD problems by replacing
6. AAAXAAAXAAAX 65 by ... in abovej 21. A
15. A BC
7. AaAAaAAAaAAAAa
BC DEF
8. ACEGIKM
GHIJ
CDE KLMNO
9. ABCDEF DEFG
PQRSTU
ABCDE EFGHI
ABCD FGHIJK
16. ABCDEF
22 A
ABC
AA
AB ACEGI
AAAA
A ADGJ AAAAAAA
AEI AAAAAAAAAAA

10. A AF
AB A
23. A
17. ABCDEF
ABC BC
BBCCDDEEFF
ABCD CDEF
CCCDDDEEEFFF
ABCDE DEFGHIJ
DDDDEEEEFFFF
ABCDEF EFGHIJKLMNO
EEEEEFFFFF

ABCDEF FFFFFF
11. 24. UJ
BCDEF ST
CDEF 18. A
PQR
DEF ABA
LMNO
EF ABCBA
GHIJK
ABCDCBA
ABCDEF
ABCDEDCBA

12. Aabcde 25. ABDGKPV


ABabcd 19. F
26. 1,2,3,4,5,6,
ABCabc EF
ABCDab DEF
27. 0,4,8,12,16,20,24,28,
CDEF
ABCDEa 0,5,10,15,20,25,30,
ABCDEF
BCDEF
0,6,12,18,24,30,
ABCDEF
0,7,14,21,28,
13. aBCDE F 0,8,16,24
abCDEF 0,9,18,27,
abcDEF 0,10,20,30,
abcdEF

abcdeF 28. 0,4,8,12,16,20,24,28,


0,5,10,15,20,25,30,
int x,k,.i,sum=0; int x.d;
Digits
int x,d.y-0,t=1;
scanf("%d%d",&x,&k); printf("Give number:"); scanf("%d",&x);
for (i=1;i<=k;it+)
scanf("%d",&x); while (x>0)
sum=sum+(x%10); do d-x%10;
X=x/10; d-x%10; if (d%2-=0)
printf("%d,",d); y=y+t*d;
printf("%d ",sum): x=x/10; t=*10;
while (x>0); x=x/10;
The progranm to find the The program to print all printf("%d".y);
sum of last k digits of a digits of a number program deletes odd
number. digits.
In following programs only one loop should be used unless essential
23.
1. Write program to delete the last k digits. e.g. input 23617 and k=3 output
and k=4 output 5.
23617
2. Write program to print the k" digit from last. e.g. input
2.
3. Write a program to print first digit. e.g. input 23516 output
Write program to print the second digit. e.g. input
23516 the output is 3.
2+3+6+1+7=19.
23617 output
5. Write program to find sum of all digits. Input when the
consecutive digits. e.g.
6.Write which will find sum of product to
program,
3x1 + Ix4 +4x5 =33.
is 23145 the output is 2x3
+
input number of
(assume that both have
same

T.Write program, which


reads two number 327
digits. Input
the sum of product of corresponding
digits). The program outputs
3x5+2x3+7x9-84.
and 539 output 21463 output 3, 60, 400,
positional values of digits. Input
8. Write program to print
1000 and 20000.
23617 output 2+6=8.
9. Write program to
find sum even digits. Input423
of 21151 output 5.
Input output 3. Input
to find number digits.of
10. Write program In above case 2 and 1 respectively.
to find number of even digits. 23613 output 6.
11. Write program the even digit. e.g. input
last
print
to
12. Write program
while (x%2* 0) x=x/10] the last even digit. In
above case 3.
[Hint: immediately before
digit
to print the last even digit. In above case1
13. Program
the digit immediately after the
to print 23617 output 6.
14. Write program which is mult1ple of 3. e.g. input
the last digit,
to print
input 23863 output 8 (do
not
15. Write program the second last even digit. e.g.
to print
16. Write program use two loops]
325145761 output 4. [Hint:
use if). Input
3 times. Input 61389426
after last odd digit. Input
number. Do half of number 261. Input 78 output 222 (74*3).
17. Read a Input 87 output
184167639 (61389213*3).
output numbers
formed by consecutive digits. Input
program,
finds the sum
which of
18. 2Write
415 output 24+41+15-80.
consecutive digits. In above
numbers formed by exchanging
sum of
19. Find
42+14+51=107.
71236 output 2.
print
tothe first even digit. e.g. input
23617 output 6,3.
20. Write program which are multiple of 3. e.g. input
to print digits, the output all
21. Write program and prints another integer. In
which reads an integer
decrement. e.g. input 23145 output
22044.
22. Write a program, and odd digits are
are retained
even digits
Factors
int i,x, int i,x int i,x,
printf("Give number"): printf"Give number");
scanf("%d".&x);
scanf("%d".&x); i-2: scanf("%d",&x);
for (i-2:i<=x:i++) for (i-2;xoi!-0;i++);
while (x%il-0)
if (x%i-=0) iit1 printf("%d",i);
Alternative method to
printf("%dn".i); printf("%d"i);
prints all factor Finds smallest factor find the smallest factor

The number 385 has factors 5, 7, 11, 35, 55, 77, 385. The smallest factor is .

5 715575.
. Write program, which will print last digits of all factors. Inputlast385twooutput
digits. Input 385 output
S0
W r i t e program, which will output all factors by exchanging
70 11 53 55 77 358. 12 +24 =

3 +4 +6 +8 +
of all factors (input 24 output 2
+
3. Write program, which prints sum

59). 24 output 7. Input 35 outout 3.


4. Write program, to find the number of factors. Input If the
if the given number is prime.
. Write program, which reads a number and prints "yes" number is
Smallest factor of prime
number is not a prime then "no" is printed. (Hint:
given
13 "yes".
equal to itself). Input 24 output "no". Input andoutput
its count. When number is 24 (1,2) (2.3) (3,4)
6. Write program, which outputs all factors
(4.6) (5,8) (6,12) (7,24) will be printed. the kh factor. If given number is 24
7. Write program, which reads another integer
k and prints
and k is 5 then output is 8.
which leave remainder 2
number and prints only those factors,,
8. Write program, which reads a and 24.
number is 24 then its factors are 2, 3, 4, 6, 8, 12,
when divided by 5. e.g. if given
However, only 2 and 12 will be printed.
factor other than itself.
9. Write program, which prints biggest If number is 24 then
the smallest and second smallest factors.
10. Write program, which prints
is 65 then outputs are 5 and 13.
outputs are 2 and 3. If number of smallest factors. e.g. input
two numbers and finds the sum
11. Write program, which reads
number 35 and 21 output 5+3-8. finds the
number. Let t be its smallest factor. The program
12. Write program, which reads a
3 (since smallest factor of 77 is 7 and smallest factor
smallest factor of t+2. e.g. input 77 output
of 7+2 is 3). Input 55 output 7. If given numbers are
two numbers and prints all common factors.
13. Write program, which reads
and 6.
24 and 54 then outputs are 2, 3
24 35 output 1.
14. Modify the above program
to print the greatest common factor. Input
to print the smallest common factor. Input 24, 15 output3.
15. Modify the above program least common multiplier. e.g. input 24
reads two numbers and finds the
16. Write program, which above
216. [Do not use the method of (a*b/gcd(a,5)).] [Hint: modify
and 54 then output is
program by replacing x%i by i%x] 'while' (in place of for).
reads a number and prints all factors using
17. Write program, which
finds the sum of product of consecutive factors. e.g. if given number is
18. Write program, which
114. Since 2.3+3.4+4.6+6.12=l |14.
12 then answeris

You might also like