0% found this document useful (0 votes)
4 views6 pages

FOP Problemsheet 2

The document contains a problem sheet with six programming tasks written in C. The tasks include finding the sum of numbers from 1 to N, summing individual digits of a number, checking for palindromes, converting decimal to binary, checking for prime numbers, and displaying a multiplication table. Each task is accompanied by sample code demonstrating how to implement the solution.

Uploaded by

assupatel009
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)
4 views6 pages

FOP Problemsheet 2

The document contains a problem sheet with six programming tasks written in C. The tasks include finding the sum of numbers from 1 to N, summing individual digits of a number, checking for palindromes, converting decimal to binary, checking for prime numbers, and displaying a multiplication table. Each task is accompanied by sample code demonstrating how to implement the solution.

Uploaded by

assupatel009
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

PROBLEMSHEET - 2

1. Write a program to find the sum of 1 to N numbers using [While loop]

#include <stdio.h>
#include<conio.h>
void main()
{
int N, sum = 0, i = 1;

printf("Enter a positive integer N: ");


scanf("%d", &N);

if (N < 1)
{
printf("Please enter a positive integer greater than 0.\n");
return 1;
}

while (i <= N) {
sum += i;
i++;

printf("The sum of numbers from 1 to %d is: %d\n", N, sum);

getch();
}

Page | 1
PROBLEMSHEET - 2

2. Write a program To find the sum of individual digits of a number [eg: 135 then
sum is 1+3+5=9]

#include <stdio.h>
#include<conio.h>
void main()
{

int number, sum = 0, digit;


printf("Enter an integer:
"); scanf("%d", &number);

number = (number < 0) ? -number : number;

while (number > 0) {


digit = number %
10; sum += digit;
number /= 10;
}
printf("The sum of the individual digits is: %d\n", sum);
‘ getch();
}

Page | 2
PROBLEMSHEET - 2

3. Write a program to check whether the entered number is palindrome or not.

#include <stdio.h>
#include<conio.h>
void main()
{
int number, reversedNumber = 0, originalNumber, digit;
printf("Enter an integer: ");
scanf("%d", &number);
originalNumber = number;
number = (number < 0) ? -number : number;

while (number > 0) {


digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
if (originalNumber == reversedNumber)
{
printf("%d is a palindrome.\n", originalNumber);

} else {
printf("%d is not a palindrome.\n", originalNumber);
}
getch();

Page | 3
PROBLEMSHEET - 2

4. Write a program to read a decimal number and convert it into binary number.

#include <stdio.h>
#include<conio.h>
void main()
{
int decimalNumber, binaryNumber[32], i =
0; printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);

if (decimalNumber == 0)

{
printf("Binary representation: 0\n");
getch();
}
while (decimalNumber > 0) {

binaryNumber[i] = decimalNumber %
2; decimalNumber = decimalNumber /
2;
i++;
}

printf("Binary representation: ");


for (int j = i - 1; j >= 0; j--)
{
printf("%d", binaryNumber[j]);

}
printf("\n");
getch();
}

Page | 4
PROBLEMSHEET - 2

5. Write a program to check whether the entered number is prime or not

#include <stdio.h>
#include<conio.h>
void main()
{
int number, i, isPrime = 1;

printf("Enter a positive integer:


"); scanf("%d", &number);
if (number <= 1)
{

isPrime = 0;
} else {

for (i = 2; i * i <= number; i++)


{

if (number % i == 0)
{
isPrime = 0;
break;
}
}
}
if (isPrime)

{
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);

Page | 5
PROBLEMSHEET - 2

getch();
}
6. Write a program to display the table of given no.

#include <stdio.h>
#include<conio.h>
void main()
{
int number, i;

printf("Enter a number to display its multiplication table: ");


scanf("%d", &number);
printf("Multiplication table of %d:\n", number);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", number, i, number * i);

}
getch();
}

Page | 6

You might also like