Showing posts with label program. Show all posts
Showing posts with label program. Show all posts
Saturday, February 14, 2015
C Program to find sum of first n natural number
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int i,n,sum=0;
cout<<"How many numbers? ";
cin>>n;
for(i=1;i<=n;++i)
{
sum+=i;
cout<<"
SUM="<<sum<<endl;
}
getch();
}
C program to write student record to a binary file

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct student
{
int rollno;
char *name;
int m1,m2,m3;
}s1;
void main()
{
FILE *fp;
clrscr();
fp=fopen("Record.dat","wb"); //opening binary file in writing mode
if(fp==NULL)
{
printf("File could not open");
exit(0);
}
printf("Enter student details
");
printf("Roll No:");
scanf("%d",&s1.rollno);
printf("Name:");
scanf("%s",s1.name);
printf("Marks in three subjects:");
scanf("%d%d%d",&s1.m1,&s1.m2,&s1.m3);
fwrite(&s1,sizeof(s1),1,fp); //writing to binary file
printf("
Record has been added successfully...!!");
fclose(fp);
getch();
}
Friday, February 13, 2015
Java program to check whether a given number is palindrome or not

class Palindrome
{
public static void main(String...s)
{
int n,m,rev=0,i;
n=Integer.parseInt(s[0]);
m=n;
while(n!=0)
{
i=n%10;
rev=(rev*10)+i;
n/=10;
}
if(m==rev)
System.out.println("
Number is
palindrome");
else
System.out.println("
Number is not
palindrome");
}
}
C Program to reverse a string
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[20],a1[20];
int i,j;
cout<<"Enter any String:"<<"
";
gets(a); cout<<"Reverse of the string is: ";
for(i=0;a[i]!=
Thursday, February 12, 2015
C program to find whether a given number is an Armstrong number or not
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,m=0,x,y;
clrscr();
printf("Enter any three digit numnber:");
scanf("%d",&n);
y=n;
while(n!=0)
{
x=n%10;
m+=pow(x,3);
n=n/10;
}
if(y==m)
printf("The given number is an Armstrong number");
else
printf("The given number is not an Armstrong number");
getch();
}
C program to add two matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
printf("How many rows and columns?");
scanf("%d%d",&m,&n);
printf("
Enter first matrix: ");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
printf("
Enter second matrix: ");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&b[i][j]);
printf("
Matrix after addition:
");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("
");
}
getch();
}
Wednesday, February 11, 2015
C program to check whether given string is palindrome or not

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,flag=1,len;
char str[50];
clrscr();
printf("Enter any string:");
gets(str);
len=strlen(str);
for(i=0,j=len-1;i<len/2;++i,--j)
if(str[i]!=str[j])
{
flag=0;
break;
}
if(flag)
printf("String is palindrome");
else
printf("String is not palindrome");
getch();
}
C Program to print following triangle
*
* *
* *
*****
code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,k,j,n;
cout<<"Enter size of triangle in odd number(min-5,ex-5,7,9):";
cin>>n;
cout<<"
";
for(i=1;i<=n;i+=2)
{
for(j=n;j>i;j-=2)
cout<<" ";
for(k=1;k<=i;++k)
{
if(i==n)
cout<<"*";
else
if(k==1||k==i)
cout<<"*";
else
cout<<" ";
}
cout<<"
";
}
getch();
}
Subscribe to:
Posts (Atom)