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

C Assignment

Uploaded by

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

C Assignment

Uploaded by

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

Q1: Write a program to print Fibonacci series less than the

given number.
0, 1, 1, 2, 3, 5...
Sol:
#include <stdio.h>

int main() { int a =

0; int b = 1; int

nxtNum = 0;

int n; printf("Enter a positive

number: "); scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", a, b); nxtNum

= a + b;

while (nxtNum <= n) {

printf("%d, ", nxtNum);

a = b; b = nxtNum;

nxtNum = a + b;

}
return 0;

Q2: Write a program to read a number from the keyboard and find
the maximum digit in the given number.

Ex– In 3847, the maximum digit is 8.

# include <stdio.h> int main()

{ int num, large = 0, rem =

0;

//USER INPUT printf("Enter

your input value:"); scanf("%d",

&num);

while (num > 0) {

rem = num % 10;


if (rem > large) {

large = rem;

num = num / 10;

printf("Maximum digit of the given number is: %d\n", large);

return 0;

Q3: Write a program to print the following pattern: A

AB

ABC

ABCD

ABCDE

Sol:
#include <stdio.h> int

main()

{ int i,

j;

for(i=1;i<=5;i++)
{

for(j=1;j<=i;j++)

printf("%c",'A' + j-1);

printf("\n");

return 0;

Q4: Write a program to print the following pattern:

23

456

7 8 9 10

Sol:
#include <stdio.h> int main() { int

rows, i, j, num = 1; printf("Enter the

number of rows: "); scanf("%d",

&rows); for (i = 1; i <= rows; i++) {


for (j = 1; j <= i; ++j) { printf("%d

", num);

++num;

printf("\n");

return 0;

Q5: Write a program to print the following pattern:

*****

****

***

**

Sol:
#include <stdio.h> int main() { int i, j,

rows; printf("Enter the number of

rows: "); scanf("%d", &rows);

for (i = rows; i >= 1; --i) {

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

printf("* ");
}

printf("\n");

return 0;

Q6: Write a program to check if the given number has three


consecutive 5s. If yes, print YES; else print NO.

Example –

Number 152554, Result: NO

Number 145558, Result YES

Sol:

#include <stdio.h> int

main(){ int num; int temp;

int count = 0; printf("Enter

the number: "); scanf("%d",

&num); temp = num; while

(temp > 0)

{
if (temp % 10 == 5)

count++; else

count = 0;

if(count == 3)

break; temp

/= 10;

if (count == 3)

printf("YES");

else printf("NO");

return 0;

Q7:Write a program to convert the decimal number to octal


number using function.

Sol:
#include <stdio.h> int

main()
{

long decNum, r, q,octNum=0;

int octalNumber[100], i = 1, j;

printf("Enter the decimal number: ");

scanf("%ld", &decNum); q=

decNum;

//Storing remainders until number is equal to zero

while (q != 0)

octalNumber[i++] = q % 8;

q = q / 8;

//Converting stored remainder values in corresponding octal number

for (j = i - 1; j > 0; j--)

octNum = octNum*10 + octalNumber[j]; printf("Equivalent octal value

of decimal no %d is: %d ", decNum,octNum); return 0;

}
Q8: Write a program to convert the binary number to
decimalnumber using function.

Sol:
#include <stdio.h> void

main()

int num, binary_val, decimal_val = 0, base = 1, rem;

printf("Enter a binary number \n");

scanf("%d", &num); binary_val =

num; while (num > 0)

rem = num % 10; decimal_val =

decimal_val + rem * base; num = num /

10 ; base = base * 2;

printf("Its decimal equivalent is = %d \n", decimal_val);

You might also like