Thursday, May 16, 2013
c programming for mathematical calculations....
-->
1) How to convert a decimal integer to hexadecimal?
#include<stdio.h>
main()
{
int a;
scanf("%d",&a);
printf("%x",a);
}
This program converts a decimal integer to hexadecimal integer.
To convert hexadecimal integer to decimal integer by replacing %d by %x and %x by %b.
#include<stdio.h>
main()
{
int a;
scanf("%x",&a);
printf("%d",a);
}
2) Write a program to find sum of digit of a number ?
#include<stdio.h>
main()
{
int n,i,j,sum=0;
printf("enter a number:");
scanf("%d",&n);
printf("sum of digits of %d is \n",n);
while(n)
{
i=n;
j=i%10;
n=i/10;
sum=sum+j;
}
printf("%d \n",sum);
}
3) Write a c program to check a number prime or not?
#include<stdio.h>
main()
{
int a,b,count=0;
printf("enter no:");
scanf("%d",&b);
for(a=2;a<b;a++)
{
if(b%a==0)
{
count++;
break;
}
}
if(count==0)
{
printf("prime \n");
}
else
{
printf("not prime \n");
}
}
4) Write a c program to find the LCM & HCF of two given numbers?
#include<stdio.h>
main()
{
int n1,n2,d,lcm,hcf,r,product;
printf("enter two numbers:");
scanf("%d%d",&n1,&n2);
product=n1*n2;
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
hcf=n1;
lcm=product/hcf;
printf("HCF: %d",hcf);
printf("\nLCM: %d",lcm);
}
5) Write a c program to list Fibonacci Series up to n terms?
#include<stdio.h>
main()
{
int a=1,b=0,c=0,n;
printf("enter the number:");
scanf("%d",&n);
while(c<=n)
{
printf("%d \n",c);
c=a+b;
a=b;
b=c;
}
}
Algorithm for fibonacci series
1)Declare all variables a=1,b=0,c=0 &n.
(because, first two numbers are 0 & 2)
2)store input(a integer value) to a variable.
3)Answer-----0 1 1 2 3 5 8
so, current number should be sum of two previous numbers.
c=a+b;
a=b;
This command is used to store previous value of
b to a.
b=c;
This command is used to store previous value of
c to a.
Let's start c programming...
-->
Start with a simple program to print a "Hello World" message on screen and i will tell what does the commands in it.
#include<stdio.h>
main()
{
printf("Hello World \n");
}
#include<stdio.h>
C programs are divided into modules or functions. Library functions are grouped category-wise and stored in different files known as header files. It is necessary to tell the compiler about the files to be accessed for accessing it.
#include<name>
#include<stdio.h> includes the files that performs standard input output operations.
main()
main is a part of all C program.
printf("Hello World \n")
printf command for printing output into screen.
A simple program to understand function of #define..
#include<stdio.h>
#define N 10
main()
{
int count;
float sum,average,number;
sum=0;
count=0;
while(count<N)
{
scanf("%f",&number);
sum=sum+number;
count=count+1;
}
average=sum/N;
printf("N=%d Sum=%f",N,sum);
printf("Average=%f",average);
}
In above program N is taken as a constant value.
Here N=10. When "average=sum/N;" loads value of N loaded from "#define N 10".
Program to understand scanf function...
#include<stdio.h>
main()
{
int a;
scanf("%d",&a);
printf("%d",a);
}
Above program will show you function of scanf.
While executing above program the cursor blinks. That indicates program need input from you.
This program print the input what you typed.
In scanf("%d",&a);
&a is the instruction to save your input into a.
(This program for linux. In turbo c you must return a value)
Subscribe to:
Posts (Atom)