Sunday, August 30, 2015

C Programs on flow control statements (if, switch, loop, break and continue)-2


2015 & 2016 Batch Freshers Registration Link

JOIN MY FACEBOOK GROUP FOR UPDATES 


Printing numbers from 1 to given number.

Intput:  Enter a number : 10
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Program:

main()
{
          int num,i;
          printf(“Enter a number”);
          scanf(“%d”,&num);
          printf(“printing numbers from 1 to %d\n”,num);
          for(i=1;i<=num;i++)
{
                   printf(“%d “,i);
          }
}

Printing odd numbers from 1 to given number.

Intput:  Enter a number : 10
Output: 1,3, 5, 7, 9

Program:

main()
{
          int num,i;
          printf(“Enter a number”);
          scanf(“%d”,&num);
          printf(“printing numbers from 1 to %d\n”,num);
          for(i=1;i<=num;i++)
{
          if(num%2==1)
                             printf(“%d “,i);
          }
}

Printing even numbers from 2 to given number.

Intput:  Enter a number : 10
Output:  2, 4, 6, 8, 10

Program:

main()
{
          int num,i;
          printf(“Enter a number”);
          scanf(“%d”,&num);
          printf(“printing numbers from 1 to %d\n”,num);
          for(i=1;i<=num;i++)
{
          if(num%2==0)
                             printf(“%d “,i);
          }

}

C Program to Calculate Sum of Natural Numbers

Intput:  Enter a number : 10
Output: 55

Program:

main()
{
          int num,i,sum=0;
          printf(“Enter a number”);
          scanf(“%d”,&num);
         
          for(i=1;i<=num;i++)
{
                   sum=sum+i;
          }
          printf(“Sum of natural numbers from 1 to %d is %d”,num,sum);
}


Printing factors of a given number

Intput:  Enter a number : 10
Output: 1, 2, 5, 10

Program:

main()
{
          int num,i;
          printf(“Enter a number”);
          scanf(“%d”,&num);
          printf(“printing factors of %d\n”,num);
          for(i=1;i<=num;i++)
{
          if(num%i==0)
                             printf(“%d “,i);
          }
}

Printing number of factors of a given number

Intput:  Enter a number : 10
Output:4

Program:

main()
{
          int num,i,count=0;
          printf(“Enter a number”);
          scanf(“%d”,&num);
         
          for(i=1;i<=num;i++)
{
          if(num%i==0)
                             count++;
          }
          printf(“Number of factors of %d are %d\n”,num,count);
}

C Program to Check Whether a Number is Prime or Not

Intput:  Enter a number : 10
Output:not a prime

Program:

main()
{
          int num,i,count=0;
          printf(“Enter a number”);
          scanf(“%d”,&num);
         
          for(i=1;i<=num;i++)
{
          if(num%i==0)
                             count++;
          }
          if(count==2)
                   printf(“Prime”);
          else
                   printf(“not a prime”);
}

Printing sum of the factors of a given number

Intput:  Enter a number : 10
Output:18

Program:

main()
{
          int num,i,sum=0;
          printf(“Enter a number”);
          scanf(“%d”,&num);
         
          for(i=1;i<=num;i++)
{
          if(num%i==0)
                             sum=sum+i;
          }
          printf(“sum of factors of %d are %d\n”,num,sum);
}

Write a c program to check given number is perfect number or not.

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).

Intput:  Enter a number : 6
Output: Perfect Number

Program:

main()
{
          int num,i,sum=0;
          printf(“Enter a number”);
          scanf(“%d”,&num);
         
          for(i=1;i<num;i++)
{
          if(num%i==0)
                             sum=sum+i;
          }
          if(sum==num)
printf(“perfect number”);
else
printf(“Not a perfect number”);
}


Contact Geek Placement Preparation

Name

Email *

Message *