Problem Solving Using C
NAME : Shrinand Sekar
CLASS : MECH – A (1
ST YEAR)
ROLL NO. : CB.EN.U4MEE24028
1. Scan the value of n from user. Print all the prime
numbers till n using while
loop. #include<stdio.h>
int main()
{
int n,i=1,j,prime;
printf("Enter a number upto which prime number needs
to be displayed\n");
scanf("%d",&n);
while(i<=n)
{
prime=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf("%d\t",i);
}
i++;
}
}
2.Scan a 3 digit number from user and check if it’s an
Armstrong number. #include<stdio.h>
#include<math.h>
int main()
{
int num,r1,q1,r2,q2,r3,arm;
printf("Enter a three digit number\n");
scanf("%d",&num);
r1=num%10;
q1=num/10;
r2=q1%10;
q2=q1/10;
r3=q2%10;
arm=pow(r3,3)+pow(r2,3)+pow(r1,3);
if(arm==num)
{
printf("%d is an armstrong number",num);
}
else
{
printf("%d is not an armstrong number",num);
}
}
3. Use nested for loop to print the following patterns:
a). #include<stdio.h>
int main()
{
int m;
for(int i=1;i<=3;i++)
{
m=2*i;
for(int j=1;j<=m;j++)
{
printf("*\t");
}
printf("\n");
}
}
b). #include<stdio.h>
int main()
{
int m,n;
for(int i=1;i<=3;i++)
{
m=2*i;
for(int j=1;j<=m;j++)
{
printf("*\t");
}
printf("\n");
}
for(int k=8;k>=0;k--)
{
n=k/2;
for(int l=1;l<=n;l++)
{
printf("*\t");
}
k=k-3;
printf("\n");
}
}
4. Write a C code that collects user data such as Height,
weight ,Age, Sex, SSLC and HSC percentage
#include<stdio.h>
int main()
{
float weight,sslc,hsc;
int height,age;
char string1[15];
printf("Enter your height in cm\n");
scanf("%d",&height);
printf("Enter your weight in kg\n");
scanf("%f",&weight);
printf("Enter your age\n");
scanf("%d",&age);
printf("Enter your gender 'Male' or 'Female'\n");
scanf("%14s",&string1);
printf("Enter your SSLC percentage\n");
scanf("%f",&sslc);
printf("Enter your HSC percentage\n");
scanf("%f",&hsc);
printf("Height :%dcm\n",height);
printf("Weight :%0.2fkg\n",weight);
printf("Age :%dyears\n",age);
printf("Gender :%s\n",string1);
printf("SSLC Percentage :%0.2f%\n",sslc);
printf("HSC Percentage :%0.2f%\n",hsc);
}
5. Write three functions in call by value method for the
following:
#include<stdio.h>
#include<math.h>
void vot_check(char sex,int age)
{
printf("Enter your gender as M or F :\n");
scanf("%c",&sex);
printf("Enter your age :\n");
scanf("%d",&age);
}
void BB_team_check(float height,float weight,char
sex,float press,int h)
{
printf("Enter your height in cm :\n");
scanf("%f",&height);
printf("Enter your weight :\n");
scanf("%f",&weight);
printf("Enter your gender as M or F :\n");
scanf(" %c",&sex);
h = height*10;
press = weight/(pow(h,2));
}
char Scholarship_test(float SSLC,float HSC)
{
printf("Enter your SSLC percentage\n");
scanf("%f",&SSLC);
printf("Enter your HSC percentage\n");
scanf("%f",&HSC);
if(SSLC >=95 && HSC>=95)
{
return 'Y';
}
else
{
return 'N';
}
}
int main()
{
char ch;
printf("Enter V for Voting Eligibility Check , B for
Basketball team Eligibility
Check , S for Scholarship Eligibility Check\n");
scanf("%c",&ch);
if(ch== 'V')
{
int age;
char sex;
vot_check(age,sex);
switch(sex)
{
case 'M' :
{
if(age>=20)
printf("Eligible\n");
else
printf("Not eligible\n");
break;
}
case 'F' :
{
if(age>=18)
printf("Eligible\n");
else
printf("Not eligible\n");
break;
}
default :
{
printf("Invalid gender");
}
}
}
else if(ch== 'B')
{
char sex;
float height,weight,h,press;
BB_team_check(sex,height,weight,h,press);
switch(sex)
{
case 'M' :
{
if(press>=20 && press<=22)
printf("Eligible");
else
printf("Not eligible");
break;
}
case 'F' :
{
if(press>=18 && press<=22)
printf("Eligible");
else
printf("Not eligible");
break;
}
default :
{
printf("Invalid gender");
}
}
}
else if(ch== 'S')
{
float SSLC,HSC;
char result;
Scholarship_test(SSLC,HSC);
if(result=='Y')
{
printf("Eligible");
}
else
{
printf("Not eligible");
}
}
else
{
printf("Enter the correct input");
}