0% found this document useful (0 votes)
46 views12 pages

Ex No: 7a Date: Print All Combinations of A Four Digit Number

The document contains 4 examples of C programs related to prime numbers and series calculations. The first program prints all combinations of a 4-digit number using nested loops. The second generates the sine series for a given value of x and number of terms. The third checks if a given number is prime by testing for divisibility. The fourth generates all prime numbers up to a given limit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views12 pages

Ex No: 7a Date: Print All Combinations of A Four Digit Number

The document contains 4 examples of C programs related to prime numbers and series calculations. The first program prints all combinations of a 4-digit number using nested loops. The second generates the sine series for a given value of x and number of terms. The third checks if a given number is prime by testing for divisibility. The fourth generates all prime numbers up to a given limit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Ex no: 7a

Date:
PRINT ALL COMBINATIONS OF A FOUR DIGIT NUMBER

Aim: To print all combinations of a 4-digit number.


Algorithm:
1) Start the program.
2) Enter the values of a,b,c and d.
3) Use nested loops and find all possible ways that the four digits can be ordered.
4) To get the permutations, remove those in which a digit is found more than once.
5) Print all the combinations.
6) Stop.

Program:
#include<stdio.h>
int main()
{
int i1=0,i2=0,i3=0,n;
printf("\nenter a value for n");
scanf("%d",&n);
for(i1=1;i1<=n;i1++)
{
for(i2=1;i2<=n;i2++)
{
for(i3=1;i3<=n;i3++)
{
if(i1!=i2&&i2!=i3&&i1!=i3)
{
printf("%d%d%d\n",i1,i2,i3);
}
}
}
}
return 0;
}

Output :
Enter the values: 1 2 3 4
Output:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

RESULT:
The program has been sucessfuly excecuted

Ex no: 7b
Date:
Sine series

Aim: To write a C program to generate the Sine series: S=x - x3 /3! + x5 /5! - x7 /7! +
Xn /n!
Algorithm :
1.Get the values of x and n where n decides how long the series should continue.
2.Set the initial conditions S=x, term=x , i=1
3.To get the terms with alternate sign repeatedly execute sign=-sign
4.Till the desired no of terms are generated repeat the following steps
a. Identify the current I th term
b. Generate current term from its predecessor.
i*(i-1)

current ith term = (previous term)*x2 /

c. Add current term with the appropriate sign to the accumulated sum for the sine
function
i=i+2 term=-term* x2 / i*(i-1) S=S+ term

Program:
#include<stdio.h>
main()
{
int i=1,n;
float sum, term, x;
printf(enter the value of x in radians\t);
scanf(%f,&x);
printf(\n enter the prower of end term\t);
scanf(%d,&n);
sum=0;
term=x;
I=1;
while(i<=n)
{
sum=sum+term;
term=(term*x*x-1)/(i+1)*(i+2);
I=i+2;
}
printf(sin of %42f is %f,x,sum);
}

Sine Series
Input:.
Enter the values of x and n
1
2
Output
0.8333333.
RESULT:
The program has been sucessfuly excecuted.

Ex no: 7c
Date:
Prime Number Checking

Aim: To check the given number is prime.


Algorithm:
1. Start
2. Read a number n
3. Declare a variable d =2 and count =0
4. Make a loop from d to n
5. Check d is less than n, if yes divide n%d and check equal to zero
6. If step 5 condition is true then increment the count value
7. Increment d value by one then go to step 5
8. Check count value is equal to zero, if yes display number is prime
9. If step 8 is false, then display number is not prime.
10. stop

Program:
#include<stdio.h>
int main()
{
int n, d=2, count=0;
int mul=0;
printf("Enter the Number n:");
scanf("%d",&n);
for(;d<n;d++)
{

if(d<n)
{ mul=n%d;
if(mul==0)
count++;
}

}
if(count==0)
{
printf("Number is Prime");
}
else
{
printf("Not Prime");
}
return 0;}

Output:
Enter a number : 11
11 is a prime number .
Enter a number : 10
10 is not a prime number.

RESULT: The program has been successfully excecuted.

Ex no: 7d
Date:
Prime Number Generation

Aim: To generate a prime number series upto the given limit.


Algorithm:
1.Start
2. Read the value of n.
3. To print the value 2 on screen.
4. Make a loop
Declare variable i=3
Increment i by 1 upto n
5. Using another loop
Declare variable j=2
From j=2 to j<i; divide i by j
If i % j = = 0 then print the number else print ( )
6. Stop

PROGRAM:
#include<stdio.h>
int main()
{
int n;
int i, j;
printf("Enter the Number:");
scanf("%d",&n);
printf("2");
for(i=3;i<n;i++)
{

for(j=2;j<i;j++)
{

if(i%j==0)
{
printf("%d",i);
}
else
{
printf(" ");
}

}
}
return 0;}

Output:
Enter the Limit value : 10
2
3
5
7
RESULT:
The program has been successfully excecuted.

You might also like