Top 10 C-Programming program you must know.....
1.WAP which finds the sum, difference and product of two numbers using switch statement?
=>
#include<stdio.h>
void main()
{
int sum,dif,mul,a,b;
char ch;
printf(“\nEnter first Number:”);
scanf(“%d”,&a);
printf(“\nEnter the sign(+,-,*):”);
scanf(“%s”,&ch);
printf(“\nEnter second Number:”);
scanf(“%d”,&b);
switch(ch)
{
case ‘+’:
sum=a+b;
printf(“\nThe sum of given number is %d”,sum);
break;
case ‘-‘:
dif=a-b;
printf(“\nThe difference of given number is %d”,dif);
break;
case ‘*’:
mul=a*b;
printf(“\nThe product of given number is %d”,mul);
break;
default:
printf(“please enter the correct sign:”);
break;
}
}
2.WAP to display first ten even numbers?
=>
#include<stdio.h>
void main()
{
int i,n=2;
printf(“The first Ten even Number are:”);
for(i=0;i<10;i=i++)
{
printf(“\n%d\t”,n);
n=n+2;
}
}
3.WAP to sort 20 integer number using function?
=>
#include<stdio.h>
void sort(main);
int i,j,temp,a[20];
void main()
{
printf(“Enter any 20 number:”);
for(i=0;i<20;i++)
{
scanf(“%d”,&a[i]);
}
sort();
}
void sort(main)
{
for(i=0;i<20-1;i++)
{
for(j=i+1;j<20;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(“\nNumbers in sorted form is:”);
for(i=0;i<20;i++)
{
printf(“\n%d”,a[i]);
}
}
4.WAP to add two number using function?
=>
#include<stdio.h>
void sum(main);
int a,b,s;
void main()
{
printf(“Enter the value of a:”);
scanf(“%d”,&a);
printf(“Enter the value of b:”);
scanf(“%d”,&b);
sum();
printf(“The sum of given number is %d”,s);
}
void sum(main)
{
s=a+b;
}
5.WAP to enter Name, Post and salary of employee and write it in a file employee.dot?
=>
#include<stdio.h>
void main()
{
FILE*f;
float salary;
char name[20];
char post[20];
f=fopen(“employee.dot”,”w”);
printf(“\nEnter the name of the employee:”);
scanf(“%s”,&name);
printf(“\nEnter the post:”);
scanf(“%s”,&post);
printf(“\nEnter the salary of the employee:”);
scanf(“%f”,&salary);
fprintf(f,”\n%s\t%s\t%f”,name,post,salary);
fclose(f);
}
6.WAP to display the following:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
=>
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d\t”,j);
}
printf(“\n”);
}
}
7.WAP to check if a given number is odd or even using if statement?
=>
#include<stdio.h>
void main()
{
int n;
printf(“\nEnter any number to find wheater it is odd or even:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“\nThe Number is Even.”);
else
printf(“\nThe Number is Odd.”);
}
8.WAP to find factorial of given number?
=>
#include<stdio.h>
void main()
{
int n,fact=1,i;
printf(“Enter the number to find its factorial:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“The factorial of given number is:%d”,fact);
}
9.WAP to find the greater number between four numbers?
=>
#include<stdio.h>
void main()
{
int a,b,c,d;
printf(“Enter the four number you want to find the greatest:”);
scanf(“%d%d%d%d”,&a,&b,&c,&d);
if(a>b&&a>c&&a>d)
printf(“%d is greatest among all.”,a);
else if(b>c&&b>d)
printf(“%d is greatest among all.”,b);
else if(c>d)
printf(“%d is greatest among all.”,c);
else
printf(“%d is greatest among all.”,d);
}
10.WAP to which reads name of 10 students and sort them in alphabetic order?
=>
#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
char name[30][20],temp[20];
printf(“Enter the name of Ten students:”);
for(i=0;i<10;i++)
{
scanf(“%s”,&name[i]);
}
for(i=0;i<10-1;i++)
{
for(j=i+1;j<10;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf(“The Names in sorted order are:”);
for(i=0;i<10;i++)
printf(“\n%s”,name[i]);
}
No comments: