Assignment 5: Name: Nitesh Kumar Yadav Reg. No.: 20195103 Group: B1
Assignment 5: Name: Nitesh Kumar Yadav Reg. No.: 20195103 Group: B1
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("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
return result;
}
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>=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;
isPrime = 1;
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>=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>=10. Factorial of
n= n*(n-1)*(n-2)*(n-3)* *1
#include<stdio.h>
int main()
{
int c, n, f = 1;
return 0;
}
#include<stdio.h>
int main()
{
int i,N,sum;
sum=0;
ASSIGNMENT 5
for(i=1;i<=N;i++)
sum= sum+ i;
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;
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.
int main()
{
int num, count = 1, rem, sum;
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");
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("Result = ");
convert_to_x_base(num, base);
printf("\n\n");
}
return 0;
}
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);
}
}
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));
}