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

Write A C Program To Check Whether A Number Is Prime Number or Not. While Fun

The document contains C program code snippets to check if a number is prime, count the digits in a number, find the first and last digit of a number, find the sum of the first and last digit, swap the first and last digit, calculate the sum and product of digits, and print the number in reverse order. The programs take a number as input, perform the specified operation using loops and modulus operator, and print the output.

Uploaded by

pradip
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Write A C Program To Check Whether A Number Is Prime Number or Not. While Fun

The document contains C program code snippets to check if a number is prime, count the digits in a number, find the first and last digit of a number, find the sum of the first and last digit, swap the first and last digit, calculate the sum and product of digits, and print the number in reverse order. The programs take a number as input, perform the specified operation using loops and modulus operator, and print the output.

Uploaded by

pradip
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

14. Write a C program to check whether a number is Prime number or not.

WHILE Fun:
#include <stdlib.h>
int fun(int n){
int i;
i=2;
while(i<=n/2){
if(n%2==0){
break;}
i++;
}
if(i>=n/2){
printf("%d is prime number", n);
}
else{
printf("%d is not prime number", n);
}
return 0;
}
int main(){
int x;
printf("Enter value of N:");
scanf("%d",&x);
fun(x);
return 0;
}

For function:

1
#include <stdlib.h>

int fun(int n){


int i;
for(i=2;i<=n;i++){
if(n%i==0){
break;}
}
if(i>=n/2){
printf("%d is prime number", n);
}
else{
printf("%d is't a prime number", n);}
return 0;
}
int main(){
int x;
printf("Enter value of N:");
scanf("%d",&x);
fun(x);
return 0;
}

15. Write a C program to count number of digits in a number.

2
#include <stdio.h>
#include <stdlib.h>

int fun(int n){


int a[20]; int i;
i=0;
while(n>0){
a[++i]=n%10;
n=n/10;
}
printf("Number of digit %d", i);
return 0;
}
int main(){
int x;
printf("Enter a number:");
scanf("%d",&x);
fun(x);
return 0;
}

16. Write a C program to find first and last digit of a number.

#include <stdio.h>

3
#include <stdlib.h>

int fun(int n){


int a[20]; int i;
i=0;
while(n>0){
a[++i]=n%10;
n=n/10;
}
printf("First number is %d and last number is %d", a[i],a[1]);
return 0;
}
int main(){
int x;
printf("Enter a number:");
scanf("%d",&x);
fun(x);
return 0;
}

17. Write a C program to find sum of first and last digit of a number.
#include <stdio.h>
#include <stdlib.h>

4
int fun(int n){
int a[20]; int i;
i=0;
while(n>0){
a[++i]=n%10;
n=n/10;
}
printf("1st digit is %d & last digit %d",a[i],a[1]);
printf("\n\nsum %d", a[i]+a[1]);
return 0;
}
int main(){
int x;
printf("Enter a nimber:");
scanf("%d",&x);
fun(x);
return 0;
}

18. Write a C program to swap first and last digits of a number.

#include <stdio.h>
#include <stdlib.h>

5
int fun(int n){
int a[20]; int i;
i=0;
while(n>0){
a[++i]=n%10;
n=n/10;
}
printf(" swap 1st digit %d & last digit %d", a[1],a[i]);
return 0;
}
int main(){
int x;
printf("Eater a digit:");
scanf("%d",&x);
fun(x);
return 0;
}

19 Write a C program to calculate sum of digits of a number.


#include <stdlib.h>
int fun(int n){
int a[20]; int i; int s;

6
i=0; s=0;
while(n>0){
a[++i]=n%10;
n=n/10;
s=s+a[i];
}
printf("Sum of number: %d", s);
return 0;
}
int main(){
int x;
printf("Eater a digit:");
scanf("%d",&x);
fun(x);
return 0;
}

20. Write a C program to calculate product of digits of a number.


#include <stdio.h>
#include <stdlib.h>

int fun(int n){

7
int a[20]; int i; int s;
i=0; s=1;
while(n>0){
a[++i]=n%10;
n=n/10;
s=s*a[i];
}
printf("Product of number: %d", s);
return 0;
}
int main(){
int x;
printf("Eater a digit:");
scanf("%d",&x);
fun(x);
return 0;
}

21. Write a C program to enter a number and print its reverse.


#include <stdio.h>
#include <stdlib.h>

int fun(int n){

8
int a[20]; int i;
i=0;
while(n>0){
a[++i]=n%10;
n=n/10;
}
int j;
for(j=1;j<=i;j++){
printf("%d", a[j]);
}
return 0;
}
int main(){
int x;
printf("Enter a number:");
scanf("%d",&x);
fun(x);
return 0;
}

You might also like