Docsity

SIMPLE C PROGRAMS TP PRACTICE, Exercises of C programming

Toggle sidebar
bg1
PROGRAMS
1. Print your name
#include <stdio.h>
int main()
{
printf("Prince");
return 0;
}
2. Print “GOOD MORNING” vertically
#include <stdio.h>
int main()
{
printf("G\nO\nO\nD\n\nM\nO\nR\nN\nI\nN\nG");
return 0;
}
3. Sum of two numbers
#include <stdio.h>
int main()
{
int a, b, sum;
scanf("%d%d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
4. Swap two numbers
#include <stdio.h>
int main()
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download SIMPLE C PROGRAMS TP PRACTICE and more Exercises C programming in PDF only on Docsity!

PROGRAMS

1. Print your name

#include int main() { printf("Prince"); return 0; }

2. Print “GOOD MORNING” vertically

#include int main() { printf("G\nO\nO\nD\n\nM\nO\nR\nN\nI\nN\nG"); return 0; }

3. Sum of two numbers

#include int main() { int a, b, sum; scanf("%d%d", &a, &b); sum = a + b; printf("Sum = %d", sum); return 0; }

4. Swap two numbers

#include int main()

int a, b, temp; scanf("%d%d", &a, &b); temp = a; a = b; b = temp; printf("a=%d b=%d", a, b); return 0; }

5. Average of three numbers

#include int main() { float a, b, c, avg; scanf("%f%f%f", &a, &b, &c); avg = (a + b + c) / 3; printf("Average = %f", avg); return 0; }

6. Area of circle

#include int main() { float r, area; scanf("%f", &r); area = 3.14 * r * r; printf("Area = %f", area);

int a, b, c; scanf("%d%d%d", &a, &b, &c); if(a > b && a > c) printf("%d", a); else if(b > c) printf("%d", b); else printf("%d", c); return 0; }

10. Menu-driven area program

#include #include int main() { int ch; float r, l, b, h, area; scanf("%d", &ch); switch(ch) { case 1: scanf("%f", &r); area = 3.14 * r * r; printf("%f", area); break; case 2: scanf("%f%f", &l, &b); area = l * b; printf("%f", area);

break; case 3: scanf("%f%f", &b, &h); area = 0.5 * b * h; printf("%f", area); break; default: exit(0); } return 0; }

11. Multiplication table

#include int main() { int n, i; scanf("%d", &n); for(i = 1; i <= 10; i++) printf("%d x %d = %d\n", n, i, n*i); return 0; }

12. Sum of first n numbers

#include int main() { int n, i, sum = 0; scanf("%d", &n);

rem = n % 10; rev = rev * 10 + rem; n /= 10; } if(rev == temp) printf("Palindrome"); else printf("Not Palindrome"); return 0; }

15. Armstrong number

#include int main() { int n, sum = 0, rem, temp; scanf("%d", &n); temp = n; while(n > 0) { rem = n % 10; sum += remremrem; n /= 10; } if(sum == temp) printf("Armstrong"); else printf("Not Armstrong");

return 0; }

16. Factorial

#include int main() { int n, i, fact = 1; scanf("%d", &n); for(i = 1; i <= n; i++) fact *= i; printf("%d", fact); return 0; }

17. Fibonacci series

#include int main() { int n, a = 0, b = 1, next, i; scanf("%d", &n); for(i = 0; i < n; i++) { printf("%d ", a); next = a + b; a = b; b = next; } return 0; }

#include int main() { int i, j, n; scanf("%d", &n); for(i = n; i >= 1; i--) { for(j = 1; j <= i; j++) printf("*"); printf("\n"); } return 0; }

21. Same number pattern

#include int main() { int i, j, n; scanf("%d", &n); for(i = 1; i <= n; i++) { for(j = 1; j <= i; j++) printf("%d", i); printf("\n"); } return 0; }

22. Leap year

#include int main()

int y; scanf("%d", &y); if((y%4==0 && y%100!=0) || (y%400==0)) printf("Leap year"); else printf("Not leap year"); return 0; }

23. Simple calculator

#include int main() { int a, b; char op; scanf(" %c", &op); scanf("%d%d", &a, &b); switch(op) { case '+': printf("%d", a+b); break; case '-': printf("%d", a-b); break; case '': printf("%d", ab); break; case '/': printf("%d", a/b); break; default: printf("Error"); } return 0; }

24. Sum of digits

#include int main()

(j j j ){ c[i][j]=a[i][j]+b[i][j]; } } printf("Result matrix:\n"); for(i=0;i int main(){ int a[10][10],b[10][10],c[10][10]; int i,j,k,r1,c1,r2,c2; printf("Enter r1 c1: "); scanf("%d%d",&r1,&c1); printf("Enter r2 c2: "); scanf("%d%d",&r2,&c2); if(c1!=r2){ printf("Not possible"); return 0; } printf("Enter first matrix:\n"); for(i=0;i

printf("Enter second matrix:\n"); for(i=0;i int main(){ int a[10][10],t[10][10]; int i,j,r,c; i tf("E t d l ")

float a = area(radius); printf("%f",a); return 0;

29.swap Using Call by Value

#include void swap(int x, int y) { int temp; temp = x; x = y; y = temp; printf("Inside function: %d %d", x, y); } int main() { int a = 100, b = 200; printf("Before swap: %d %d\n", a, b); swap(a, b); printf("After swap: %d %d\n", a, b); return 0; }

30.Swap Using Call by Reference (Simple Program)

#include void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a = 100, b = 200; printf("Before swapping: %d %d\n", a, b);

swap(&a, &b); printf("After swapping: %d %d\n", a, b); return 0;

31.Write a program to perform arithmetic operations

using function

#include int add(int a, int b) { return (a + b); } int diff(int a, int b) { return (a - b); } int mul(int a, int b) { return (a * b); } float div(int a, int b) { return (float)a / b; } int mod(int a, int b) { return (a % b); } void main() { int x, y; printf("Enter the values: "); scanf("%d%d", &x, &y); printf("Sum = %d\n", add(x, y)); printf("Difference = %d\n", diff(x, y)); printf("Multiply = %d\n", mul(x, y)); printf("Division = %f\n", div(x, y)); printf("Modulo = %d\n", mod(x, y));

printf("Enter a number: "); scanf("%d", &n); printf("Factorial = %d", fact(n)); }

34.Program to find nCr and nPr

#include int fact(int n) { if(n == 0) return 1; else return n * fact(n - 1); } void main() { int n, r; float nCr, nPr; printf("Enter n and r: "); scanf("%d%d", &n, &r); nPr = (float)fact(n) / fact(n - r); nCr = (float)fact(n) / (fact(r) * fact(n - r)); printf("nCr = %f\n", nCr); printf("nPr = %f", nPr); }

35. Sum of series: 1 + 1/2! + 1/3! +

#include int fact(int n) { if(n == 0) return 1;

else return n * fact(n - 1); } void main() { int n, i; float sum = 0; printf("Enter limit: "); scanf("%d", &n); for(i = 1; i <= n; i++) sum = sum + 1.0 / fact(i); printf("Sum = %f", sum); }

36. Fibonacci Series (Recursive)

#include int fib(int n) { if(n == 1) return 0; else if(n == 2) return 1; else return fib(n-1) + fib(n-2); } void main() { int n, i; printf("Enter limit: "); scanf("%d", &n); for(i = 1; i <= n; i++) printf("%d\t", fib(i)); }

37. Sum of digits (Recursive)

#include