CS Programming _Dr.Sanjeev
CS Programming _Dr.Sanjeev
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.
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.
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
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
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
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") ;
#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);
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
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