25. WRITE A C PROGRAM FIND SUM OF EVEN NUMBERS
/* C program to print sum of allEVEN numbers between 1 to n*/#include<stdio.h>
#include<conio.h>
int main()
{
int i, n, sum=0;
/* Reads range from user */
printf("Enter any number: ");
scanf("%d", &n);
for(i=2; i<=n; i+=2)
{
/* Adds each subsequentEVEN numbers to sum */
sum += i;
}
printf("\nSum of all even number between 1 to %d = %d", n, sum);
return 0;
}
output
Enter any number: 10
Sum of all even number between 1 to 10 = 30
26. WRITE A C PROGRAM TO FIND SUM OF 1 TO N ODD NUMBERS USING WHILE
/*C program to find Sum of 1 to n odd numbers using while*/#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0,n;
clrscr();
printf("enter the number ");
scanf("%d",&n);
while(i<=n)
{
if(i%2!=0)
{
sum=sum+i;
}
i=i+1;
}
printf("sum of odd number=%d",sum);
getch();
}
output
enter the number10
sum of odd numbers = 25
/*program to Check the given number is armstrong number or not using c program*/
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
output
Sample output:
Enter a number: 153
153 is an Armstrong number
27. WRITE A C PROGRAM TO CHECK THE GIVEN NUMBER IS ARMSTRONG NUMBER OR NOT
/*program to Check the given number is armstrong number or not using c program*/#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
output
Sample output:
Enter a number: 153
153 is an Armstrong number
28. WRITE A C PROGRAM TO FIND ARMSTRONG NUMBERS BETWEEN TWO INTEGERS
/*program to find Armstrong Numbers Between Two Integers*/#include <stdio.h>
#include <math.h>
int main()
{
int low, high, i, temp1, temp2, remainder, n = 0, result = 0;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d an %d are: ", low, high);
for(i = low + 1; i < high; ++i)
{
temp2 = i;
temp1 = i;
// number of digits calculation
while (temp1 != 0)
{
temp1 /= 10;
++n;
}
// result contains sum of nth power of its digits
while (temp2 != 0)
{
remainder = temp2 % 10;
result += pow(remainder, n);
temp2 /= 10;
}
// checks if number i is equal to the sum of nth power of its digits
if (result == i) {
printf("%d ", i);
}
// resetting the values to check Armstrong number for next iteration
n = 0;
result = 0;
}
return 0;
}
output
Enter two numbers(intervals): 999
99999
Armstrong numbers between 999 an 99999 are: 1634 8208 9474 54748 92727 93084
29. WRITE A C PROGRAM PROGRAM TO CHECK PALINDROME
/*C program Program to Check Palindrome*/#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}
output
Enter an integer: 1001
1001 is a palindrome.
30. WRITE A C PROGRAM FIND PALINDROME NUMBERS BELOW RANGE ‘N’ AND PRINT THEM
/*program to print palindrome numbers below range ‘n’*/#include<stdio.h>
#include<conio.h>
int main()
{
int n1,r,sum=0,n=1,range;
clrscr();
printf(“enter the range\n”);
scanf(“%d”,&range);
do
{
sum=0;
n=n1;
do
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
while(n>0)
if(sum==n1)
printf(“%d\n”,n1);
n1++;
}
while(n1<=range);
getch();
return 0;
}
output
enter range
100
1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
31. WRITE A C PROGRAM FOE SUM OF FIRST N NATURAL NUMBERS USING WHILE LOOP
/*c program foe sum of first n natural numbers using while loop*/#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum;
clrscr();
printf("Enter a number \n");
scanf("%d",&n);
i=1;
sum=0;
while(i<=n)
{
sum+=i;
i++;
}
printf("sum =%d\n",sum);
getch();
}
output
Enter a number
10
sum=55
32. WRITE A C PROGRAM TO CALCULATE FACTORIAL VALUE USING DO WHILE
/* calculate factorial value using do while */#include<stdio.h>
#include<conio.h>
void main()
{
long int i,n,fact=1; /*variable declaration */
clrscr();
printf("Enter the value of n \n");
scanf("%ld", &n);
/* do loop start */
i=1;
do
{
fact*=i;
i++;
}
while(i<=n);
printf("Factorial = %ld\n",fact);
getch();
}
output
enter the value of n
6
factorial = 720
33. WRITE A C PROGRAM TO CALCULATE FACTORIAL S FOR 1 TO 10 NUMBERS
/*C program to calculate factorial s for 1 to 10 numbers*/#include <stdio.h>
#include<conio.h>
int main(void)
{
int i, j;
int num;
// Outer loop - We want 10 different calculations
for(i = 1;i <= 10;++i)
{
// Initialize num to 1 every time through the outer loop
num = 1;
// Inner loop - Do the actual factorial calculation
for(j = 1;j <= i;++j)
num *= j;
// Print the result - Once each time through the outer loop
printf("%d\t%d\n", i, num);
}
return 0;
}
output
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
34. WRITE A C PROGRAM FOR SUM OF THE INDIVIDUAL DIGITS I.E, ADDING ALL THE DIGITS OF A NUMBER.
/*C program for Sum of the individual digits i.e, adding all the digits of a number.*/#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int number = 0, digit = 0, sumOfDigits = 0;
clrscr();
printf("Enter any number\n ");
scanf("%d", &number);
while (number != 0)
{
digit = number % 10;
sumOfDigits = sumOfDigits + digit;
number = number / 10;
}
printf ("Sum of individual digits of a given number is %d", sumOfDigits);
getch();
}
output
Enter any number
1234
Sum of individual digits of a given number is 10
35. WRITE A C PROGRAM TO PRINT TO REVERSE OF A NUMBER
/*C program to print to reverse of a number*/#include<stdio.h>
#include<conio.h>
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
output
enter a number to reverse
1234
reverse of entered number = 4321
36. WRITE A C PROGRAM TO PRINT NATURAL NUMBERS USING FOR LOOP
/* C program to print all natural numbers from 1 to n*/#include <stdio.h>
int main()
{
int i, n;
/*
* Reads the value of n from user
*/
printf("Enter any number: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d : \n", n);
/*
* Starts loop counter from 1 (i=1) and goes till n (i<=n)
* And in each repetition prints the value of i
*/
for(i=1; i<=n; i++)
{
printf("%d\n", i);
}
return 0;
}
output
Enter any number: 10
Natural numbers from 1 to 10 :
1
2
3
4
5
6
7
8
9
10
37. WRITE A C PROGRAM TO PRINT 10 TO 1 NUMBERS USING FOR LOOP
/*C program to Print 10 to 1 Numbers Using for Loop*/#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n"); //for new line
// For loop
// (initialize; condition ; increment)
for(n=10;n>=1;n--)
{
printf(" %d\n",n);
}
getch();
}
output
10
9
8
7
6
5
4
3
2
1
38. WRITE A C PROGRAM TO FIND SUM OF EVEN NUMBERS & SUM OF ODD NUMBERS BELOW 1000 & PRINT THEM
/*program to find sum of even and odd numbers below 1000*/#include<stdio.h>
#include<conio,h>
int main()
{
int num;
long int sume=0,sumo=0;
clrscr();
for(num=1;num<1000;num++)
{
if(num%2==0)
{
sume = sume+num;
}
else
{
sumo = sumo+num;
}
}
printf(“sum of even numbers is %d\n”sume);
printf(“sum of odd numbers is %d”,sumo);
getch();
return 0;
}
output:
sum of even numbers is 249500
sum of odd numbers is 250000
39. WRITE A C PROGRAM TO EXPLAIN UNCONDITIONAL STATEMENT BREAK(FOR LOOP)
/*program to explain unconditional statement break in for*/#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,n=10;
clrscr();
for(i=1;i<=n;i++)
{
printf(“%d\n”,i);
if(i==5)
{
break;
}
}
getch();
return 0;
}
output
1
2
3
4
5
40. WRITE A C PROGRAM TO EXPLAIN UNCONDITIONAL STATEMENT ‘CONTINUE’(IN FOR)
/*program to explain unconditional statement continue*/#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
printf(“enter the n value”);
scanf(“%d”,&n)
for(i=1;i<=n;i++)
{
if(i==5)
{
continue;
}
printf(“%d\t”,i);
}
getch();
return 0;
}
output
enter the n value
9
1 2 3 4 5 6 7 8 9
41. WRITE A C PROGRAM TO PRINT ALL PRIME NUMBERS BETWEEN 1 TO N USING FOR LOOP
/*C program to print all prime numbers between 1 to N using for loop*/#include<stdio.h>
#include<conio.h>
int main(){
int N, i, j, isPrime, n;
printf("To print all prime numbers between 1 to N\n");
printf("Enter the value of N\n");
scanf("%d",&N);
/* For every number between 2 to N, check
whether it is prime number or not */
printf("Prime numbers between %d to %d\n", 1, N);
for(i = 2; i <= N; i++){
isPrime = 0;
/* Check whether i is prime or not */
for(j = 2; j <= i/2; j++){
/* Check If any number between 2 to i/2 divides I
completely If yes the i cannot be prime number */
if(i % j == 0){
isPrime = 1;
break;
}
}
if(isPrime==0 && N!= 1)
printf("%d ",i);
}
getch();
return 0;
}
To print all prime numbers between 1 to N
Enter the value of N
50
output
Prime numbers between 1 to 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Comments
Post a Comment