0% found this document useful (0 votes)
2 views5 pages

C Programming Exercises on Control Structures

Coding techniques

Uploaded by

Kalai Chelvan G
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)
2 views5 pages

C Programming Exercises on Control Structures

Coding techniques

Uploaded by

Kalai Chelvan G
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

Ex:2.

1
#include<stdio.h>
int main()
{
int years;
printf("enter the years:");
scanf("%d",&years);
if(years%4==0 &&(years%400==0||years%100!=0))
{
printf("\n IT IS A LEAP YEAR %d",years);
}
else
{
printf("IT IS NOT A LEAP YEAR");
}
return 0;
}

Ex:2.2
#include<stdio.h>
void main()
{
int n,ud,td;
printf("ENTER AN INTEGER:");
scanf("%d",&n);
if(n>=10)
{
ud=n%10;
td=(n/10)%10;
if (ud%2==0&&td%2==0)
printf("EE \n");
else if(ud%2!=0&&td%2!=0)
printf("OO \n");
else if(ud%2==0&&td%2!=0)
printf("OE \n");
else
printf("OE \n");
}
else
{
printf("plese enter a number greater than 10");
}
}

Exercise 2.3
#include<stdio.h>
void main()
{
int s1,s2,s3,tot,avg;
printf("Enter the mark for subject 1 : ");
scanf("%d%",&s1);
printf("Enter the mark for subject 2 : ");
scanf("%d%",&s2);
printf("Enter the mark for subject 3 : ");
scanf("%d%",&s3);
tot=s1+s2+s3;
avg=tot/3;
printf("\nTotal marks :%d",tot);
printf("\nAvarage marks :%d",avg);
if(avg>90 && avg<=100)
{
printf("\nGrade:O");
}
else if(avg>80 && avg<=90)
{
printf("\nGrade:A+");
}
else if(avg>70 && avg<=80)
{
printf("\nGrade:A");
}
else if(avg>60 && avg<=70)
{
printf("\nGrade:B+");
}
else if(avg>=55 && avg<=59)
{
printf("\nGrade:B");
}
else if(avg>=50 && avg<55)
{
printf("\nGrade:C");
}
else
{
printf("\nGrade:RA");
}
}

Exercise 2.4

#include<stdio.h>
void main()
{
int n,s;
printf("ENTER A POSITIVE NUMBER :");
scanf("%d",&n);
if (n<0||n>9)
{
printf(" INVALID INPUT \n");
}
else
{
s=n*n;
if(s%10==n)
{
printf("AUTOMORPHIC NUMBER \n");
}
else
{
printf("NOT AUTOMORPHIC NUMBER \n");
}
}
}

Ex:2.5
#include<stdio.h>
voidmain()
{
int r,b,h,a,l,w,ch;

float area;
printf("enter your choice between 1 to 4");
scanf("%d",&ch);
switch(ch);
{
case 1:
printf("enter the valueof r");
scanf("%d",&r);
area=3.14*r*r;
printf("the area of the circle : %d",area);
break;
case 2:
printf("enter the value of b and h : ");
scanf("%d%d",&b,&h);
area=1/2*b*h;
printf("area of a triangle :%d",area);
break;
case 3:
printf("enter the value of a");
scanf("%d",&a);
area=a*a;
printf("area of the square : %d,area");
break;
case 4:
printf("enter the value of l and w");
scanf("%d%d",&l,&w);
area=l*w;
printf("area of the rectangle : %d,area");
break;
default:
printf("invalid value");
break;
}

Common questions

Powered by AI

The program calculates the total and average marks for three subjects. Based on the average, it assigns grades: 'O' for average >90 to 100, 'A+' for >80 to 90, 'A' for >70 to 80, 'B+' for >60 to 70, 'B' for 55 to <60, 'C' for 50 to <55, and 'RA' for below 50 .

The program incorrectly uses integer division for the area calculations, failing to store them as floats, and misuses printing format specifiers. For instance, the area of a triangle uses '1/2*b*h' instead of '0.5*b*h'. The program incorrectly uses '%d' instead of '%f' for float outputs: Corrected code: - Replace '1/2' with '0.5' for the triangle area. - Use '%f' in printf for floats. - Remove redundant semicolon after 'switch(ch)' .

The program classifies numbers based on the parity (even or odd) of their last two digits. If both digits are even, it returns 'EE'; if both are odd, it returns 'OO'. If the unit digit (ud) is even and tens digit (td) is odd, it returns 'OE', and the reverse condition also returns 'OE', which is a redundancy due to logic overlap .

The program checks if the year is divisible by 4 and simultaneously either divisible by 400 or not divisible by 100. These conditions are necessary because, traditionally, a year is a leap year if it is divisible by 4. However, if a year is divisible by 100, it is only a leap year if it is also divisible by 400. These adjustments correct for the slight discrepancy between the solar year and the Julian calendar .

An automorphic number is a number whose square ends in the number itself. The program checks if the last digit of the square of 'n' is equal to 'n'. This is accomplished by squaring the input and checking the condition s%10==n. If true, it declares the number automorphic; otherwise, not .

You might also like