You are on page 1of 9

ASSIGNMENT 5

Name: Nitesh Kumar Yadav


Reg. No.: 20195103
Group: B1

1. Pascal Triangle is something where each element (i, j) is the sum of (i-1,j) and (i-1,j-1)
element . Now create the Pascal triangle up to nth row with the help of FOR LOOP.
Value of n will be key board input.
#include<stdio.h>

long factorial(int);

int main()
{
int i, n, c;

printf("Enter the number of rows you wish to see in pascal triangle\n");


scanf("%d",&n);

for (i = 0; i < n; i++)


{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");

for (c = 0 ; c <= i; c++)


printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

printf("\n");
}

return 0;
}

long factorial(int n)
{
int c;
long result = 1;

for (c = 1; c <= n; c++)


result = result*c;

return result;
}

2. Write a program to print the following pattern:


ASSIGNMENT 5
#include<stdio.h>
int main()
{
int I,j;
char k;

for(i=1;i<=4;i++)
k=’A”;
for(j=1;j<=7;j++)
{
if(j<=5-i || j>=3)
{
printf(“%c”,k);
j<4?k++:k--;
}
else
printf(“ “);
}
printf(“\n”);
}

3. Write a program to print all the prime number between 1 to n using for loop, break
and continue. The value of n will be input from keyboard. N&gt;=300. Also count the
sum of all those prime numbers and print the sum as well as numbers.

#include <stdio.h>

int main()
{
int i, j, end, isPrime;

printf("Find prime numbers between 1 to : ");


scanf("%d", &end);

printf("All prime numbers between 1 to %d are:\n", end);

for(i=2; i<=end; i++)


{

isPrime = 1;

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


{
ASSIGNMENT 5
if(i%j==0)
{
isPrime = 0;
break;
}
}

if(isPrime==1)
{
printf("%d, ", i);
}
}

return 0;
}

5. Print all the Romanian number from 1 to n. N will be key board input. N&gt;=10.
Romanian letters are I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XII, XIV and XV
and so on.
#include<stdio.h>
int main()
{
int n;
printf("Decimal Roman\n");
printf("numbers numerals\n");
printf("-------------------\n");
for(int i=1; i<=100; i++)
{
n = i;
printf(" %d ",i);
while(n != 0)
{
if (n >= 1000)
{
printf("M");
n -= 1000;
}
else if (n >= 900)
{
printf("CM");
n -= 900;
}
else if (n >= 500)
{
printf("D");
ASSIGNMENT 5
n -= 500;
}
else if (n >= 400)
{
printf("CD");
n -= 400;
}
else if (n >= 100)
{
printf("C");
n -= 100;
}
else if (n >= 90)
{
printf("XC");
n -= 90;
}
else if (n >= 50)
{
printf("L");
n -= 50;
}
else if (n >= 40)
{
printf("XL");
n -= 40;
}
else if (n >= 10)
{
printf("X");
n -= 10;
}
else if (n >= 9)
{
printf("IX");
n -= 9;
}
else if (n >= 5)
{
printf("V");
n -= 5;
}
else if (n >= 4)
{
printf("IV");
n -= 4;
ASSIGNMENT 5
}
else if (n >= 1)
{
printf("I");
n -= 1;
}
}
printf("\n");
}
return 0;
}

6. Write a program to calculate the factorial of a number. Then calculate the sum of n
terms of the following series: 1/1! + 2/2! + 3/3! + ……. + n/n! . N will be key board input.
N&gt;=10. Factorial of
n= n*(n-1)*(n-2)*(n-3)* *1

#include<stdio.h>

int main()
{
int c, n, f = 1;

printf("Enter a number to calculate its factorial\n");


scanf("%d", &n);
for (c = 1; c <= n; c++)
f = f * c;

printf("Factorial of %d = %d\n", n, f);

return 0;
}

#include<stdio.h>

int main()
{
int i,N,sum;

printf("Enter the value of N: ");


scanf("%d",&N);

sum=0;
ASSIGNMENT 5
for(i=1;i<=N;i++)
sum= sum+ i;

printf("Sum of the series is: %d\n",sum);

return 0;
}

7. Write a program to print all the ASCII values and their equivalent characters using
while loop. The ASCII values vary from 0 to 255. Then also fill the entire screen with
the smiling face. The ASCII value of smiling face 1.

#include <stdio.h>

int main()
{
int i;

/* Print ASCII values from 0 to 255 */


for(i=0; i<=255; i++)
{
printf("ASCII value of character %c = %d\n", i, i);
}

return 0;
}

8. Write a program to print all the Armstrong numbers between 1 and 500. If the sum of
cubes of each number is equal to the number itself, then the number is called an
Armstrong number. For example, 153= 1^3 + 5^3+ 3^3.

#include < stdio.h >

int main()
{
int num, count = 1, rem, sum;

while(count <= 500)


{
num = count;
sum = 0;

while(num)
ASSIGNMENT 5
{
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}

if(count == sum)
{
printf("%d is a Armstrong number\n", count);
}

count++;
}

return 0;
}

9. Write a program to find the OCTAL, Hexadecimal and Binary forms of given
decimal number. For example, Octal, Hexadecimal, Binary of Decimal number 10
are A, 12, 1010. Decimal number should key board input.

#include<stdio.h>
void convert_to_x_base(int, int);

int main(void)
{
int num, choice, base;

while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Decimal to hexadecimal. \n");
printf("4. Exit. \n");

printf("\nEnter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1:
base = 2;
break;
ASSIGNMENT 5
case 2:
base = 8;
break;
case 3:
base = 16;
break;
case 4:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}

printf("Enter a number: ");


scanf("%d", &num);

printf("Result = ");

convert_to_x_base(num, base);

printf("\n\n");
}

return 0;
}

void convert_to_x_base(int num, int base)


{
int rem;

if (num == 0)
{
return;
}

else
{
rem = num % base;
convert_to_x_base(num/base, base);
if(base == 16 && rem >= 10)
{
printf("%c", rem+55);
}
ASSIGNMENT 5
else
{
printf("%d", rem);
}
}

10. The natural logarithm can be approximated by the following series.


(X-1)/x + 1/2(x-1/x) 2 + ½(x-1/x) 3 +……… + ½(x-1/x) n If x, n is input through key
board, write a program to calculate sum of first n terms of this series.

include<stdio.h>
void main()
{
int i, j;
float sum = 0.0f;
float power;
float x;
printf("enter x for sum up to 7th term: ");
scanf("%f", &x);
for (i = 1; i <= 7; i++)
{

power = 1.0f;
for (j = 0; j < i; j++)
{
power = power * ((x - 1.0f) / x);
}
sum += (1.0f / i) * power;
}
//sum = sum + (float) (x - 1.0f) / x;
printf("ln(%f) = \n%f\n%lf\n", x, sum, log(x));
}

You might also like