Control Structure

Aim: 3.1 Write a C program to find that the accepted number is Negative, or Positive or Zero.

Code:

#include <stdio.h>

int main()

{

     int x;

     printf("Enter the number x : ");

     scanf("%d",&x);

     if (x > 0)

     printf("%d is positive.",x);

     else if (x < 0)

     printf("%d is negative.",x);

     else if (x == 0)

     printf("%d is zero.",x);

     return 0;

}

Output screenshot:

Aim: 3.2 Write a program to read marks of a student from the keyboard whether the student is pass or fail(using if else).

Code:

#include<stdio.h>

int main()

{

     int marks;

     printf("Enter Marks : ");

     scanf("%d",&marks);

     if(marks<23)

     {

         printf("\nYou are Fail");

     }

     else

     {

         printf("\nCongratulation You are Pass");

     }

     return 0;

}

Output screenshot:

Aim: 3.3 Write a program to read three numbers from the keyboard and find the maximum out of these three. (nested if else)

Code:

#include <stdio.h>

int main() 

{

     double n1, n2, n3;

     printf("Enter three different numbers : ");

     scanf("%lf %lf %lf", &n1, &n2, &n3);

     if (n1>=n2 && n1>=n3)

     printf("%.2f is the largest number.",n1);

     if (n2>=n1 && n2>=n3)

     printf("%.2f is the largest number.",n2);

     if (n3>=n1 && n3>=n2)

     printf("%.2f is the largest number.",n3);

     return 0;

}

Output screenshot:

Aim: 3.4 Write a C program to check whether the entered character is capital, small letter, digit or any special character.

Code:

#include <stdio.h>

int main()

{

    char ch;

    printf("Enter any character : ");

    scanf("%c",&ch);

    if(ch>='A' && ch<='Z')

    printf("Entered character is upper case character");

    else if(ch>='a' && ch<='z')

    printf("Entered character is lower case character");

    else if(ch>='0' && ch<='9')

    printf("Entered character is digit");

    else

    printf("Entered character is special character");

    return 0;

}

Output screenshot:





Aim: 3.5 Write a program to read marks from keyboard and your program should display equivalent grade according to following table(if else ladder)

Marks Grade

100 - 80 Distinction

79 - 60 First Class

59 - 40 Second Class

< 40 Fail

Code:

#include <stdio.h>

int main()

{

     int marks;

     printf("Enter Marks between 0-100 : ");

     scanf("%d",&marks);

     if(marks>100 || marks<0)

     {

         printf("Your Input is out of Range");

     }

     else if(marks>=80)

     {

         printf("You got Distinction.");

     }

     else if(marks>=60)

     {

     printf("You got First Class.");

     }

     else if(marks>=40)

     {

         printf("You got Second Class.");

     }

     else

     {

         printf("You got Fail.");

     }

     return 0;

}

Output screenshot:

Aim: 3.6 Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday.

Code:

#include <stdio.h>

int main()

{

     int day;

     printf("Enter number between 1-7 : ");

     scanf("%d",&day);

     switch (day)

     {

         case 1:

         printf("Sunday");

         break;

         case 2:

         printf("Monday");

         break;

         case 3:

         printf("Tuesday");

         break;

         case 4:

         printf("Wednesday");

         break;

         case 5:

         printf("Thursday");

         break;

         case 6:

         printf("Friday");

         break;

         case 7:

         printf("Saturday");

         break;

         default:

         printf("Invalid input");

     }

     return 0;

}

Output screenshot:

Aim: 3.7 Write a C program to find out the Maximum and Minimum number from given 10 numbers.

Code:

#include <stdio.h>

int main() 

{

    int a[10],i,min,max;

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

    {

        printf("Enter Interger Value %d : ",i+1);

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

        if(i==0)

        {

            min=max=a[i];

        }

        else

        {

            if(min>a[i])

            {

                min=a[i];

            }

            if(max<a[i])

            {

                max=a[i];

            }

        }

    }

    printf("\nMinimum number : %d",min);

    printf("\nMaximum number : %d",max);

    return 0;

}

