Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Programming Questions

Uploaded by

ashaw3312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Programming Questions

Uploaded by

ashaw3312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Programming Questions

IF-ELSE-
1. Write a C program to accept two integers and check whether they are
equal or not.
Ans-
#include<stdio.h>
int main(){
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
if(a==b)
printf("The numbers are equal.");
else
printf("The numbers are not equal");
}
2. Write a C program to check whether a given number is even or odd.
Ans-
#include<stdio.h>
int main(){
int num;
printf("Enter a number:");
scanf("%d",&num);
if(num%2==0)
printf("The number is EVEN.");
else
printf("The number is ODD.");
}
3. Write a C program to find whether a given year is a leap year or not.
Ans-
#include<stdio.h>
int main(){
int year;
printf("Enter an year:");
scanf("%d",&year);
if(year%4==0)
printf("It is a leap year.");
else
printf("It is not a leap year.");
}
4. Write a C program to read the age of a candidate and determine
whether he is eligible to cast his/her own vote.
Ans-
#include<stdio.h>
int main(){
int age;
printf("Enter age of the candidate:");
scanf("%d",&age);
if(age>=18)
printf("The candidate is eligible to give vote.");
else
printf("The candidate is not eligible to give vote.");
}
5. Write a C program to find the largest of three numbers.
Ans-
#include<stdio.h>
int main(){
int n1,n2,n3,lar;
printf("Enter three numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
lar=(n1>n2?(n1>n3?n1:n3):(n2>n3?n2:n3));
printf("The largest number is:%d",lar);
}
6. Write a C program to read temperature in centigrade and display a
suitable message according to the temperature state below:
 Temp < 0 then Freezing weather
 Temp 0-10 then Very Cold weather
 Temp 10-20 then Cold weather
 Temp 20-30 then Normal in Temp
 Temp 30-40 then Its Hot
 Temp >=40 then Its Very Hot
Ans-#include<stdio.h>
int main(){
int temp;
printf("Enter the temperature in centigrade:");
scanf("%d",&temp);
if(temp<0)
printf("Freezing Weather.");
else if(temp>=0 && temp<10)
printf("Very cold weather.");
else if(temp>=10 && temp<20)
printf("Cold Weather.");
else if(temp>=20 && temp<30)
printf("Normal in Temperature.");
else if(temp>=30 && temp<40)
printf("Its hot.");
else
printf("Its very hot.");
}

SWITCH-CASE-
1. Write a C program that takes an integer input from the user and prints
the corresponding month name using a switch case statement.
Ans-
#include<stdio.h>
int main(){
int month;
printf("Enter the month number:");
scanf("%d",&month);
switch(month){
case 1:
printf("The month is JANUARY.");
break;
case 2:
printf("The month is FEBRUARY.");
break;
case 3:
printf("The month is MARCH.");
break;
case 4:
printf("The month is APRIL.");
break;
case 5:
printf("The month is MAY.");
break;
case 6:
printf("The month is JUNE.");
break;
case 7:
printf("The month is JULY.");
break;
case 8:
printf("The month is AUGUST.");
break;
case 9:
printf("The month is SEPTEMBER.");
break;
case 10:
printf("The month is OCTOBER.");
break;
case 11:
printf("The month is NOVEMBER.");
break;
case 12:
printf("The month is DECEMBER.");
break;
default:
printf("INVALID INPUT.");
}
}
2. Develop a simple calculator program that takes two numbers and an
operator (+, -, *, /) as input from the user. Use a switch case statement
to perform the corresponding arithmetic operation and display the
result.
Ans-
#include<stdio.h>
int main(){
int n1,n2,op;
printf("Enter two numbers:");
scanf("%d%d",&n1,&n2);
printf("Enter your choice:");
printf("1-Addition\n2-Subtraction\n3-Division\n4-Multiplication\n5-
Modulus");
scanf("%d",&op);
switch(op){
case 1:
int sum;
sum=n1+n2;
printf("Addition:%d",sum);
break;
case 2:
int sub;
sub=n1-n2;
printf("Substraction:%d",sub);
break;
case 3:
int div;
div=n1/n2;
printf("Division:%d",div);
break;
case 4:
int mul;
mul=n1*n2;
printf("Multiplication:%d",mul);
break;
case 5:
int mod;
mod=n1%n2;
printf("Modulus:%d",mod);
break;
default:
printf("Invalid Input.");
}

}
3. Write a program that takes a character input from the user and checks
whether it's a vowel or a consonant using a switch case statement.
Ans-
#include<stdio.h>
int main(){
char ch;
printf("Enter any character:");
scanf("%c",&ch);
switch(ch){
case 'a':
printf("The character is a VOWEL.");
break;
case 'e':
printf("The character is a VOWEL.");
break;
case 'i':
printf("The character is a VOWEL.");
case 'o':
printf("The character is a VOWEL.");
break;
case 'u':
printf("The character is a VOWEL.");
break;
default:
printf("The character is a CONSONANT.");
}
}
4. Implement a program that calculates the area of different geometric
shapes (circle, rectangle, square). The program should prompt the
user to choose a shape and then input the necessary parameters
(radius, length, width, base, height) to calculate the area using a switch
case statement.
Ans-
#include<stdio.h>
int main(){
int ch;
float area;
printf("Enter your choice:\n1-Area of RECTANGLE\n2-Area of
CIRCLE\n3-Area of SQUARE\n");
scanf("%d",&ch);
switch(ch){
case 1:
int l,b;
printf("Enter the length and breadth of the rectangle:");
scanf("%d%d",&l,&b);
area=l*b;
printf("Area:%.2f",area);
break;
case 2:
int r;
printf("Enter the radius of the circle:");
scanf("%d",&r);
area=3.14*r*r;
printf("Area:%.2f",area);
break;
case 3:
int s;
printf("Enter the side of the square:");
scanf("%d",&s);
area=s*s;
printf("Area:%.2f",area);
break;
default:
printf("INVALID INPUT.");
}
}

FOR-LOOP-
1. Create a program that calculates the factorial of a given number using
a for loop.
Ans-
#include<stdio.h>
int main(){
int num,i,facto=1;
printf("Enter a number:");
scanf("%d",&num);
for(i=num;i>0;i--){
facto=facto*i;
}
printf("Factorial of %d is %d",num,facto);
}
2. Implement a program that prompts the user to enter an integer and
then prints the multiplication table of that number up to 10 using a for
loop.
Ans-
#include<stdio.h>
int main(){
int num,i,mul=1;
printf("Enter a number:");
scanf("%d",&num);
for(i=1;i<=10;i++){
mul=i*num;
printf("\n%d*%d=%d",num,i,mul);
}
}
3. Write a program that prompts the user to enter a number and then
prints its reverse using a for loop.
Ans-
#include<stdio.h>
int main(){
int num,i,rev=0;
printf("Enter a number:");
scanf("%d",&num);
i=num;
while(i>0)
{
rev=rev*10+(i%10);
i=i/10;
}
printf("Reversed number:%d",rev);
}
4. Develop a program that prompts the user to enter the number of terms
and then prints the Fibonacci series up to that number of terms using a
for loop.
Ans-
#include<stdio.h>
int main(){
int num,n,i,t1=0,t2=1;
printf("Enter the value of n:");
scanf("%d",&num);
n=t1+t2;
printf("Fibonacci series:%d %d %d",t1,t2,n);
for(i=3;i<num;i++)
{
t1=t2;
t2=n;
n=t1+t2;
printf(" %d ",n);
}
}

ARRAYS-
1. Create a program that finds the largest element in an array of integers.
Ans-
#include<stdio.h>
int main(){
int arr[30],i,n,lar=0;
printf("Enter the length of the array:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
if(arr[i]>arr[i-1])
lar=arr[i];
}
printf("Largest number is:%d",lar);
}
2. Implement a program to calculate the average of all elements in a
floating-point array.
Ans-
#include<stdio.h>
int main(){
int arr[30],n,sum=0,av=1,i;
printf("Enter the length of the array:");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
sum=sum+arr[i];
}
av=sum/n;
printf("The average of the numbers is:%d",av);
}
3. Develop a program that checks whether a given element exists in an
array or not.
Ans-
#include<stdio.h>
int main(){
int arr[30],n,ch,i;
printf("Enter the length of the array:");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Enter the number to be checked:");
scanf("%d",&ch);
for(i=0;i<n;i++){
if(arr[i]==ch)
printf("The number is there in the array.");
}
}
4. Write a program to find the transpose of a matrix.
Ans-
#include<stdio.h>
int main(){
int arr1[30][30],i,j,arr2[30][30],row,col;
printf("Enter the number of rows and columns of the matrix:");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matirx:");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d",&arr1[i][j]);
}
}
printf("Transposed matrix is:");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
arr2[i][j]=arr1[j][i];
}
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%d",arr2[i][j]);
}
printf("\n");
}
}
5. Implement a program to multiply two matrices.
Ans-
#include<stdio.h>
int main(){
int arr1[30][30],i,j,arr2[30][30],arr3[30][30],row,col,k;
printf("Enter the number of rows and columns of the matrix:");
scanf("%d%d",&row,&col);
printf("Enter the elements of the first matirx:");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d",&arr1[i][j]);
}
}
printf("Enter the elements of the second matirx:");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d",&arr2[i][j]);
}
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
arr3[i][j]=0;
for(k=0;k<col;k++){
arr3[i][j]+=arr1[i][k]*arr2[k][j];
}
}
}
printf("Multiplied matrix:");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%d ",arr3[i][j]);
}
printf("\n");
}
}
6. Write a program to find the sum of two different arrays.
Ans-
#include<stdio.h>
int main(){
int arr1[30],arr2[30],sum=0,i,n;
printf("Enter the length of the arrays:");
scanf("%d",&n);
printf("Enter the elements of the first array:");
for(i=0;i<n;i++){
scanf("%d",&arr1[i]);
}
printf("Enter the elements of the second array:");
for(i=0;i<n;i++){
scanf("%d",&arr2[i]);
}
for(i=0;i<n;i++){
sum+=arr1[i]+arr2[i];
}
printf("Sum of the two arrays:%d",sum);
}

You might also like