Monday, 13 October 2014

Some Basic Programs of C Language


1. Write a program to print “Assalam-O-Alaikum” on the screen.
Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
printf("Assalam-O-Alaikum");
getch();
}
Output
Assalam-O-Alaikum

2. Write a program to print your name and address into two lines. Using of new line escape sequence.
Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("My name is Zaffar Shah. \nI live in Quetta city.");
getch();
}
Output
My name is Zaffar Shah.
I live in Quetta city.

3. Write a program to read three integers values and print their sum.
Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
int v1, v2, v3, sum;
v1=10;
v2=20;
v3=30;
printf("\nFirst value is %d",v1);
printf("\nSecond value is %d",v2);
printf("\nThird value is %d",v3);
sum=v1+v2+v3;
printf("\nThe sum is %d",sum);
getch();
}
Output
First value is 10
Second value is 20
Third value is 30
The sum is 60

4. Write a program which prints a text of 4 lines consisting of characters, integer values and floating point values using print statement.
Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
int y=2009, m=10;
float r1=3.70, r2=4.90;
clrscr();
printf("\n\nBalochistan is the largest province of Pakistan.\n");
printf("According to %d estimates Balochistan has \na population of roughly %d million.\n",y,m);
printf("Balochistan share of the National Economy has\n");
printf("historically ranged between %.2f percent to %.2f percent.",r1,r2);
getch();
}
Output
Balochistan is the largest province of Pakistan.
According to 2009 estimates Balochistan has
a population of roughly 10 million.
Balochistan share of the National Economy has
historically ranged between 3.70 percent to 4.90 percent.

5. Write a program that calculates the area of a rectangle. Area = Width x Length
Code
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,w,l;
printf("Enter value of rectangle width= ");
scanf("%d",&w);
printf("Enter value of rectangle length= ");
scanf("%d",&l);
a=w*l;
printf("The area of rectangle is %d.",a);
getch();
}
Output
Enter value of rectangle width= 12
Enter value of rectangle length= 15
The area of rectangle is 180.

6. Write a program to calculate the area of a circle. A = pi x r^2
Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
float pi=3.14;
float r,a;
clrscr();
printf("\nEnter Radius of a Circle= ");
scanf("%f",&r);
a=pi*r*r;
printf("\nThe Area of a Rectangle is= %.3f",a);
getch();
}
Output
Enter Radius of a Circle= 12
The Area of a Rectangle is= 452.160

7. Write a program uses the single character format specifier %c.
Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
char c1, c2;
clrscr();
c1=‘S’;
c2=‘H’;
printf(“The simple characters %c and %c.”,c1,c2);
getch();
}
Output
The simple characters S and H.

8. Write a program that performs all arithmetical operations on two variables.
Code
#include<stdio.h>
#include<conio.h>
void main()
{
int s,h;
clrscr();
printf("\nEnter First Value= ");
scanf("%d",&s);
printf("\nEnter Second Value= ");
scanf("%d",&h);
printf("\nAddition = %d",s+h);
printf("\nSubtraction = %d",s-h);
printf("\nMultiplication = %d",s*h);
printf("\nDivision = %d",s/h);
printf("\nReminder = %d",s%h);
getch();
}
Output
Enter First Value= 5
Enter Second Value= 2
Addition = 7
Subtraction = 3
Multiplication = 10
Division = 2
Reminder = 1

9. Write a program that reads and prints using Escape Sequence (asking the name, age, height and gender) of the students using printf and scanf statements.
Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
char nam[20], g[6];
int age;
float h;
clrscr();
printf("Enter Your Name: ");
scanf("%s",&nam);
printf("Enter Your Age: ");
scanf("%d",&age);
printf("Enter Your Height: ");
scanf("%f",&h);
printf("Enter Your Gender [Male/Female]: ");
scanf("%s",&g);
printf("\nYour name is %s.\nYou are %d years old.\n",nam,age);
printf("Your height is %.1f.\n",h);
printf("You are a %s student.\n",g);
getch();
}
Output
Enter Your Name: Maria
Enter Your Age: 19
Enter Your Height: 4.7
Enter Your Gender [Male/Female]: Female
Your name is Maria.
You are 19 years old.
Your height is 4.7.
You are a Female student

10. Write a program in for loop that can print 10 to 1.
Code
#include<stdio.h>
#include<conio.h>
void main()
{
int m;
clrscr();
for(m=10;m>=1;m--)
printf("\n%d",m);
getch();
}
Output
10
9
8
7
6
5
4
3
2
1

12. Write a program which uses for loop statement to generate the multiplication table from 2 to 20.
Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ab;
clrscr();
for(a=2;a<=20;a++)
{
for(b=1;b<=10;b++)
{
ab=a*b;
printf("\n%d x %d = %d",a,b,ab);
}
}
getch();
}
Output
2 x 1 = 2
2 x 2 = 4


2 x 10 = 20
3 x 1 = 3
3 x 2 = 6


20 x 10 = 200

13. Write a program that displays the given star output using nested for loop.
*
* *
* * *
* * * *
* * * * *
Code
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
}

14. Write a program that displays your name for five time using while loop.
Code
#include<stdio.h>
#include<conio.h>
void main()
{
int zs;
zs=1;
clrscr();
while(zs<=5)
{
printf("\nSalman Danish");
zs++;
}
getch();
}
Output
Salman Danish
Salman Danish
Salman Danish
Salman Danish
Salman Danish

15. Write a program to print the corresponding Celsius to Fahrenheit table using while loop.
Code
#include<stdio.h>
#include<conio.h>
int main(void)
{
  float fahr,celsius;
  int lower,upper,step;
  lower=0;
  upper=70;
  step=10;
  clrscr();
  printf("\nC     F\n\n");
  celsius = lower;
  while(celsius <= upper)
  {
  fahr = (9.0/5.0) * celsius + 32.0;
  printf("%3.0f %6.1f\n", celsius, fahr);
  celsius = celsius + step;
  }
getch();
}
Output
C F
0 32.0
10 50.0
20 68.0
30 86.0
40 104.0
50 122.0
60 140.0
70 158.0

No comments:

Post a Comment