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

PPSC MID-I ASSIGNMENT PROGRAMS

The document contains a series of C programming assignments focused on problem-solving techniques. Each assignment includes a specific task, such as swapping numbers, calculating interest, finding distances, and checking for prime numbers, along with the corresponding C code. The document serves as a practical guide for students to enhance their programming skills in C.

Uploaded by

buradabalaji66
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)
5 views

PPSC MID-I ASSIGNMENT PROGRAMS

The document contains a series of C programming assignments focused on problem-solving techniques. Each assignment includes a specific task, such as swapping numbers, calculating interest, finding distances, and checking for prime numbers, along with the corresponding C code. The document serves as a practical guide for students to enhance their programming skills in C.

Uploaded by

buradabalaji66
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/ 9

Programming for Problem Solving Using C

Mid-I (Unit-I & II)


Assignment Programs

1. Develop a C program to swap 2 numbers without using a third variable.

Program:

#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}

2. Develop a C program to calculate Simple and Compound Interest.

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float p, t, r, si, ci;
clrscr();
printf("Enter principal amount (p): ");
scanf("%f", &p);
printf("Enter time in year (t): ");
scanf("%f", &t);
printf("Enter rate in percent (r): ");
scanf("%f", &r);

/* Calculating simple interest */


si = (p * t * r)/100.0;

/* Calculating compound interest */


ci = p * (pow(1+r/100, t) - 1);

printf("Simple Interest = %0.3f\n", si);


printf("Compound Interest = %0.3f", ci);
getch();
return(0);
}

3. Develop a C program to calculate Distance between 2 points.

Program:

#include <stdio.h>
#include<math.h>

int main()
{
int x1, x2, y1, y2, dtn;

printf("Enter the First Point Coordinates =\n ");


scanf("%d %d",&x1, &y1);

printf("Enter the Second Point Coordinates = \n");


scanf("%d %d",&x2, &y2);

int x = pow((x2- x1), 2);


int y = pow((y2- y1), 2);

dtn = sqrt(x + y);

printf("\nThe Distance Between Two Points = %d", dtn);


}

4. Build a C Program to accept temperature at Celsius and convert it into


Fahrenheit.

Program:

/**
* C program to convert temperature from degree celsius to fahrenheit
*/

#include <stdio.h>

int main()
{
float celsius, fahrenheit;

/* Input temperature in celsius */


printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

/* celsius to fahrenheit conversion formula */


fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;
}

5. Construct a C Program to check whether a given number is Prime or not.

Program:

#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}

6. Develop a C Program to accept any number and find the sum of digits of
the number

Program:

#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;

printf("Enter an integer\n");
scanf("%d", &n);

t = n;

while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}

printf("Sum of digits of %d = %d\n", n, sum);


return 0;
}

7. Build a Program to accept any number and check whether that number is
Palindrome or not

Program:

#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
8. Develop a C Program to display Fibonacci Series.

Program:

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

9. Build a C Program to accept any number and find out the biggest and
smallest digit in that number

Program:

#include<stdio.h>

int main(){
int max=-1;
int min=10;

int rem, num;

printf("Enter a number : ");


scanf("%d",&num);

while(num > 0) {

//Extracting digits from number


rem = num % 10;

//checking minimum digit in number


if(rem < min) {
min = rem;
}

//checking maximum digit in number


if(rem > max) {
max = rem;
}

num /= 10;
}
printf("Max : %d Min : %d", max, min);

return 0;
}

10.Write a C program to check whether a given number is an Armstrong


number or not.

Program:
#include <stdio.h>

int main()

int num,r,sum=0,temp;

printf("Input a number: ");

scanf("%d",&num);

for(temp=num;num!=0;num=num/10)

r=num % 10;

sum=sum+(r*r*r);

if(sum==temp)

printf("%d is an Armstrong number.\n",temp);

else

printf("%d is not an Armstrong number.\n",temp);

You might also like