Thursday, 6 October 2016

Basic C Programs

1)Write a C program to find the largest of 3 numbers?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nProgram to find the greatest of  numbers");
printf("\nEnter  values");
scanf("%d%d%d",&a,&b,&c);
if(a<b && a>c)
printf("\nA=%d is the greatest number",a);
else if(b>a && b>c)
printf("\nB=%d is the greatest number",b);
else if(a==b && b==c)
printf("\nAll are equal");
else
printf("\nC=%d is the greatest number",c);
getch();
}

2) Write a C program to find the sum of first 15 even numbers and calculate the square of the sum?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
printf("\nProgram to find sum of first even 15 even numbers and square of sum\n");
for(i=0;i<=15;i++)
if(i%2==0)
{
printf("%d",i);
sum=sum+i;
}
printf("\nSum=%d",sum);
printf("\nSquare of sum=%d",sum*sum);
getch();
}
3) Write a C program to find the total and average of the marks given in an array?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[10],n,tot=0;
float avg;
clrscr();
printf("\nProgram to find the total and average of the marks in an array\n");
printf("\nHow many subjects you want");
scanf("%d",&n);
printf("\nEnter %d subject values\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nMarks in various subjects are\n");
for(i=0;i<n;i++)
{
printf("\nSubject %d:\t%d\n",i+1,a[i]);
tot=tot+a[i];
avg=tot/n;
}
printf("\nTotal of given elements %d",tot);
printf("\nAverage=%f",avg);
getch();
}

4) Write a C program whether the given number is Armstrong or not?
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,am=0,m;
clrscr();
printf("\nProgram to find whether the given number is Armstrong or not");
printf("\nEnter a number");
scanf("%d",&n);
m=n;
while(n!=0)
{
r=n%10;
am=am+r*r*r;
n=n/10;
}
if(m==am)
printf("Yes, the given number is Armstrong");
else
printf("\nNo, it is not an Armstrong number");
getch();
}

5) Write a C program whether the given number is perfect or not?
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("\nProgram to find whether the given number is perfect or not\n");
printf("\nEnter a number\n");
scanf("%d",&n);
printf("\nFactors for the given number are\n");
for(i=1;i<n;i++)
{
if(n%i==0)
{
printf("%d",i);
sum=sum+i;
}
}
printf("\n sum=%d",sum);
if(n==sum)
printf("\nYes the given number is perfect number");
else
printf("\nNo the given number is a perfect number");
getch();
}

No comments:

Post a Comment