0% found this document useful (0 votes)
19 views35 pages

Shravajournl 2

The document contains a series of practical programming exercises in C, covering various topics such as digit extraction, palindrome checking, grading systems, and number properties like prime, perfect, and Armstrong numbers. Each exercise includes a coding solution, input/output specifications, and a brief description of the problem. The exercises utilize concepts like sentinel-controlled repetition, conditionals, and arrays.

Uploaded by

Shravan Ghasti
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)
19 views35 pages

Shravajournl 2

The document contains a series of practical programming exercises in C, covering various topics such as digit extraction, palindrome checking, grading systems, and number properties like prime, perfect, and Armstrong numbers. Each exercise includes a coding solution, input/output specifications, and a brief description of the problem. The exercises utilize concepts like sentinel-controlled repetition, conditionals, and arrays.

Uploaded by

Shravan Ghasti
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/ 35

Practical No :-1

Date:-

Q.1]Extract digits of an integer number (left to right and right to left)?

A]Left To Right
CODING :-

#include<stdio.h>

#include<conio.h>

void extract_digits(int num)

int temp=abs(num);

int digits[10];

int i=0,j;

while(temp>0)

digits[i++]=temp%10;

temp/=10;

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

printf("%d",digits[j]);

printf("\n");

}
void main()

int num;

clrscr();

printf("Enter an integer:");

scanf("%d",&num);

printf("Digits from left to right:");

extract_digits(num);

getch();

OUTPUT :-
B]Right To Left

CODING :-

#include<stdio.h>

#include<conio.h>

void extract_digits_right_to_left(int num)

while(num>0){

int digit=num%10;

printf("%d",digit);

num=num/10;

printf("\n");

void main(){

int num;

printf("enter number:");

scanf("%d",&num);

printf("\n extracted digits from right to left:");

extract_digits_right_to_left(num);

getch();

OUTPUT :-
Practical No :-2

Date:-

Q.1]Given a sequence of digits from the number composed of the digits. Use
sentinel controlled repetition to read the digits followed by -1. For example, for
the input 2 7 32 9 -1 the output number is 27329?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int digit;

long long number=0;

clrscr();

printf("Enter a sequence of digits(-1 to terminate):");

while(1)

scanf("%d",&digit);

if(digit==-1)

break;

number=number*10+digit;

printf("The number composed of the digit is:%lld\n",number);


getch();

OUTPUT :-
Practical No :- 3

Date:-

Q.1]Check if a given positive number is a palindrome or not?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int n,r,sum=0,temp;

clrscr();

printf("enter the number :");

scanf("%d",&n);

if(n>0)

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");

else

printf("given number is not positive");

getch();

OUTPUT :-
Practical No :- 4

Date:-

Q.1]Compute character grade from the marks(0 ≤marks ≤100) of a subject.


Grading Scheme : 80-100:A, 60-79:B, 50-59:C, 40-49:D, 0:39:F? Solve this using
both else if ladder and switch case?

A]Using else-if ladder:

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int m1,m2,m3,m4,m5,total;

float per;

clrscr();

printf("Enter five subject marks:");

scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);

total=m1+m2+m3+m4+m5;

printf("\n Total marks often= %d",total);

per=total/5;

printf("\n Percentage=%f",per);

if(per>=80)

printf("\n A grade");

}
else if(per<=79 && per>=60)

printf("\n B grade");

else if(per<=59 && per>=50)

printf("\n C grade");

else if(per<=49 && per>=40)

printf("\n D grade");

else

printf("\n Fail");

getch();

OUTPUT :-
B]Using Switch case:

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int marks;

char grade;

clrscr();

printf("Enter the marks(0-100):");

scanf("%d",&marks);

if(marks<0 || marks>100)

printf("Invalid marks.Please enter a value between 0 and 100\n");

else

int range=(marks/10)*10;

switch (range)

case 80:

case 90:

case 100:

grade='A';
break;

case 60:

case 70:

