## Sunday, August 30, 2015

### C Programming material ( frequently asked c programs )

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 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,c=0;
printf(“Enter a number”);
scanf(“%d”,&num);

for(i=1;i<=num;i++)
{
if((num%i)==0)
{
c++;
}
}
printf(“number of factors of %d are %d”,num,c);
}

Printing sum of the factors of a given number

Intput:  Enter a number : 10
Output: 18

Program:

main()
{
int num,i,c=0;
printf(“Enter a number”);
scanf(“%d”,&num);

for(i=1;i<=num;i++)
{
if((num%i)==0)
{
c=c+i;
}
}
printf(“Sum of factors of %d is %d”,num,c);

}

Name

Email *

Message *