Q 1. C program to print the pattern.
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
char x='*';
int i,k,l;
clrscr();
for(i=0;i<6;i++)
{
for(k=0;k<5-i;k++)
printf(" ");
for(l=0;l<i;l++)
printf(" %c",x);
printf("\n");
}
getch();
}
#include<conio.h>
void main()
{
char x='*';
int i,k,l;
clrscr();
for(i=0;i<6;i++)
{
for(k=0;k<5-i;k++)
printf(" ");
for(l=0;l<i;l++)
printf(" %c",x);
printf("\n");
}
getch();
}
Q 2. C program to revers an integer.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
while(n)
{
x=n%10;
printf("%d",x);
n=n/10;
}
getch();
}
Q 3. C program to solve two liner equations.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,l,m,n;
float x,y;
clrscr();
printf("Enter a ,b, c:");
scanf("%d%d%d",&a,&b,&c);
printf("Enter l ,m, n:");
scanf("%d%d%d",&l,&m,&n);
x=(float)(c*m-n*b)/(m*a-l*b);
y=(float)(c*l-n*a)/(b*l-m*a);
printf("x= %f\ny=%f\n",x,y);
getch();
}
#include<conio.h>
void main()
{
int a,b,c,l,m,n;
float x,y;
clrscr();
printf("Enter a ,b, c:");
scanf("%d%d%d",&a,&b,&c);
printf("Enter l ,m, n:");
scanf("%d%d%d",&l,&m,&n);
x=(float)(c*m-n*b)/(m*a-l*b);
y=(float)(c*l-n*a)/(b*l-m*a);
printf("x= %f\ny=%f\n",x,y);
getch();
}
Q 4. C program to check whether given number is some power of 2 without using any loop or % operator.
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
clrscr();
printf("Enter the number:");
scanf("%d",&a);
if(!(a&(a-1)))
printf("It is some power of 2");
else
printf("It is not some power of 2");
getch();
return(0);
}
#include<conio.h>
int main()
{
int a;
clrscr();
printf("Enter the number:");
scanf("%d",&a);
if(!(a&(a-1)))
printf("It is some power of 2");
else
printf("It is not some power of 2");
getch();
return(0);
}
Q 5. C program to check whether given string is anagram of first string or not .
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<string.h>
char S[20];
void swap(char * x,char *y);
void pert(char *a,int i,int n);
void main()
{
char a[20];
int l;
clrscr();
printf("Enter the string:");
scanf("%s",a);
printf("Enter the other string:");
scanf("%s",S);
l=strlen(a);
pert(a,0,l);
printf("NO");
getch();
}
void swap(char *x , char *y)
{
char z;
z=*x;
*x=*y;
*y=z;
}
void pert(char *a,int i,int n)
{
int j;
if(i==n)
{
if(strcmp(a,S)==0)
{
printf("YESS");
getch();
exit(0);
}
}
else
{
for(j=i;j<n;j++)
{
swap((a+i),(a+j));
pert(a,i+1,n);
swap((a+i),(a+j));
}
}
}
#include<process.h>
#include<conio.h>
#include<string.h>
char S[20];
void swap(char * x,char *y);
void pert(char *a,int i,int n);
void main()
{
char a[20];
int l;
clrscr();
printf("Enter the string:");
scanf("%s",a);
printf("Enter the other string:");
scanf("%s",S);
l=strlen(a);
pert(a,0,l);
printf("NO");
getch();
}
void swap(char *x , char *y)
{
char z;
z=*x;
*x=*y;
*y=z;
}
void pert(char *a,int i,int n)
{
int j;
if(i==n)
{
if(strcmp(a,S)==0)
{
printf("YESS");
getch();
exit(0);
}
}
else
{
for(j=i;j<n;j++)
{
swap((a+i),(a+j));
pert(a,i+1,n);
swap((a+i),(a+j));
}
}
}
Q 6. C program to print hello world without using semicolon.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
if(printf("Hello word"))
{}
getch();
}
#include<conio.h>
void main()
{
clrscr();
if(printf("Hello word"))
{}
getch();
}