D0.docx
D0.docx
U.no : UEC2023156
#Practice program in C:
1. If number is prime or not
2. Calc area of circle
3. Find square root of a number
4. Calc sum of first 50 prime numbers
5. Factorial of n
6. Generate Fibonacci series
7. Swap two variables without using temporary variable
8. Print the patterns
9. Calculator.
10. Find count of numbers
11. Check palindrome
Q.1 to Q.7
#include<stdio.h>
#include<math.h>
#include <stdbool.h>
bool is_prime(int n){
if (n%2==0){
return false;
}
else{
return true;
}
}
int factorial(int n){
if (n==1|| n==0){
return 1;
}
return n*factorial(n-1);
}
void printFibonacci(int n) {
int a = 0, b = 1, c;
if (n < 1) {
return;
}
printf("%d", a);
printf("\n");
}
int main(){
//Print Even or Odd
printf("even or odd: \n");
int a;
printf("Enter a number:");
scanf("%d",&a);
if(a%2==0){
printf("Even\n");
}else{
printf("Odd\n");
}
}
printf("Sum= %d\n",sum);
//Fibbo Series
int factorial_of_n = factorial(n);
printf("Factorial = %d",factorial_of_n);
printf("Fibbonacci Series\n")
int n1;
printf("Enter a number: ")
scanf("%d",&n1)
printFibonacci(n1)
return 0;
}
OUTPUT
even or odd:
Enter a number:458
Even
Prime or not
Enter a number: 954
Not Prime
Calculate area of circle
Enter the radius: 6.4
Area = 128.61
Find square root
Enter a number: 25
Square Root = 5.00
Sum of first 50 prime numbers
Sum= 5355
Find factorial of:
Enter a number:
8
Factorial = 40320
Fibbonacci Series
Enter a number:
7
0, 1, 1, 2, 3, 5, 8
int main(){
for(int i=0; i<5; i++){
for(int j =1; j<=i+1; j++){
printf("%d\t",j);
}
printf("\n");
}
return 0;
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#printing pattern 2
#include<stdio.h>
int main(){
int k=1;
for(int i=0; i<4; i++){
for(int j =1; j<=i+1; j++){
printf("%d\t",k++);
}
printf("\n");
}
return 0;
}
1
2 3
4 5 6
7 8 9 10
#printing pattern3
#include<stdio.h>
int main(){
char k = 'A';
for(int i=0; i<4; i++){
for(int j =1; j<=i+1; j++){
printf("%c\t",k++);
}
printf("\n");
}
return 0;
}
A
B C
D E F
G H I J
#printing pattern4
#include<stdio.h>
int main(){
int n = 3;
for(int i=0; i<3; i++){
for(int spaces =0;spaces< n-i; spaces++){
printf(" ");
}
for(int j=0; j<=i;j++){
printf("* ");
}
printf("\n");
}
return 0;
}
*
* *
* * *
Q. 9
#include<stdio.h>
#include<stdlib.h>
int main(){
//Calculator
scanf("%d", &a);
printf("\n");
scanf("%d", &b);
while(1){
scanf("%d", &choice);
switch(choice){
case 1:{
break;
case 2:{
break;
case 3:{
float mul = a*b;
break;
case 4:{
break;
case 5:{
printf("Exiting program...\n");
exit(0);
default:{
printf("Invalid choice\n");
return 0;
}
Enter first number: 56
#include<stdlib.h>
int main(){
scanf("%d", &number);
printf("\n");
if(number == 0){
count = 1;
while(number != 0){
number = number/10;
count++;
return 0;
#include <stdbool.h>
int main(){
//Palindrome
int n;
scanf("%d", &n);
scanf("%s",word);
rev_word[i] = word[n-i-1];
if(rev_word[i] == word[i]){
continue;
match = false;
}
if(match == true){
printf("Is a Palindrome\n");
else{
return 0;