0% found this document useful (0 votes)
28 views14 pages

Assignment 1

The document contains 11 questions that provide C code examples to solve various programming problems. Each question includes the code to solve a specific problem such as: printing a name and roll number, swapping two values, converting temperatures between Fahrenheit and Celsius, calculating gross salary, finding the maximum of three numbers, determining if a number is prime, and sorting an array of numbers. For each question, the C code provided implements the algorithm to compute the solution to the programming problem posed in the question.

Uploaded by

meghnabagchi23
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)
28 views14 pages

Assignment 1

The document contains 11 questions that provide C code examples to solve various programming problems. Each question includes the code to solve a specific problem such as: printing a name and roll number, swapping two values, converting temperatures between Fahrenheit and Celsius, calculating gross salary, finding the maximum of three numbers, determining if a number is prime, and sorting an array of numbers. For each question, the C code provided implements the algorithm to compute the solution to the programming problem posed in the question.

Uploaded by

meghnabagchi23
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/ 14

QUESTION 1

Write a C program to print your name and roll number on screen.

#include<stdio.h>
#include<conio.h>
int main()
{
printf("\n Name \t\t Meghna Bagchi \n Roll number\t 002310701016");
}
QUESTION 2
2.Write a C program that reads two values from the keyboard ,
swaps their values and print out the results.
#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
printf("\n Enter first number:");
scanf("%d",& num1);
printf("\n Enter second number:");
scanf("%d",& num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\n The first number is %d",num1);
printf("\n The second number is %d",num2);
return 0;
}
QUESTION 3
Temperature of a city in Fahrenheit degrees is input through the
keyboard. Write a program to convert this temperature into
Centigrade degrees.

#include<stdio.h>
#include<conio.h>
int main()
{
float far,cel;
printf("\n Enter temperature in Fahrenheit:");
scanf("%f",& far);
cel=(5.0/9.0)*(far-32.00);
printf("\n Temperature in Celsius %0.2f",cel);
return 0;
}
QUESTION 4
Write a C program which accepts basic salary as input and prints
the gross salary, which is the sum of the basic, dearness allowance
(60% of basic salary),and house rent allowance(15% of basic
salary)

#include<stdio.h>

#include<conio.h>
int main()
{
float bs,da,hra,gross;
printf(" \n Enter basic salary:");
scanf("%f",&bs);
da=0.60*bs;
hra=0.15*bs;
gross=bs+da+hra;
printf("\n Dearness allowance: %0.2f \n House rent allowance: %0.2f",da,hra);
printf("\n Gross salary: %0.2f",gross);
return 0;

}
QUESTION 5
Write a C program to determine the maximum of three different
numbers. Also draw the corresponding flowchart.

#include<stdio.h>

#include<conio.h>
int main()
{
int n1,n2,n3;
printf("Enter 3 numbers=\n");
scanf("%d\n%d\n%d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
{
printf("%d is the largest",n1);
}
else
printf("%d is the largest",n3);
}
else
{
if(n2>n3)
{
printf("%d is the largest",n2);
}
else
printf("%d is the largest",n3);
}
return 0;

}
QUESTION 6
Write a C program to determine sum of individual digits of a given
integer. Also draw the corresponding flowchart.

#include<stdio.h>
#include<conio.h>
int main()
{
int num,sum=0;
printf("Enter a number=");
scanf("%d",&num);
while(num>0)
{

sum=sum+(num%10);
num=num/10;
}
printf("\n Sum of digits is %d",sum);
return 0;
}
QUESTION 7

Write a C program to print the reverse of number read as input.


Also draw the corresponding flowchart.

#include<stdio.h>
#include<conio.h>
int main()
{
int num, rev=0;
printf("Enter a number=\n");
scanf("%d",&num);
while(num>0)
{
rev=(rev*10)+(num%10);
num=num/10;
}
printf("\n Reverse number is %d",rev);
}
QUESTION 8

Write a C program to determine if a number is prime or not. Also


draw the corresponding flowchart.

#include<stdio.h>
#include<conio.h>
int main()
{
int num,n=0;
printf("Enter a number= \n");
scanf("%d",&num);
for(int i=1;i<=num;i++)
{
if(num%i==0)
n++;
}
if(n==2)
printf("\n The number is prime");
else
printf("\n The number is not prime");
}
QUESTION 9

Write an algorithm to generate the first 100 prime numbers. Also


draw the corresponding flowchart.

#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,n=0,c=0,j;
for(i=1;n<100;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
{
n++;
printf("%d\t",i);
}

}
}
QUESTION 10
Write a C program and a flowchart to input three numbers in the
variables a,b,c and hence find the roots of the quadratic equation
ax2+bx+c=0.

#include<stdio.h>

#include<math.h>

int main()

float r1,r2;

int a, b,c,d;

printf("ax2+bx+c=0\n Enter the value of \n a=");

scanf("%d",&a);

printf("b=");

scanf("%d",&b);

printf("c=");

scanf("%d",&c);

printf("Your equation is: %dx2+%dx+%d=0\n",a,b,c);

d=pow(b,2)-4*a*c;

if(d<0)

printf("Roots are imaginary");

else if(d==0)

r1= -b/(2.0*a);

printf("Root of the equation is %f",r1);

else

r1=(-b+sqrt(d))/(2.0*a);
r2=(-b-sqrt(d))/(2.0*a);

printf("Roots of the equation are %f and %f",r1,r2);

}
QUESTION 11
Write a C program and a flowchart to input 10 numbers, sort them
and print them in ascending order.
#include<conio.h>
#include<stdio.h>
int main()
{
printf("Enter 10 numbers:\n");
int arr[10];
for(int i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("The numbers in ascending order are:\n");
for(int i=0;i<10;i++)
{
printf("%d\n",arr[i]);
}
}

You might also like