Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 3925
Number of posts: 13680

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
need help! simple counting programm Posted by ilga123 on 26 Jan 2012 at 2:15 AM
so i am beginer and just studying all this things and i would like to get help, if anyone can help me :) so there is the thing:
Programm looks like this:

program program1;
var a, b:integer;
begin
b:=0;
for a:=100 downto 1 do
b:=b+a;
writeln('number summ from 1 to 100 is: ',b);
readln;
end.

and i want to add to this program 1 feature; so how to get this program that it makes sum from 1 to 100 but doesnt counts in number which contains 7, like 7; 17; 37; 72 ...
so the thing is that i need to count all numbers (1 to 100) in summ but without those ones who contains 7

any help would be good :)


Report
Re: need help! simple counting programm Posted by Actor on 28 Jan 2012 at 11:29 AM
Why do you count down instead of up?
program program2;
var
   a, b:integer;
begin
   b:=0;
   for a:=100 downto 1 do begin
      if (a mod 10) = 7 then
         continue ;
      if ((a div 10) mod 10) = 7 then
         continue ;
      b:=b+a
   end ;
   writeln('number summ from 1 to 100 is: ',b) ;
   readln;
end.

If you have an older Pascal that does not support continue then
program program3;
var
   a, b:integer;
   flag1,
   flag2 : boolean ;
begin
   b:=0;
   for a:=100 downto 1 do begin
      flag1 := ((a mod 10) = 7) ;
      flag2 := (((a div 10) mod 10) = 7) ;
      if (not flag1) and (not flag2) then
         b:=b+a
   end ;
   writeln('number summ from 1 to 100 is: ',b) ;
   readln;
end.

Report
Re: need help! simple counting programm Posted by ilga123 on 30 Jan 2012 at 1:08 AM
thank you :) it helped :)
Report
Re: need help! simple counting programm Posted by Actor on 28 Jan 2012 at 11:31 AM
Why do you count down instead of up?
program program2;
var
   a, b:integer;
begin
   b:=0;
   for a:=100 downto 1 do begin
      if (a mod 10) = 7 then
         continue ;
      if ((a div 10) mod 10) = 7 then
         continue ;
      b:=b+a
   end ;
   writeln('number summ from 1 to 100 is: ',b) ;
   readln;
end.

If you have an older Pascal that does not support continue then
program program3;
var
   a, b:integer;
   flag1,
   flag2 : boolean ;
begin
   b:=0;
   for a:=100 downto 1 do begin
      flag1 := ((a mod 10) = 7) ;
      flag2 := (((a div 10) mod 10) = 7) ;
      if (not flag1) and (not flag2) then
         b:=b+a
   end ;
   writeln('number summ from 1 to 100 is: ',b) ;
   readln;
end.

Report
Most elegant solution Posted by Actor on 28 Jan 2012 at 11:42 AM
program program4;
{
   probably the most elegant solution
}
var
   a, b : integer ;
begin
   b := 0 ;
   for a := 1 to 100 do
      if ((a mod 10) <> 7) and (((a div 10) mod 10) <> 7) then
         b := b + a ;
   writeln('number sum from 1 to 100 is: ', b) ;
   readln
end.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.