grade='B';

break;

case 50:

grade='C';

break;

case 40:

grade='D';

break;

default:

grade='F';

break;

printf("Grade: %c\n",grade);

getch();

OUTPUT :-
Practical No :-5

Date:-

Q.1]Compute the sum of a sequence of number entered using sentinel


controlled repetition?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int num;

int sum=0;

clrscr();

printf("Enter a sequence of numbers(-1 to terminate):\n");

while(1)

printf("Enter number:");

scanf("%d",&num);

if(num==-1)

break;

sum+=num;

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


getch();

OUTPUT :-
Practical No :-6

Date:-

Q.1]Check if a given positive integer number is a prime number or not?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int n,i,m=0,flag=0;

clrscr();

printf("enter number:");

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");

getch();

OUTPUT :-
Practical No :-7

Date:-

Q.1]Compute prime factors of a positive integer number?

CODING :-
#include<stdio.h>

#include<conio.h>

void primefactor(int n)

int i;

while(n%2==0)

printf("%d",2);

n=n/2;

for(i=3;i*i<=n;i=i+2)

while(n%i==0)

printf(" %d ",i);

n=n/i;

if(n>2)
{

printf("%d",n);

void main()

int n;

clrscr();

printf("enter number:");

scanf("%d",&n);

if(n>0)

primefactor(n);

else

printf("given number is negative");

getch();

OUTPUT :-
Practical No :-8

Date:-

Q.1]Check if two positive integer numbers are amicable number or not?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int n,n1,sum=0,sum1=0,i;

printf("enter first number:");

scanf("%d",&n);

printf("enter second number:");

scanf("%d",&n1);

if(n>0 && n1>0)

for(i=1;i<=n/2;i++)

if(n%i==0)

sum=sum+i;

for(i=1;i<=n1/2;i++)
{

if(n1%i==0)

sum1=sum1+i;

if(sum==n1 && sum1==n)

printf("%d and %d are amicable numbers",n,n1);

else

printf("%d and %d are not amicable numbers",n,n1);

else

printf("enter only positive numbers");

getch();

OUTPUT :-
Practical No :-9

Date:-

Q.1]Check if given positive integer number is a perfect number or not?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int n,sum=0,i;

clrscr();

printf("enter any number:");

scanf("%d",&n);

if(n>0)

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

if(n%i==0)

sum=sum+i;

if(sum==n)

{
printf("%d is a perfect number",n);

else

printf("%d is not a perfect number",n);

else

printf("given number is negative");

getch();

OUTPUT :-
Practical No :-10

Date:-

Q.1]Check if a given positive integer number Armstrong number or not?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int n,r,sum=0,temp;

clrscr();

printf("enter number:");

scanf("%d",&n);

if(n>0)

temp=n;

while(n>0)

r=n%10;

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

n=n/10;

if(temp==sum)

{
printf("armstrong number");

else

printf("not a armstrong number");

else

printf("given number is negative");

getch();

OUTPUT :-
Practical No :-11

Date:-

Q.1]Converting a positive integer number(n>0) from one base (inputBase) to


another base(outputBase) (2<= input Base, outputBase<=10).Input number
should be validated before converting to make sure the number uses only digits
allowed in the input base?

OUTPUT :-
#include<stdio.h>

#include<conio.h>

void main()

int decimal=0,binary,base=1,rem,num;

clrscr();

printf("enter the binary number:");

scanf("%d",&binary);

num=binary;

while(binary !=0)

rem=binary%10;

decimal= decimal+ rem*base;

binary=binary/10;

base=base*2;

printf("decimal equivalent of the binary number %d is


%d",num,decimal);
getch();

OUTPUT :-
Practical No :-12

Date:-

Q.1]Write a program to display a number in text form .For example if the


number is 5432 the output should be “FIVE FOUR THREE TWO”?

OUTPUT :-
#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int n,num=0,digits;

clrscr();

printf("Enter any number to print in words:");

scanf("%d",&n);