Output screenshot:

Aim: 3.8 Write a C program to input an integer number and check the last digit of number is even or odd.

Code:

#include <stdio.h>

int main()

{

     int a,n;

     printf("Enter number : ");

     scanf("%d",&a);

     n=a%10;

     if(n%2==0)

     printf("\nlast digit of number %d is %d & it is even",a,n);

     else

     printf("\nlast digit of number %d is %d & it is odd",a,n);

}

Output screenshot:

Aim: 3.9 Write a C program to find the factorial of a given number.

Code:

#include <stdio.h>

int main()

{

    int n,fact=1;

    printf("Enter Number to find its Factorial : ");

    scanf("%d",&n);

    while(n>1)

    {

         fact=fact*n;

         n=n-1;

     }

     printf("\nFactorial of entered number is : %d",fact);

     return 0;

}

Output screenshot:

Aim: 3.10 Write a program to reverse a number.

Code:

#include <stdio.h>

int main()

{

    int n, rev = 0, remainder;

    printf("Enter a number : ");

    scanf("%d",&n);

    while (n!= 0) 

     {

        remainder = n % 10;

        rev = rev * 10 + remainder;

        n = n/10;

     }

    printf("Reversed number = %d",rev);

    return 0;

}

Output screenshot:

Aim: 3.11 Write a program to find out the sum of the first and last digit of a given number.

Code:

#include <stdio.h>

int main()

{

     int num, sum=0, firstDigit, lastDigit;

     printf("Enter any number : ");

     scanf("%d",&num);

     lastDigit = num % 10;

     firstDigit = num;

     while(num >= 10)

     {

         num = num / 10;

     }

     firstDigit = num;

     sum = firstDigit + lastDigit;

     printf("Sum of first and last digit = %d",sum);

     return 0;

}

Output screenshot:

Aim: 3.12 Write a program to calculate average and total of 5 students for 3 subjects (use nested for loops)

Code:

#include<stdio.h>

int main()

{

     int student=0,sum=0,marks=0,sub;

     for(student=0;student<5;student++)

     {

         sum=0;

         printf("\nEnter Marks for Student - %d\n",student+1);

         for(sub=0;sub<3;sub++)

         {

             printf("Subject%d Marks - ",sub+1);

             scanf("%d",&marks);

             sum=sum+marks;

         }

         printf("\nFor Student - %d : ",student+1);

         printf("\nSum = %d",sum);

         printf("\nAverage = %.2f\n",((float)sum)/sub);

     }

     return 0;

}

Output screenshot:

Aim: 3.13 Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50. 

Code:

#include<stdio.h>

int main()

{

     int person,height,weight,count=0;

     for(person=0;person<5;person++)

     {

         printf("\nEnter Detail of Person - %d",person+1);

         printf("\nEnter Height : ");

         scanf("%d",&height);

         printf("Enter Weight : ");

         scanf("%d",&weight);

         if(height>170)

         {

             if(weight<50)

             {

                 count++;

             }

         }

     }

     printf("\nTotal Person having Height > 170 and Weight < 50 : %d",count);

     return 0;

}

Output screenshot:

Aim: 3.14 Write a program to check whether the given number is prime or not.

Code:

#include <stdio.h>

int main() 

{

     int n, i, flag = 0;

     printf("Enter a positive integer: ");

     scanf("%d",&n);

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

    {

         if(n%i==0) 

        {

             flag=1;

             break;

         }

     }

     if(n==1) 

     {

         printf("1 is neither prime nor composite.");

     }

     else 

     {

         if(flag==0)

         printf("%d is a prime number.",n);

         else

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

     }

     return 0;

}

Output screenshot:

No comments:

Post a Comment

If you have any doubts or suggestions, Please let me know