Today few pattern programs are posted. The programs are solved using C language
First pattern is:
7654321
*54321
**321
***1
**321
*54321
7654321
#include <stdio.h>
int main()
{
int i,j,k,x,y=0,y1=0;
for ( i=0;i< 7;i++)
{
x=7;
for(k=0;k< y;k++)
{
printf("*");
x=x-2;
}
for(j=0;j< 7-y1;j++)
printf("%d",x--);
printf("\n");
if(i >= 7/2)
{
y--;
y1=y1-2;
}
else
{
y++;
y1=y1+2;
}
}
return 0;
}
Pattern number 2 is as follows:
1
21
123
4321
12345
#include <stdio.h>
void main()
{
int i,j,x=0;
clrscr();
for ( i=0;i< 5;i++)
{
{
if(i%2==0)
x++;
for(j=0;j<=i;j++)
{
printf("%d",x);
if(i%2==0)
x++;
else
x--;
}
printf("\n");
}
getch();
}
The third pattern is as follows:
1
01
010
1010
10101
010101
#include <stdio.h>
void main()
{
int i,j,x=1;
clrscr();
for ( i=0;i< 6;i++)
{
{
for(j=0;j<=i;j++)
{
printf("%d",x);
if(x>0)
x--;
else
x++;
}
printf("\n");
}
getch();
}
sir u have declare "clrscr();" BEFORE D LOOP IN MAIN()? what is d use of dis "clrscr();"? what is called? N last WHY u havw declard "getch();"????
ReplyDeleteclrscr() and getch() are C Language functions.
DeleteSir, why is this program malfunctioning with PrintWriter class (int BlueJ)?
ReplyDeleteScanner in=new Scanner(System.in);
PrintWriter pr=new PrintWriter(System.out, true);
pr.print("Enter number of questions (N) : ");
int n=in.nextInt();
int tr=0, fl=0;
pr.println("Enter answers - True or False");
for(int i=1;i<=n;i++)
{
pr.print("Q. "+i+" > ");
String b=in.next();
if(b.equals("True"))
tr++;
else if(b.equals("False"))
fl++;
else
{
pr.println("Please write properly with proper spelling!");
break;
}
}
pr.println("\nFrequency of True is : "+tr);
pr.println("\nFrequency of False is : "+fl);
import java.io.*;
Deleteimport java.util.*;
class Series
{
Scanner in=new Scanner(System.in);
PrintWriter pr=new PrintWriter(System.out, true);
void take() throws Exception
{
pr.println("Enter number of questions (N) : ");
int n=in.nextInt();
int tr=0, fl=0;
pr.println("Enter answers - True or False");
for(int i=1;i<=n;i++)
{
pr.println("Q. "+i+" > ");
String b=in.next();
if(b.equals("True"))
tr++;
else if(b.equals("False"))
fl++;
else
{
pr.println("Please write properly with proper spelling!");
break;
}
}
pr.println("\nFrequency of True is : "+tr);
pr.println("\nFrequency of False is : "+fl);
}
public static void main(String args[])throws Exception
{
Series tr=new Series();
tr.take();
}
}
S/I: INDIA
ReplyDeleteS/O:INDIA
INDI
IND
IN
I
Wait for 2 days more, I'll post the programs.
Delete