digits=(int) log10(n);

while(n !=0)

num=(num*10)+(n%10);

n /= 10;

digits= digits-((int) log10(num));

while(num !=0)

switch(num%10)
{

case 0:

printf("Zero");

break;

case 1:

printf("One");

break;

case 2:

printf("Two");

break;

case 3:

printf("Three");

break;

case 4:

printf("Four");

break;

case 5:

printf("Five");

break;

case 6:

printf("six");

break;

case 7:

printf("seven");
break;

case 8:

printf("eight");

break;

case 9:

printf("nine");

break;

num /= 10;

while(digits)

printf("Zero");

digits--;

getch();

OUTPUT :-
Practical No :-13

Date:-
Q.1]Using the grading scheme described in the question 4(unit III), Compute how many
students awarded each grade and display the frequency as a bar chart(horizontal) using
single”*” for each student. Use sentinel controlled repetition(-1 a sentinel value) in reading
the students marks. Use else-if ladder/switch case to compute the grade and the
corresponding frequency.

Sample bar chart when the class has 7-A, 10-B, 3-C, 7-D and 1-F grades.

A : *******

B : **********

C : ***

D : *******

F:*

CODING :-

#include<stdio.h>

#include<conio.h>

void main()

int marks,i;

int frequency_A= 0,frequency_B= 0,frequency_C= 0,frequency_D= 0,frequency_F= 0;

printf("Enter the marks(-1 to terminate):");

while(1)

scanf("%d",&marks);

if(marks==-1){

break;
}

if(marks>=80){

frequency_A++;

else if(marks>=60){

frequency_B++;

else if(marks>=50){

frequency_C++;

else if(marks>=40){

frequency_D++;

else{

frequency_F++;

printf("A:");

for(i=0;i<frequency_A;i++){

printf("*");

printf("\n");

printf("B:");

for(i=0;i<frequency_B;i++){
printf("*");

printf("\n");

printf("C:");

for(i=0;i<frequency_C;i++){

printf("*");

printf("\n");

printf("D:");

for(i=0;i<frequency_D;i++){

printf("*");

printf("\n");

printf("F:");

for(i=0;i<frequency_F;i++){

printf("*");

printf("\n");

getch();

OUTPUT :-
Practical No :-14

Date:-

Q.1]Compute maximum, minimum, sum and average of a sequence of number


which are read using sentinel controlled repetition using only few variables?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int a[100],i,sum=0,max,min,n;

float avg;

printf("enter size of array:");

scanf("%d",&n);

printf("\n enter array element:\n");

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

scanf("%d",&a[i]);

sum=sum+a[i];

avg=sum/n;

max=a[i];

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

if(a[i]>max)
{

max=a[i];

printf("greatest element of array is= %d\n",max);

printf("sum of array element = %d\n",sum);

printf("average of element = %f\n",avg);

min=a[0];

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

if(a[i]<min)

min=a[i];

printf("lowest element of array is= %d",min);

getch();

OUTPUT :-
Practical No :-15

Date:-

Q.1]Compute body mass index,BMI=WeightInKgs/(HeightInMeters * HeightIn


Meters), Both weight and height values are positive real numbers. Your
program should display BMI value followed by whether the person is
Underweight, Normal, Overweight or Obese using the below ranges :
BMI values

Underweight : less than 18.5

Normal : >=18.5 and <25

Overweight : >=25 and <30

Obese : >=30

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

double weight,height,bmi;

clrscr();

printf("Enter weight in kilograms:");

scanf("%lf",&weight);

printf("Enter height in meters:");

scanf("%lf",&height);

bmi=weight/(height*height);

printf("BMI:%.2f\n",bmi);
if(bmi<18.5)

printf("Undeweight\n");

else if(bmi>=18.5 && bmi<25)

printf("Normal\n");

else if(bmi>=25 && bmi<30)

printf("Overweight\n");

else

printf("Obese\n");

getch();

OUTPUT :-

You might also like